Exception: Vindi::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/vindi/error.rb

Overview

Custom error class for rescuing from all Vindi errors

Constant Summary collapse

ClientError =

Raised when Vindi returns a 4xx HTTP status code

Class.new(self)
BadRequest =

Raised when Vindi returns the HTTP status code 400

Class.new(ClientError)
Unauthorized =

Raised when Vindi returns the HTTP status code 401

Class.new(ClientError)
Forbidden =

Raised when Vindi returns the HTTP status code 403

Class.new(ClientError)
NotFound =

Raised when Vindi returns the HTTP status code 404

Class.new(ClientError)
NotAcceptable =

Raised when Vindi returns the HTTP status code 406

Class.new(ClientError)
UnprocessableEntity =

Raised when Vindi returns the HTTP status code 422

Class.new(ClientError)
TooManyRequests =

Raised when Vindi returns the HTTP status code 429

Class.new(ClientError)
ServerError =

Raised when Vindi returns a 5xx HTTP status code

Class.new(self)
InternalServerError =

Raised when Vindi returns the HTTP status code 500

Class.new(ServerError)
BadGateway =

Raised when Vindi returns the HTTP status code 502

Class.new(ServerError)
ServiceUnavailable =

Raised when Vindi returns the HTTP status code 503

Class.new(ServerError)
GatewayTimeout =

Raised when Vindi returns the HTTP status code 504

Class.new(ServerError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Error

Returns a new instance of Error.



70
71
72
# File 'lib/vindi/error.rb', line 70

def initialize(response)
  super(build_error_message(response))
end

Instance Attribute Details

#status_codeObject

Returns the value of attribute status_code.



4
5
6
# File 'lib/vindi/error.rb', line 4

def status_code
  @status_code
end

Class Method Details

.from_response(response) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vindi/error.rb', line 46

def from_response(response)
  status = response.status.to_i

  klass = case status
          when 400      then Vindi::Error::BadRequest
          when 401      then Vindi::Error::Unauthorized
          when 403      then Vindi::Error::Forbidden
          when 404      then Vindi::Error::NotFound
          when 406      then Vindi::Error::NotAcceptable
          when 415      then Vindi::Error::UnsupportedMediaType
          when 422      then Vindi::Error::UnprocessableEntity
          when 429      then Vindi::Error::TooManyRequests
          when 400..499 then Vindi::Error::ClientError
          when 500      then Vindi::Error::InternalServerError
          when 502      then Vindi::Error::BadGateway
          when 503      then Vindi::Error::ServiceUnavailable
          when 504      then Vindi::Error::GatewayTimeout
          when 500..599 then Vindi::Error::ServerError
          end

  klass.new(response) if klass
end

Instance Method Details

#build_error_message(response) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/vindi/error.rb', line 74

def build_error_message(response)
  return unless response&.body

  self.status_code = response.status

  response.body
end