Class: OpenapiFirst::Failure

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Failure.

Parameters:



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 = message
  @errors = errors
end

Instance Attribute Details

#errorsObject (readonly)



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

def errors
  @errors
end

#typeObject (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

Parameters:



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_typeObject

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(exception_message, context)
end

#exception_messageObject



62
63
64
65
66
# File 'lib/openapi_first/failure.rb', line 62

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

  [message_prefix, @message || generate_message].compact.join(' ')
end

#messageObject

A generic error message



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

def message
  @message ||= exception_message
end