Module: Legion::Extensions::Lakera::Helpers::Errors

Defined in:
lib/legion/extensions/lakera/helpers/errors.rb

Defined Under Namespace

Classes: ApiError, AuthenticationError, InvalidRequestError, RateLimitError, ServerError

Constant Summary collapse

STATUS_MAP =
{
  400 => InvalidRequestError,
  401 => AuthenticationError,
  429 => RateLimitError
}.freeze
RETRYABLE =
[RateLimitError, ServerError].freeze

Class Method Summary collapse

Class Method Details

.from_response(status:, body:) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/legion/extensions/lakera/helpers/errors.rb', line 34

def from_response(status:, body:)
  error_hash  = body.is_a?(Hash) ? (body[:error] || body['error']) : nil # rubocop:disable Legion/Framework/ApiStringKeys
  error_type  = error_hash.is_a?(Hash) ? (error_hash[:type] || error_hash['type']) : nil
  message     = error_hash.is_a?(Hash) ? (error_hash[:message] || error_hash['message']) : nil
  message   ||= body.to_s

  klass = STATUS_MAP[status] || (status >= 500 ? ServerError : InvalidRequestError)

  klass.new(message, status: status, error_type: error_type, body: body)
end

.retryable?(error) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/legion/extensions/lakera/helpers/errors.rb', line 45

def retryable?(error)
  RETRYABLE.any? { |klass| error.is_a?(klass) }
end