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
-
#error_type ⇒ Object
deprecated
Deprecated.
Please use #type instead
- #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.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/openapi_first/failure.rb', line 35 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 @message = @errors = errors end |
Instance Attribute Details
#errors ⇒ Object (readonly)
51 52 53 |
# File 'lib/openapi_first/failure.rb', line 51 def errors @errors end |
#type ⇒ Object (readonly)
Example: :invalid_body
48 49 50 |
# File 'lib/openapi_first/failure.rb', line 48 def type @type end |
Class Method Details
.fail!(type, message: nil, errors: nil) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/openapi_first/failure.rb', line 24 def self.fail!(type, message: nil, errors: nil) throw FAILURE, new( type, message:, errors: ) end |
Instance Method Details
#error_type ⇒ Object
Deprecated.
Please use #type instead
69 70 71 72 |
# File 'lib/openapi_first/failure.rb', line 69 def error_type warn 'OpenapiFirst::Failure#error_type is deprecated. Use #type instead.' type end |
#exception(context = nil) ⇒ Object
58 59 60 |
# File 'lib/openapi_first/failure.rb', line 58 def exception(context = nil) TYPES.fetch(type).first.new(, context) end |
#exception_message ⇒ Object
62 63 64 65 66 |
# File 'lib/openapi_first/failure.rb', line 62 def _, = TYPES.fetch(type) [, @message || ].compact.join(' ') end |
#message ⇒ Object
A generic error message
54 55 56 |
# File 'lib/openapi_first/failure.rb', line 54 def @message ||= end |