Class: OpenapiFirst::Failure
- Inherits:
-
Object
- Object
- OpenapiFirst::Failure
- Defined in:
- lib/openapi_first/failure.rb
Overview
A failure object returned when validation or parsing of a request or response has failed. This returned in ValidatedRequest#error and ValidatedResponse#error.
Instance Attribute Summary collapse
- #errors ⇒ Object readonly
-
#type ⇒ Object
readonly
Example: :invalid_body.
Class Method Summary collapse
Instance Method Summary collapse
- #exception(context = nil) ⇒ Object
- #exception_message ⇒ Object
-
#initialize(type, message: nil, errors: nil) ⇒ Failure
constructor
A new instance of Failure.
-
#message ⇒ Object
A generic error message.
Constructor Details
#initialize(type, message: nil, errors: nil) ⇒ Failure
Returns a new instance of Failure.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/openapi_first/failure.rb', line 36 def initialize(type, message: nil, errors: nil) unless TYPES.key?(type) raise ArgumentError, "type must be one of #{TYPES.keys} but was #{type.inspect}" end @type = type = @errors = errors end |
Instance Attribute Details
#errors ⇒ Object (readonly)
52 53 54 |
# File 'lib/openapi_first/failure.rb', line 52 def errors @errors end |
#type ⇒ Object (readonly)
Example: :invalid_body
49 50 51 |
# File 'lib/openapi_first/failure.rb', line 49 def type @type end |
Class Method Details
.fail!(type, message: nil, errors: nil) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/openapi_first/failure.rb', line 25 def self.fail!(type, message: nil, errors: nil) throw FAILURE, new( type, message:, errors: ) end |
Instance Method Details
#exception(context = nil) ⇒ Object
59 60 61 |
# File 'lib/openapi_first/failure.rb', line 59 def exception(context = nil) TYPES.fetch(type).first.new(, context) end |
#exception_message ⇒ Object
63 64 65 66 67 |
# File 'lib/openapi_first/failure.rb', line 63 def _, = TYPES.fetch(type) [, || ].compact.join(' ') end |
#message ⇒ Object
A generic error message
55 56 57 |
# File 'lib/openapi_first/failure.rb', line 55 def ||= end |