Class: OpenapiFirst::Failure

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_first/failure.rb

Overview

A failure object returned when validation of request or response has failed.

Constant Summary collapse

FAILURE =
:openapi_first_validation_failure

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error_type, message: nil, errors: nil) ⇒ Failure

Returns a new instance of Failure.

Parameters:



36
37
38
39
40
41
42
43
44
45
# File 'lib/openapi_first/failure.rb', line 36

def initialize(error_type, message: nil, errors: nil)
  unless TYPES.key?(error_type)
    raise ArgumentError,
          "error_type must be one of #{TYPES.keys} but was #{error_type.inspect}"
  end

  @error_type = error_type
  @message = message
  @errors = errors
end

Instance Attribute Details

#error_typeObject (readonly) Also known as: type

Example: :invalid_body



50
51
52
# File 'lib/openapi_first/failure.rb', line 50

def error_type
  @error_type
end

#errorsObject (readonly)



57
58
59
# File 'lib/openapi_first/failure.rb', line 57

def errors
  @errors
end

#messageObject (readonly)



54
55
56
# File 'lib/openapi_first/failure.rb', line 54

def message
  @message
end

Class Method Details

.fail!(error_type, message: nil, errors: nil) ⇒ Object

Parameters:



25
26
27
28
29
30
31
# File 'lib/openapi_first/failure.rb', line 25

def self.fail!(error_type, message: nil, errors: nil)
  throw FAILURE, new(
    error_type,
    message:,
    errors:
  )
end

Instance Method Details

#exception_messageObject



65
66
67
68
69
# File 'lib/openapi_first/failure.rb', line 65

def exception_message
  _, message_prefix = TYPES.fetch(error_type)

  "#{message_prefix} #{@message || generate_message}"
end

#raise!Object

Raise an exception that fits the failure.

Raises:

  • (exception)


60
61
62
63
# File 'lib/openapi_first/failure.rb', line 60

def raise!
  exception, = TYPES.fetch(error_type)
  raise exception, exception_message
end