lite-command 3.1.0 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 953b62c2ba61091b949e8fe3a673bc98be6a516afcea85192ea356e356aaf84e
4
- data.tar.gz: adfb9bf29183586b33f8b32b801afadfbcf890398827e002dd794692bb708c9e
3
+ metadata.gz: 6239fda55d099d36d3fc4c171ecf6eff2cd56bb5ffc4536dde420da339d0b5c9
4
+ data.tar.gz: a7c3949c9313a479f16f4cf709730f8262576f4d899069240073c52923c9aac3
5
5
  SHA512:
6
- metadata.gz: b618d0bda8cdbdc2335a16900e00addbdcf0a71e4ee6170c907d3b1e8110415990191d4bc9a9b5a0765582d2ce83df758d4b9109114fa33f04ec0ca27e388524
7
- data.tar.gz: 6a606bcd18ad5c97920e67557bc946f8e38446c83fdb2155a298530e338cf1f033e6388b411f7229919be6a70eb6c45510b8c1a1c95f9eeed0beabaa143550fa
6
+ metadata.gz: 6330c34c333948919685049c4eaefc5913ab364b09f28322ea6283aa73b3906715cc063b1b61192e231d5582206e0ae91be506fba09035317711dd56ad7f7ae2
7
+ data.tar.gz: 69a682501448fb31fd0ad5709fc23f5f8b5dabc3c9a01486c4848a1d01f887523ab3fee3e7c2acdf6861b181bc1347dbee9bdccd6af3f1dc21321a6585a0a0f4
data/CHANGELOG.md CHANGED
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [3.1.1] - 2024-10-25
10
+ ### Changed
11
+ - Add option to raise original or fault exception
12
+
9
13
  ## [3.1.0] - 2024-10-25
10
14
  ### Added
11
15
  - Added `raise!` method to reraise soft call errors
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lite-command (3.1.0)
4
+ lite-command (3.1.1)
5
5
  activemodel
6
6
  ostruct
7
7
 
data/README.md CHANGED
@@ -26,6 +26,7 @@ Or install it yourself as:
26
26
  * [Usage](#usage)
27
27
  * [Execution](#execution)
28
28
  * [Dynamic Faults](#dynamic-faults)
29
+ * [Raising Faults](#raising-faults)
29
30
  * [Context](#context)
30
31
  * [Attributes](#attributes)
31
32
  * [Validations](#validations)
@@ -88,21 +89,15 @@ end
88
89
 
89
90
  Executing a command can be done as an instance or class call. It returns the command instance
90
91
  in a frozen state. These will never call will never raise an execption, but will be kept track
91
- of in its internal state. You can raise the offending exception using `raise!`.
92
+ of in its internal state.
92
93
 
93
94
  ```ruby
94
- cmd = DecryptSecretMessage.call(...)
95
+ DecryptSecretMessage.call(...)
95
96
  # - or -
96
- cmd = DecryptSecretMessage.new(...).call
97
+ DecryptSecretMessage.new(...).call
97
98
 
98
99
  # On success, fault or exception:
99
- cmd #=> <DecryptSecretMessage ...>
100
-
101
- # On fault:
102
- cmd.raise! #=> raises Lite::Command::Fault
103
-
104
- # On exception:
105
- cmd.raise! #=> raises StandardError
100
+ #=> <DecryptSecretMessage ...>
106
101
  ```
107
102
 
108
103
  > [!TIP]
@@ -127,6 +122,29 @@ DecryptSecretMessage.new(...).call!
127
122
  #=> raises StandardError
128
123
  ```
129
124
 
125
+ ### Raising Faults
126
+
127
+ Sometimes its suitable to raise the offending soft call command fault later
128
+ in a call stack. Use the `raise!` method to reraise the fault or original
129
+ error (if they differ). `original: false` is the default.
130
+
131
+ ```ruby
132
+ cmd = DecryptSecretMessage.call(...)
133
+ Apm.track_stat("DecryptSecretMessage.called")
134
+ # other stuff...
135
+
136
+ # On success:
137
+ cmd.raise! #=> nil
138
+
139
+ # On fault:
140
+ cmd.raise!(original: false) #=> raises Lite::Command::Fault
141
+ cmd.raise!(original: true) #=> raises Lite::Command::Fault
142
+
143
+ # On exception:
144
+ cmd.raise!(original: false) #=> raises Lite::Command::Error
145
+ cmd.raise!(original: true) #=> raises StandardError
146
+ ```
147
+
130
148
  ### Dynamic Faults
131
149
 
132
150
  Dynamic faults are custom faults named after your command. This is especially
@@ -11,6 +11,20 @@ module Lite
11
11
  @object = object
12
12
  end
13
13
 
14
+ def reason
15
+ if object.respond_to?(:reason)
16
+ object.reason
17
+ elsif object.is_a?(StandardError)
18
+ "[#{object.class.name}] #{object.message}".chomp(".")
19
+ else
20
+ object
21
+ end
22
+ end
23
+
24
+ def metadata
25
+ Utils.try(object, :metadata) || command.metadata
26
+ end
27
+
14
28
  def caused_by
15
29
  Utils.try(object, :caused_by) || command
16
30
  end
@@ -21,18 +35,15 @@ module Lite
21
35
  Utils.try(object, :thrown_by) || command.caused_by
22
36
  end
23
37
 
24
- def metadata
25
- Utils.try(object, :metadata) || command.metadata
26
- end
38
+ def fault_exception
39
+ return if command.success?
27
40
 
28
- def reason
29
- if object.respond_to?(:reason)
30
- object.reason
31
- elsif object.is_a?(StandardError)
32
- "[#{object.class.name}] #{object.message}".chomp(".")
33
- else
34
- object
35
- end
41
+ Fault.build(
42
+ command.status.capitalize,
43
+ command,
44
+ object,
45
+ dynamic: command.send(:raise_dynamic_faults?)
46
+ )
36
47
  end
37
48
 
38
49
  end
@@ -84,13 +84,15 @@ module Lite
84
84
  @metadata ||= down_stream.metadata
85
85
  @caused_by ||= down_stream.caused_by
86
86
  @thrown_by ||= down_stream.thrown_by
87
+
88
+ @fault_exception ||= down_stream.fault_exception
87
89
  end
88
90
 
89
91
  FAULTS.each do |f|
90
92
  # eg: invalid!("idk") or failure!(fault) or error!("idk", { error_key: "some.error" })
91
93
  define_method(:"#{f}!") do |object, metadata = nil|
92
94
  fault(object, f, metadata)
93
- raise Lite::Command::Fault.build(f.capitalize, self, object, dynamic: raise_dynamic_faults?)
95
+ raise(fault_exception)
94
96
  end
95
97
  end
96
98
 
@@ -60,7 +60,7 @@ module Lite
60
60
  around_execution { call }
61
61
  Utils.try(self, :on_success)
62
62
  rescue StandardError => e
63
- @exception = e
63
+ @original_exception = e
64
64
  fault(e, ERROR, metadata) unless e.is_a?(Lite::Command::Fault)
65
65
  after_execution
66
66
  Utils.try(self, :"on_#{status}", e)
@@ -72,7 +72,7 @@ module Lite
72
72
  around_execution { call }
73
73
  Utils.try(self, :on_success)
74
74
  rescue StandardError => e
75
- @exception = e
75
+ @original_exception = e
76
76
  fault(e, ERROR, metadata) unless e.is_a?(Lite::Command::Fault)
77
77
  after_execution
78
78
  Utils.try(self, :"on_#{status}", e)
@@ -6,7 +6,7 @@ module Lite
6
6
  module Faults
7
7
 
8
8
  def self.included(base)
9
- base.class_eval { attr_reader :exception }
9
+ base.class_eval { attr_reader :fault_exception, :original_exception }
10
10
  end
11
11
 
12
12
  def caused_by
@@ -33,10 +33,9 @@ module Lite
33
33
  fault? && !caused_fault?
34
34
  end
35
35
 
36
- def raise!
37
- return if exception.nil?
38
-
39
- raise exception
36
+ def raise!(original: false)
37
+ exception = (fault_exception unless original) || original_exception
38
+ raise(exception) unless exception.nil?
40
39
  end
41
40
 
42
41
  private
@@ -3,7 +3,7 @@
3
3
  module Lite
4
4
  module Command
5
5
 
6
- VERSION = "3.1.0"
6
+ VERSION = "3.1.1"
7
7
 
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lite-command
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-25 00:00:00.000000000 Z
11
+ date: 2024-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel