Sha256: 1ce9c9c213028e87d8eaf752283a09e8896e9232d20398186231b7bc225bd963
Contents?: true
Size: 1.54 KB
Versions: 2
Compression:
Stored size: 1.54 KB
Contents
module SphereEngine # Custom error class for rescuing from all SphereEngine errors class Error < StandardError # @return [Integer] attr_reader :code # Initializes a new Error object # # @param message [Exception, String] # @param code [Integer] # @return [SphereEngine::Error] def initialize(message = '', code = nil) super(message) @code = code end # ClientErrors(Raised when SphereEngine returns a 4xx HTTP status code) # # Raised when SphereEngine returns the HTTP status code 400 BadRequest = Class.new(self) # Raised when SphereEngine returns the HTTP status code 401 Unauthorized = Class.new(self) ERRORS = { 400 => SphereEngine::Error::BadRequest, 401 => SphereEngine::Error::Unauthorized } class << self # Create a new error from an HTTP response # # @param body [String] # @param headers [Hash] # @return [SphereEngine::Error] def from_response(body) message, code = parse_error(body) new(message, code) end private def parse_error(body) if body.nil? || body.empty? ['', nil] elsif body[:message] [body[:message], nil] elsif body[:errors] extract_message_from_errors(body) end end def extract_message_from_errors(body) first = Array(body[:errors]).first if first.is_a?(Hash) [first[:message].chomp, first[:code]] else [first.chomp, nil] end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
sphere_engine-1.2.0.pre | lib/sphere_engine/error.rb |
sphere_engine-1.1.0.pre | lib/sphere_engine/error.rb |