Class: Flowy::Failure
Instance Attribute Summary collapse
-
#error_code ⇒ Object
readonly
Returns the value of attribute error_code.
-
#error_data ⇒ Object
readonly
Returns the value of attribute error_data.
-
#error_description ⇒ Object
readonly
Returns the value of attribute error_description.
-
#error_title ⇒ Object
readonly
Returns the value of attribute error_title.
-
#parent_failure ⇒ Object
readonly
Returns the value of attribute parent_failure.
Instance Method Summary collapse
- #and_then ⇒ Object
- #failure? ⇒ Boolean
- #failures_chain ⇒ Object
-
#initialize(error_code:, error_data: {}, error_title: nil, error_description: nil, parent_failure: nil) ⇒ Failure
constructor
A new instance of Failure.
- #is?(error_code:) ⇒ Boolean
-
#map_failure(error_code: nil, error_data: {}, error_title: nil, error_description: nil) ⇒ Object
In block form, when the block-returned Failure omits parent_failure, self is wired in as parent_failure so the chain is never broken.
- #merge_data(extra = nil) ⇒ Object
- #on_failure {|_self| ... } ⇒ Object
- #on_success ⇒ Object
- #or_else ⇒ Object
- #raise! ⇒ Object
- #success? ⇒ Boolean
- #tap {|_self| ... } ⇒ Object
- #to_hash ⇒ Object
Methods included from Result
_collect_results, _deep_merge, failure, success, wrap
Constructor Details
#initialize(error_code:, error_data: {}, error_title: nil, error_description: nil, parent_failure: nil) ⇒ Failure
Returns a new instance of Failure.
7 8 9 10 11 12 13 |
# File 'lib/flowy/failure.rb', line 7 def initialize(error_code:, error_data: {}, error_title: nil, error_description: nil, parent_failure: nil) @error_code = error_code @error_data = error_data @error_title = error_title @error_description = error_description @parent_failure = parent_failure end |
Instance Attribute Details
#error_code ⇒ Object (readonly)
Returns the value of attribute error_code.
5 6 7 |
# File 'lib/flowy/failure.rb', line 5 def error_code @error_code end |
#error_data ⇒ Object (readonly)
Returns the value of attribute error_data.
5 6 7 |
# File 'lib/flowy/failure.rb', line 5 def error_data @error_data end |
#error_description ⇒ Object (readonly)
Returns the value of attribute error_description.
5 6 7 |
# File 'lib/flowy/failure.rb', line 5 def error_description @error_description end |
#error_title ⇒ Object (readonly)
Returns the value of attribute error_title.
5 6 7 |
# File 'lib/flowy/failure.rb', line 5 def error_title @error_title end |
#parent_failure ⇒ Object (readonly)
Returns the value of attribute parent_failure.
5 6 7 |
# File 'lib/flowy/failure.rb', line 5 def parent_failure @parent_failure end |
Instance Method Details
#and_then ⇒ Object
50 51 52 |
# File 'lib/flowy/failure.rb', line 50 def and_then self end |
#failure? ⇒ Boolean
37 38 39 |
# File 'lib/flowy/failure.rb', line 37 def failure? true end |
#failures_chain ⇒ Object
68 69 70 71 72 |
# File 'lib/flowy/failure.rb', line 68 def failures_chain return [self] unless parent_failure parent_failure.failures_chain + [self] end |
#is?(error_code:) ⇒ Boolean
25 26 27 |
# File 'lib/flowy/failure.rb', line 25 def is?(error_code:) self.error_code == error_code end |
#map_failure(error_code: nil, error_data: {}, error_title: nil, error_description: nil) ⇒ Object
In block form, when the block-returned Failure omits parent_failure, self is wired in as parent_failure so the chain is never broken.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/flowy/failure.rb', line 76 def map_failure(error_code: nil, error_data: {}, error_title: nil, error_description: nil) if block_given? result = yield self unless result.is_a?(Flowy::Failure) raise TypeError, "map_failure block must return a Flowy::Failure, got #{result.class}" end if result.parent_failure.nil? result.class.new( error_code: result.error_code, error_data: result.error_data, error_title: result.error_title, error_description: result.error_description, parent_failure: self ) else result end else raise ArgumentError, 'map_failure requires either a block or error_code:' if error_code.nil? self.class.new( error_code: error_code, error_data: error_data, error_title: error_title, error_description: error_description, parent_failure: self ) end end |
#merge_data(extra = nil) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/flowy/failure.rb', line 107 def merge_data(extra = nil) extra = block_given? ? yield(error_data) : extra raise ArgumentError, 'merge_data requires a Hash' unless extra.is_a?(Hash) self.class.new( error_code: error_code, error_data: Flowy::Result._deep_merge(error_data, extra), error_title: error_title, error_description: error_description, parent_failure: parent_failure ) end |
#on_failure {|_self| ... } ⇒ Object
45 46 47 48 |
# File 'lib/flowy/failure.rb', line 45 def on_failure yield self self end |
#on_success ⇒ Object
41 42 43 |
# File 'lib/flowy/failure.rb', line 41 def on_success self end |
#or_else ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/flowy/failure.rb', line 54 def or_else result = yield self unless result.is_a?(Flowy::Success) || result.is_a?(Flowy::Failure) raise TypeError, "or_else block must return a Flowy::Success or Flowy::Failure, got #{result.class}" end result end |
#raise! ⇒ Object
29 30 31 |
# File 'lib/flowy/failure.rb', line 29 def raise! raise Flowy::Error.initialize_from_failure(failure: self) end |
#success? ⇒ Boolean
33 34 35 |
# File 'lib/flowy/failure.rb', line 33 def success? false end |
#tap {|_self| ... } ⇒ Object
63 64 65 66 |
# File 'lib/flowy/failure.rb', line 63 def tap yield self self end |
#to_hash ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/flowy/failure.rb', line 15 def to_hash { success: false, error_code: error_code, error_data: error_data, error_title: error_title, error_description: error_description } end |