class Crowdkit::ServiceError
Custom error class for rescuing from all CrowdFlower errors
Attributes
response[R]
Public Class Methods
from_response(response)
click to toggle source
Returns the appropriate Crowdkit::Error
sublcass based on status and response message
@param [Hash] response HTTP response @return [Crowdkit::Error]
# 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
new(response=nil)
click to toggle source
Calls superclass method
# File lib/crowdkit/error.rb, line 91 def initialize(response=nil) @response = Faraday::Response.new(response) super(build_error_message) end
Public Instance Methods
documentation_url()
click to toggle source
Documentation URL returned by the API
for some errors
@return [String]
# File lib/crowdkit/error.rb, line 99 def documentation_url data[:_links][:documentation] if data.is_a?(Hash) && data[:_links] end
Private Instance Methods
build_error_message()
click to toggle source
# File lib/crowdkit/error.rb, line 139 def build_error_message return nil if @response.env.nil? message = "#{@response.env[:method].to_s.upcase} " message << "#{@response.env[:url]} - " message << "(#{@response.status}):" message << "#{response_error_summary}" unless response_error_summary.nil? message << " // See: #{documentation_url}" unless documentation_url.nil? message end
data()
click to toggle source
# File lib/crowdkit/error.rb, line 115 def data @data ||= if (body = @response.body) && !body.empty? if body.is_a?(String) && @response.headers && @response.headers[:content_type] =~ /json/ Sawyer::Agent.serializer.decode(body) else body end else nil end end
response_error_summary()
click to toggle source
# File lib/crowdkit/error.rb, line 131 def response_error_summary return nil if errors.empty? summary = "\nError summary:\n\t" summary << data[:errors].join("\n\t") summary << "\n" end