Class: OpenapiFirst::Failure
- Inherits:
-
Object
- Object
- OpenapiFirst::Failure
- 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
-
#error_type ⇒ Object
(also: #type)
readonly
Example: :invalid_body.
- #errors ⇒ Object readonly
- #message ⇒ Object readonly
Class Method Summary collapse
Instance Method Summary collapse
- #exception_message ⇒ Object
-
#initialize(error_type, message: nil, errors: nil) ⇒ Failure
constructor
A new instance of Failure.
-
#raise! ⇒ Object
Raise an exception that fits the failure.
Constructor Details
#initialize(error_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(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 = @errors = errors end |
Instance Attribute Details
#error_type ⇒ Object (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 |
#errors ⇒ Object (readonly)
57 58 59 |
# File 'lib/openapi_first/failure.rb', line 57 def errors @errors end |
#message ⇒ Object (readonly)
54 55 56 |
# File 'lib/openapi_first/failure.rb', line 54 def @message end |
Class Method Details
.fail!(error_type, message: nil, errors: nil) ⇒ Object
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_message ⇒ Object
65 66 67 68 69 |
# File 'lib/openapi_first/failure.rb', line 65 def _, = TYPES.fetch(error_type) "#{} #{@message || }" end |
#raise! ⇒ Object
Raise an exception that fits the failure.
60 61 62 63 |
# File 'lib/openapi_first/failure.rb', line 60 def raise! exception, = TYPES.fetch(error_type) raise exception, end |