Exception: Observable::StructuredError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/observable/structured_error.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, type: self.class.name, context: {}) ⇒ StructuredError

Returns a new instance of StructuredError.



7
8
9
10
11
# File 'lib/observable/structured_error.rb', line 7

def initialize(message, type: self.class.name, context: {})
  @context = context.to_h
  @type = (type || self.class.name).to_s
  super(message)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



5
6
7
# File 'lib/observable/structured_error.rb', line 5

def context
  @context
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/observable/structured_error.rb', line 5

def type
  @type
end

Class Method Details

.from_error(error) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/observable/structured_error.rb', line 25

def self.from_error(error)
  custom_converter_result = try_custom_converter(error)
  if custom_converter_result
    new(
      custom_converter_result[:message],
      type: custom_converter_result[:type],
      context: custom_converter_result[:context]
    )
  else
    new(
      safe_message(error),
      type: safe_type(error),
      context: safe_context(error)
    )
  end
end

Instance Method Details

#inspectObject



17
18
19
# File 'lib/observable/structured_error.rb', line 17

def inspect
  "#<#{self.class.name}: #{message}, type=#{type}, context=#{@context}>"
end

#pretty_printObject



21
22
23
# File 'lib/observable/structured_error.rb', line 21

def pretty_print
  "#<#{self.class.name}: message: #{message}, type: #{type}, context: #{@context}>"
end

#to_hObject



13
14
15
# File 'lib/observable/structured_error.rb', line 13

def to_h
  {message: message}.merge(@context)
end