Exception: Notisend::ApiError

Inherits:
Error
  • Object
show all
Defined in:
lib/notisend/errors.rb

Overview

Non-2xx API response. Body format: "errors":[{"code":..,"detail":"."errors":[{"code":..,"detail":".."]}

Constant Summary collapse

STATUS_MAP =
{
  400 => 'Notisend::BadRequest',
  401 => 'Notisend::Unauthorized',
  403 => 'Notisend::Forbidden',
  404 => 'Notisend::NotFound',
  412 => 'Notisend::PreconditionFailed',
  422 => 'Notisend::UnprocessableEntity',
  429 => 'Notisend::TooManyRequests'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, body:) ⇒ ApiError

Returns a new instance of ApiError.



37
38
39
40
41
# File 'lib/notisend/errors.rb', line 37

def initialize(status:, body:)
  @status = status
  @errors = parse_errors(body)
  super(detail || "Notisend API error (status #{status})")
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



30
31
32
# File 'lib/notisend/errors.rb', line 30

def errors
  @errors
end

#statusObject (readonly)

Returns the value of attribute status.



30
31
32
# File 'lib/notisend/errors.rb', line 30

def status
  @status
end

Class Method Details

.for(status:, body:) ⇒ Object



32
33
34
35
# File 'lib/notisend/errors.rb', line 32

def self.for(status:, body:)
  klass_name = STATUS_MAP[status] || ((500..599).cover?(status) ? 'Notisend::ServerError' : name)
  Object.const_get(klass_name).new(status: status, body: body)
end

Instance Method Details

#detailObject



43
44
45
# File 'lib/notisend/errors.rb', line 43

def detail
  errors.first&.fetch('detail', nil)
end