Class: SenroUsecaser::Error
- Inherits:
-
Object
- Object
- SenroUsecaser::Error
- Defined in:
- lib/senro_usecaser/error.rb
Overview
Represents an error in a Result
Instance Attribute Summary collapse
-
#cause ⇒ Object
readonly
: Exception?.
-
#code ⇒ Object
readonly
: Symbol.
-
#field ⇒ Object
readonly
: Symbol?.
-
#message ⇒ Object
readonly
: String.
Class Method Summary collapse
-
.from_exception(exception, code: :exception) ⇒ Object
Creates an Error from an exception.
Instance Method Summary collapse
-
#==(other) ⇒ Object
: (Error) -> bool.
-
#caused_by_exception? ⇒ Boolean
Returns true if this error was caused by an exception.
-
#initialize(code:, message:, field: nil, cause: nil) ⇒ Error
constructor
: (code: Symbol, message: String, ?field: Symbol?, ?cause: Exception?) -> void.
-
#inspect ⇒ Object
: () -> String.
-
#to_s ⇒ Object
: () -> String.
Constructor Details
#initialize(code:, message:, field: nil, cause: nil) ⇒ Error
: (code: Symbol, message: String, ?field: Symbol?, ?cause: Exception?) -> void
39 40 41 42 43 44 |
# File 'lib/senro_usecaser/error.rb', line 39 def initialize(code:, message:, field: nil, cause: nil) @code = code @message = @field = field @cause = cause end |
Instance Attribute Details
#cause ⇒ Object (readonly)
: Exception?
36 37 38 |
# File 'lib/senro_usecaser/error.rb', line 36 def cause @cause end |
#code ⇒ Object (readonly)
: Symbol
27 28 29 |
# File 'lib/senro_usecaser/error.rb', line 27 def code @code end |
#field ⇒ Object (readonly)
: Symbol?
33 34 35 |
# File 'lib/senro_usecaser/error.rb', line 33 def field @field end |
#message ⇒ Object (readonly)
: String
30 31 32 |
# File 'lib/senro_usecaser/error.rb', line 30 def @message end |
Class Method Details
.from_exception(exception, code: :exception) ⇒ Object
Creates an Error from an exception
: (Exception, ?code: Symbol) -> Error
49 50 51 52 53 54 55 |
# File 'lib/senro_usecaser/error.rb', line 49 def self.from_exception(exception, code: :exception) new( code: code, message: exception., cause: exception ) end |
Instance Method Details
#==(other) ⇒ Object
: (Error) -> bool
65 66 67 68 69 |
# File 'lib/senro_usecaser/error.rb', line 65 def ==(other) return false unless other.is_a?(Error) code == other.code && == other. && field == other.field && cause == other.cause end |
#caused_by_exception? ⇒ Boolean
Returns true if this error was caused by an exception
: () -> bool
60 61 62 |
# File 'lib/senro_usecaser/error.rb', line 60 def caused_by_exception? !cause.nil? end |
#inspect ⇒ Object
: () -> String
78 79 80 81 82 83 84 85 86 |
# File 'lib/senro_usecaser/error.rb', line 78 def inspect parts = [ "code=#{code.inspect}", "message=#{.inspect}", "field=#{field.inspect}" ] parts << "cause=#{cause.class}" if cause "#<#{self.class.name} #{parts.join(" ")}>" end |
#to_s ⇒ Object
: () -> String
72 73 74 75 |
# File 'lib/senro_usecaser/error.rb', line 72 def to_s base = field ? "[#{field}] #{} (#{code})" : "#{} (#{code})" cause ? "#{base} caused by #{cause.class}" : base end |