Method: Crowdkit::ServiceError.from_response

Defined in:
lib/crowdkit/error.rb

.from_response(response) ⇒ Crowdkit::Error

Returns the appropriate Crowdkit::Error sublcass based on status and response message

Parameters:

  • response (Hash)

    HTTP response

Returns:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/crowdkit/error.rb', line 65

def self.from_response(response)
  status  = response[:status].to_i
  body    = response[:body].to_s
  headers = response[:response_headers]

  if klass =  case status
              when 400      then Crowdkit::BadRequest
              when 401      then Crowdkit::Unauthorized
              when 403      then Crowdkit::Forbidden
              when 404      then Crowdkit::NotFound
              when 406      then Crowdkit::NotAcceptable
              when 409      then Crowdkit::Conflict
              when 415      then Crowdkit::UnsupportedMediaType
              when 422      then Crowdkit::UnprocessableEntity
              when 429      then Crowdkit::TooManyRequests
              when 400..499 then Crowdkit::ClientError
              when 500      then Crowdkit::InternalServerError
              when 501      then Crowdkit::NotImplemented
              when 502      then Crowdkit::BadGateway
              when 503      then Crowdkit::ServiceUnavailable
              when 500..599 then Crowdkit::ServerError
              end
    klass.new(response)
  end
end