Module: AfterShip::ErrorHandler

Defined in:
lib/after_ship/core/error_handler.rb

Overview

Response handling logic.

Constant Summary collapse

SUCCESS_CODES =

These mean that the response is good.

[200, 201]
CODE_TO_ERROR_MAP =

Map meta codes to error classes.

{
  400  => Error::BadRequest,
  4001 => Error::InvalidJsonData,
  4002 => Error::InvalidJsonData,
  4003 => Error::TrackingAlreadyExists,
  4004 => Error::TrackingDoesNotExist,
  4005 => Error::TrackingNumberInvalid,
  4006 => Error::TrackingObjectRequired,
  4007 => Error::TrackingNumberRequired,
  4008 => Error::FieldInvalid,
  4009 => Error::FieldRequired,
  4010 => Error::SlugInvalid,
  4011 => Error::CourierFieldInvalid,
  4012 => Error::CourierNotDetected,
  4013 => Error::RetrackNotAllowed,
  4016 => Error::RetrackNotAllowed,
  4014 => Error::NotificationRequired,
  4015 => Error::IdInvalid,
  401  => Error::Unauthorized,
  403  => Error::Forbidden,
  404  => Error::NotFound,
  429  => Error::TooManyRequests,
  500  => Error::InternalError,
  502  => Error::InternalError,
  503  => Error::InternalError,
  504  => Error::InternalError
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check(meta) ⇒ Object

Check the meta code of the response. If it isn’t 200 or 201, raise an error.

Parameters:

  • meta (Hash)

Options Hash (meta):

  • :code (Fixnum)
  • :message (String, nil)
  • :type (String, nil)


53
54
55
56
57
58
59
60
# File 'lib/after_ship/core/error_handler.rb', line 53

def check(meta)
  code = meta.fetch(:code)

  return if SUCCESS_CODES.include?(code)

  error_class = error_class_for(code)
  fail error_class, meta[:message]
end

.error_class_for(code) ⇒ Object

Pick the corresponding error class for the code.



63
64
65
# File 'lib/after_ship/core/error_handler.rb', line 63

def error_class_for(code)
  CODE_TO_ERROR_MAP[code] || Error::UnknownError
end

.precheck(response) ⇒ Object

Did it timeout? If the body empty?

Parameters:

  • response (Typhoeus::Response)


41
42
43
44
# File 'lib/after_ship/core/error_handler.rb', line 41

def precheck(response)
  fail Error::Timeout, "#{response.effective_url} cannot be reached" if
    response.timed_out?
end

Instance Method Details

#check(meta) ⇒ Object (private)

Check the meta code of the response. If it isn’t 200 or 201, raise an error.

Parameters:

  • meta (Hash)

Options Hash (meta):

  • :code (Fixnum)
  • :message (String, nil)
  • :type (String, nil)


53
54
55
56
57
58
59
60
# File 'lib/after_ship/core/error_handler.rb', line 53

def check(meta)
  code = meta.fetch(:code)

  return if SUCCESS_CODES.include?(code)

  error_class = error_class_for(code)
  fail error_class, meta[:message]
end

#error_class_for(code) ⇒ Object (private)

Pick the corresponding error class for the code.



63
64
65
# File 'lib/after_ship/core/error_handler.rb', line 63

def error_class_for(code)
  CODE_TO_ERROR_MAP[code] || Error::UnknownError
end

#precheck(response) ⇒ Object (private)

Did it timeout? If the body empty?

Parameters:

  • response (Typhoeus::Response)


41
42
43
44
# File 'lib/after_ship/core/error_handler.rb', line 41

def precheck(response)
  fail Error::Timeout, "#{response.effective_url} cannot be reached" if
    response.timed_out?
end