Exception: Crowdkit::ServiceError

Inherits:
Error
  • Object
show all
Defined in:
lib/crowdkit/error.rb

Overview

Custom error class for rescuing from all CrowdFlower errors

Direct Known Subclasses

ClientError, ServerError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ ServiceError

Returns a new instance of ServiceError.



91
92
93
94
# File 'lib/crowdkit/error.rb', line 91

def initialize(response=nil)
  @response = Faraday::Response.new(response)
  super(build_error_message)
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



58
59
60
# File 'lib/crowdkit/error.rb', line 58

def response
  @response
end

Class Method Details

.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

Instance Method Details

#documentation_urlString

Documentation URL returned by the API for some errors

Returns:

  • (String)


99
100
101
# File 'lib/crowdkit/error.rb', line 99

def documentation_url
  data[:_links][:documentation] if data.is_a?(Hash) && data[:_links]
end

#errorsArray<Hash>

Array of validation errors

Returns:



105
106
107
108
109
110
111
# File 'lib/crowdkit/error.rb', line 105

def errors
  if data && data.is_a?(Hash)
    data[:errors] || []
  else
    []
  end
end