rescue_from 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +451 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +8 -0
- data/LICENSE +21 -0
- data/README.md +141 -0
- data/Rakefile +9 -0
- data/lib/rescue_from/overrider.rb +17 -0
- data/lib/rescue_from/rescuable.rb +73 -0
- data/lib/rescue_from/version.rb +3 -0
- data/lib/rescue_from.rb +8 -0
- data/sig/rescue_from.rbs +4 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 84abd11a09f6dc0dda03750944d313ab3d55573d6736aeaed334bb861380d256
|
4
|
+
data.tar.gz: 73d8b1c733a292b4a17d52333c63c9b78518c920b6bac340fb98a22f5643e43d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3a4e678a97aad70d14b7893a2e942fe65c49a11f1cf540ca80e818f273f569776665ba9ef34a71f219e2626270e98120605bb9ab9424e1f1013ac8143c89ff36
|
7
|
+
data.tar.gz: 251097319a84dce69292693f9769c98729f49a57efa5e5ca8848abd6098878bedd8fb69cd260c68f4492a82a7019be73365e1a83bec9529d5f877382ab42e9c3
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,451 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- 'bin/**'
|
4
|
+
|
5
|
+
Gemspec/DeprecatedAttributeAssignment:
|
6
|
+
Enabled: true
|
7
|
+
|
8
|
+
Gemspec/DevelopmentDependencies:
|
9
|
+
Enabled: true
|
10
|
+
|
11
|
+
Gemspec/RequireMFA:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Layout/AccessModifierIndentation:
|
15
|
+
EnforcedStyle: outdent
|
16
|
+
|
17
|
+
Layout/BeginEndAlignment:
|
18
|
+
EnforcedStyleAlignWith: begin
|
19
|
+
|
20
|
+
Layout/BlockAlignment:
|
21
|
+
EnforcedStyleAlignWith: start_of_block
|
22
|
+
|
23
|
+
Layout/CommentIndentation:
|
24
|
+
AllowForAlignment: true
|
25
|
+
|
26
|
+
Layout/EmptyLineAfterMultilineCondition:
|
27
|
+
Enabled: true
|
28
|
+
|
29
|
+
Layout/EndOfLine:
|
30
|
+
EnforcedStyle: lf
|
31
|
+
|
32
|
+
Layout/ExtraSpacing:
|
33
|
+
AllowForAlignment: true
|
34
|
+
AllowBeforeTrailingComments: true
|
35
|
+
|
36
|
+
Layout/HashAlignment:
|
37
|
+
EnforcedHashRocketStyle: table
|
38
|
+
EnforcedColonStyle: table
|
39
|
+
EnforcedLastArgumentHashStyle: ignore_implicit
|
40
|
+
|
41
|
+
Layout/LineContinuationLeadingSpace:
|
42
|
+
Enabled: true
|
43
|
+
|
44
|
+
Layout/LineContinuationSpacing:
|
45
|
+
Enabled: true
|
46
|
+
|
47
|
+
Layout/LineEndStringConcatenationIndentation:
|
48
|
+
Enabled: true
|
49
|
+
|
50
|
+
Layout/MultilineArrayLineBreaks:
|
51
|
+
Enabled: true
|
52
|
+
|
53
|
+
Layout/MultilineAssignmentLayout:
|
54
|
+
Enabled: true
|
55
|
+
EnforcedStyle: same_line
|
56
|
+
|
57
|
+
Layout/MultilineHashKeyLineBreaks:
|
58
|
+
Enabled: true
|
59
|
+
|
60
|
+
Layout/MultilineMethodArgumentLineBreaks:
|
61
|
+
Enabled: true
|
62
|
+
|
63
|
+
Layout/MultilineMethodCallIndentation:
|
64
|
+
EnforcedStyle: indented_relative_to_receiver
|
65
|
+
|
66
|
+
Layout/SingleLineBlockChain:
|
67
|
+
Enabled: true
|
68
|
+
|
69
|
+
Layout/SpaceAroundEqualsInParameterDefault:
|
70
|
+
EnforcedStyle: no_space
|
71
|
+
|
72
|
+
Layout/SpaceAroundOperators:
|
73
|
+
EnforcedStyleForExponentOperator: space
|
74
|
+
|
75
|
+
Layout/SpaceBeforeBrackets:
|
76
|
+
Enabled: true
|
77
|
+
|
78
|
+
Layout/SpaceInsideHashLiteralBraces:
|
79
|
+
EnforcedStyle: no_space
|
80
|
+
|
81
|
+
Layout/TrailingWhitespace:
|
82
|
+
AllowInHeredoc: true
|
83
|
+
|
84
|
+
Lint/AmbiguousAssignment:
|
85
|
+
Enabled: true
|
86
|
+
|
87
|
+
Lint/AmbiguousOperatorPrecedence:
|
88
|
+
Enabled: true
|
89
|
+
|
90
|
+
Lint/AmbiguousRange:
|
91
|
+
Enabled: true
|
92
|
+
RequireParenthesesForMethodChains: true
|
93
|
+
|
94
|
+
Lint/AssignmentInCondition:
|
95
|
+
AllowSafeAssignment: false
|
96
|
+
|
97
|
+
Lint/ConstantOverwrittenInRescue:
|
98
|
+
Enabled: true
|
99
|
+
|
100
|
+
Lint/DeprecatedConstants:
|
101
|
+
Enabled: true
|
102
|
+
|
103
|
+
Lint/DuplicateBranch:
|
104
|
+
Enabled: true
|
105
|
+
IgnoreLiteralBranches: true
|
106
|
+
IgnoreConstantBranches: true
|
107
|
+
|
108
|
+
Lint/DuplicateMagicComment:
|
109
|
+
Enabled: true
|
110
|
+
|
111
|
+
Lint/DuplicateRegexpCharacterClassElement:
|
112
|
+
Enabled: true
|
113
|
+
|
114
|
+
Lint/EmptyBlock:
|
115
|
+
Enabled: true
|
116
|
+
|
117
|
+
Lint/EmptyClass:
|
118
|
+
Enabled: true
|
119
|
+
AllowComments: true
|
120
|
+
|
121
|
+
Lint/EmptyInPattern:
|
122
|
+
Enabled: true
|
123
|
+
|
124
|
+
Lint/HeredocMethodCallPosition:
|
125
|
+
Enabled: true
|
126
|
+
|
127
|
+
Lint/IncompatibleIoSelectWithFiberScheduler:
|
128
|
+
Enabled: false
|
129
|
+
|
130
|
+
Lint/LambdaWithoutLiteralBlock:
|
131
|
+
Enabled: true
|
132
|
+
|
133
|
+
Lint/NoReturnInBeginEndBlocks:
|
134
|
+
Enabled: true
|
135
|
+
|
136
|
+
Lint/NonAtomicFileOperation:
|
137
|
+
Enabled: true
|
138
|
+
|
139
|
+
Lint/NumberedParameterAssignment:
|
140
|
+
Enabled: true
|
141
|
+
|
142
|
+
Lint/OrAssignmentToConstant:
|
143
|
+
Enabled: true
|
144
|
+
|
145
|
+
Lint/RedundantDirGlobSort:
|
146
|
+
Enabled: true
|
147
|
+
|
148
|
+
Lint/RedundantSplatExpansion:
|
149
|
+
AllowPercentLiteralArrayArgument: false
|
150
|
+
|
151
|
+
Lint/RefinementImportMethods:
|
152
|
+
Enabled: true
|
153
|
+
|
154
|
+
Lint/RequireRangeParentheses:
|
155
|
+
Enabled: true
|
156
|
+
|
157
|
+
Lint/RequireRelativeSelfPath:
|
158
|
+
Enabled: true
|
159
|
+
|
160
|
+
Lint/SymbolConversion:
|
161
|
+
Enabled: true
|
162
|
+
|
163
|
+
Lint/ToEnumArguments:
|
164
|
+
Enabled: true
|
165
|
+
|
166
|
+
Lint/TripleQuotes:
|
167
|
+
Enabled: true
|
168
|
+
|
169
|
+
Lint/UnexpectedBlockArity:
|
170
|
+
Enabled: true
|
171
|
+
|
172
|
+
Lint/UnmodifiedReduceAccumulator:
|
173
|
+
Enabled: true
|
174
|
+
|
175
|
+
Lint/UnusedBlockArgument:
|
176
|
+
AutoCorrect: false
|
177
|
+
|
178
|
+
Lint/UnusedMethodArgument:
|
179
|
+
AutoCorrect: false
|
180
|
+
|
181
|
+
Lint/UselessRescue:
|
182
|
+
Enabled: true
|
183
|
+
|
184
|
+
Lint/UselessRuby2Keywords:
|
185
|
+
Enabled: true
|
186
|
+
|
187
|
+
Metrics:
|
188
|
+
Enabled: false
|
189
|
+
|
190
|
+
Naming/BlockForwarding:
|
191
|
+
Enabled: true
|
192
|
+
|
193
|
+
Naming/InclusiveLanguage:
|
194
|
+
Enabled: false
|
195
|
+
|
196
|
+
Security/CompoundHash:
|
197
|
+
Enabled: true
|
198
|
+
|
199
|
+
Security/Eval:
|
200
|
+
Enabled: false
|
201
|
+
|
202
|
+
Security/IoMethods:
|
203
|
+
Enabled: true
|
204
|
+
|
205
|
+
Style/AccessorGrouping:
|
206
|
+
EnforcedStyle: separated
|
207
|
+
|
208
|
+
Style/ArgumentsForwarding:
|
209
|
+
Enabled: false
|
210
|
+
|
211
|
+
Style/ArrayIntersect:
|
212
|
+
Enabled: true
|
213
|
+
|
214
|
+
Style/AutoResourceCleanup:
|
215
|
+
Enabled: true
|
216
|
+
|
217
|
+
Style/CollectionCompact:
|
218
|
+
Enabled: true
|
219
|
+
|
220
|
+
Style/CollectionMethods:
|
221
|
+
Enabled: true
|
222
|
+
|
223
|
+
Style/ComparableClamp:
|
224
|
+
Enabled: true
|
225
|
+
|
226
|
+
Style/ConcatArrayLiterals:
|
227
|
+
Enabled: true
|
228
|
+
|
229
|
+
Style/DirEmpty:
|
230
|
+
Enabled: true
|
231
|
+
|
232
|
+
Style/DocumentDynamicEvalDefinition:
|
233
|
+
Enabled: false
|
234
|
+
|
235
|
+
Style/Documentation:
|
236
|
+
Enabled: false
|
237
|
+
|
238
|
+
Style/DocumentationMethod:
|
239
|
+
Enabled: false
|
240
|
+
|
241
|
+
Style/DoubleNegation:
|
242
|
+
EnforcedStyle: forbidden
|
243
|
+
|
244
|
+
Style/EmptyHeredoc:
|
245
|
+
Enabled: true
|
246
|
+
|
247
|
+
Style/EmptyMethod:
|
248
|
+
EnforcedStyle: expanded
|
249
|
+
|
250
|
+
Style/EndlessMethod:
|
251
|
+
Enabled: true
|
252
|
+
EnforcedStyle: disallow
|
253
|
+
|
254
|
+
Style/EnvHome:
|
255
|
+
Enabled: true
|
256
|
+
|
257
|
+
Style/FetchEnvVar:
|
258
|
+
Enabled: false
|
259
|
+
|
260
|
+
Style/FileEmpty:
|
261
|
+
Enabled: true
|
262
|
+
|
263
|
+
Style/FileRead:
|
264
|
+
Enabled: true
|
265
|
+
|
266
|
+
Style/FileWrite:
|
267
|
+
Enabled: true
|
268
|
+
|
269
|
+
Style/FormatString:
|
270
|
+
EnforcedStyle: percent
|
271
|
+
|
272
|
+
Style/FrozenStringLiteralComment:
|
273
|
+
Enabled: false
|
274
|
+
|
275
|
+
Style/HashConversion:
|
276
|
+
Enabled: true
|
277
|
+
|
278
|
+
Style/HashExcept:
|
279
|
+
Enabled: true
|
280
|
+
|
281
|
+
Style/HashSyntax:
|
282
|
+
EnforcedShorthandSyntax: never
|
283
|
+
|
284
|
+
Style/IfWithBooleanLiteralBranches:
|
285
|
+
Enabled: true
|
286
|
+
|
287
|
+
Style/ImplicitRuntimeError:
|
288
|
+
Enabled: true
|
289
|
+
|
290
|
+
Style/InPatternThen:
|
291
|
+
Enabled: true
|
292
|
+
|
293
|
+
Style/IpAddresses:
|
294
|
+
Enabled: true
|
295
|
+
|
296
|
+
Style/MagicCommentFormat:
|
297
|
+
Enabled: true
|
298
|
+
|
299
|
+
Style/MapCompactWithConditionalBlock:
|
300
|
+
Enabled: true
|
301
|
+
|
302
|
+
Style/MapToHash:
|
303
|
+
Enabled: true
|
304
|
+
|
305
|
+
Style/MapToSet:
|
306
|
+
Enabled: true
|
307
|
+
|
308
|
+
Style/MethodCallWithArgsParentheses:
|
309
|
+
Enabled: true
|
310
|
+
EnforcedStyle: omit_parentheses
|
311
|
+
AllowParenthesesInMultilineCall: true
|
312
|
+
AllowParenthesesInChaining: true
|
313
|
+
AllowParenthesesInCamelCaseMethod: true
|
314
|
+
|
315
|
+
Style/MethodDefParentheses:
|
316
|
+
EnforcedStyle: require_no_parentheses_except_multiline
|
317
|
+
|
318
|
+
Style/MinMaxComparison:
|
319
|
+
Enabled: true
|
320
|
+
|
321
|
+
Style/MultilineBlockChain:
|
322
|
+
Enabled: false
|
323
|
+
|
324
|
+
Style/MultilineInPatternThen:
|
325
|
+
Enabled: true
|
326
|
+
|
327
|
+
Style/NegatedIfElseCondition:
|
328
|
+
Enabled: true
|
329
|
+
|
330
|
+
Style/NestedFileDirname:
|
331
|
+
Enabled: true
|
332
|
+
|
333
|
+
Style/NestedParenthesizedCalls:
|
334
|
+
Enabled: false
|
335
|
+
|
336
|
+
Style/NilLambda:
|
337
|
+
Enabled: true
|
338
|
+
|
339
|
+
Style/NonNilCheck:
|
340
|
+
Enabled: false
|
341
|
+
|
342
|
+
Style/NumberedParameters:
|
343
|
+
Enabled: true
|
344
|
+
EnforcedStyle: disallow
|
345
|
+
|
346
|
+
Style/NumberedParametersLimit:
|
347
|
+
Enabled: true
|
348
|
+
|
349
|
+
Style/ObjectThen:
|
350
|
+
Enabled: true
|
351
|
+
|
352
|
+
Style/OpenStructUse:
|
353
|
+
Enabled: false
|
354
|
+
|
355
|
+
Style/OperatorMethodCall:
|
356
|
+
Enabled: true
|
357
|
+
|
358
|
+
Style/OptionHash:
|
359
|
+
Enabled: true
|
360
|
+
|
361
|
+
Style/QuotedSymbols:
|
362
|
+
Enabled: true
|
363
|
+
|
364
|
+
Style/RedundantArgument:
|
365
|
+
Enabled: false
|
366
|
+
|
367
|
+
Style/RedundantConstantBase:
|
368
|
+
Enabled: false
|
369
|
+
|
370
|
+
Style/RedundantDoubleSplatHashBraces:
|
371
|
+
Enabled: true
|
372
|
+
|
373
|
+
Style/RedundantEach:
|
374
|
+
Enabled: true
|
375
|
+
|
376
|
+
Style/RedundantException:
|
377
|
+
Enabled: false
|
378
|
+
|
379
|
+
Style/RedundantHeredocDelimiterQuotes:
|
380
|
+
Enabled: true
|
381
|
+
|
382
|
+
Style/RedundantInitialize:
|
383
|
+
Enabled: true
|
384
|
+
|
385
|
+
Style/RedundantParentheses:
|
386
|
+
Enabled: false
|
387
|
+
|
388
|
+
Style/RedundantSelfAssignmentBranch:
|
389
|
+
Enabled: true
|
390
|
+
|
391
|
+
Style/RedundantStringEscape:
|
392
|
+
Enabled: true
|
393
|
+
|
394
|
+
Style/RegexpLiteral:
|
395
|
+
EnforcedStyle: percent_r
|
396
|
+
|
397
|
+
Style/ReturnNil:
|
398
|
+
Enabled: true
|
399
|
+
|
400
|
+
Style/SelectByRegexp:
|
401
|
+
Enabled: true
|
402
|
+
|
403
|
+
Style/SingleLineMethods:
|
404
|
+
AllowIfMethodIsEmpty: false
|
405
|
+
|
406
|
+
Style/StaticClass:
|
407
|
+
Enabled: true
|
408
|
+
|
409
|
+
Style/StringChars:
|
410
|
+
Enabled: true
|
411
|
+
|
412
|
+
Style/StringHashKeys:
|
413
|
+
Enabled: false
|
414
|
+
|
415
|
+
Style/SwapValues:
|
416
|
+
Enabled: true
|
417
|
+
|
418
|
+
Style/SymbolArray:
|
419
|
+
EnforcedStyle: brackets
|
420
|
+
|
421
|
+
Style/TernaryParentheses:
|
422
|
+
EnforcedStyle: require_parentheses_when_complex
|
423
|
+
AllowSafeAssignment: false
|
424
|
+
|
425
|
+
Style/TopLevelMethodDefinition:
|
426
|
+
Enabled: true
|
427
|
+
|
428
|
+
Style/TrailingCommaInArguments:
|
429
|
+
Enabled: true
|
430
|
+
EnforcedStyleForMultiline: no_comma
|
431
|
+
|
432
|
+
Style/TrailingCommaInArrayLiteral:
|
433
|
+
Enabled: true
|
434
|
+
EnforcedStyleForMultiline: no_comma
|
435
|
+
|
436
|
+
Style/TrailingCommaInBlockArgs:
|
437
|
+
Enabled: true
|
438
|
+
|
439
|
+
Style/TrailingCommaInHashLiteral:
|
440
|
+
Enabled: true
|
441
|
+
EnforcedStyleForMultiline: no_comma
|
442
|
+
|
443
|
+
Style/UnlessLogicalOperators:
|
444
|
+
EnforcedStyle: forbid_logical_operators
|
445
|
+
|
446
|
+
Style/WordArray:
|
447
|
+
EnforcedStyle: brackets
|
448
|
+
|
449
|
+
Style/YodaCondition:
|
450
|
+
Enabled: true
|
451
|
+
EnforcedStyle: forbid_for_all_comparison_operators
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 Moku S.r.l., Riccardo Agatea
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
# Rescue From
|
2
|
+
|
3
|
+
An imitation of `ActiveSupport::Rescuable` that relies on Ruby's callbacks to reduce boilerplate code.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rescue_from', '~> 1.0'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or you can install the gem on its own:
|
20
|
+
|
21
|
+
```bash
|
22
|
+
gem install rescue_from
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Simply extend with `RescueFrom::Rescuable` any class or module that you want to use `rescue_from` in.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class DataImporter
|
31
|
+
extend RescueFrom::Rescuable
|
32
|
+
|
33
|
+
rescue_from JSON::ParserError do
|
34
|
+
[]
|
35
|
+
end
|
36
|
+
|
37
|
+
def temperature_data
|
38
|
+
JSON.parse File.read('temperatures.json')
|
39
|
+
end
|
40
|
+
|
41
|
+
def humidity_data
|
42
|
+
JSON.parse File.read('humidity.json')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
Both `#temperature_data` and `#humidity_data` will return an empty array if the respective files contain JSON syntax errors.
|
48
|
+
|
49
|
+
`rescue_from` can accept multiple patterns, and will associate the same handler with all of them. Handlers are looked up top to bottom, left to right, and up the ancestors chain as it is returned by `self.class.ancestors`. The lookup uses case equality to match patterns against the raised exception, so you can use less straightforward patterns instead of classes. This class will silence all exceptions the messages of which don't start with "failure":
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
class Service
|
53
|
+
extend RescueFrom::Rescuable
|
54
|
+
|
55
|
+
rescue_from(proc {|e| !e.message.start_with? 'failure'}) do
|
56
|
+
nil
|
57
|
+
end
|
58
|
+
|
59
|
+
...
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
If no pattern matches the exception, it will be reraised.
|
64
|
+
|
65
|
+
If you want to call a given method bypassing the automatic exception handling, you can append `_without_rescue` to its name:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
di = DataImporter.new
|
69
|
+
di.temperature_data # => []
|
70
|
+
di.temperature_data_without_rescue # => JSON::ParserError
|
71
|
+
```
|
72
|
+
|
73
|
+
Sometimes, instead of silencing the exception, you want to change its class, for example if you're using the same low level library in different parts of the application and you need to distinguish the exceptions coming from the different sides. In such cases, you can use `relabel`:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class Service
|
77
|
+
extend RescueFrom::Rescuable
|
78
|
+
|
79
|
+
relabel pattern1, pattern2, ..., to: ExceptionClass do |e|
|
80
|
+
code
|
81
|
+
end
|
82
|
+
|
83
|
+
...
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
is equivalent to
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
class Service
|
91
|
+
extend RescueFrom::Rescuable
|
92
|
+
|
93
|
+
rescue_from pattern1, pattern2, ... do |e|
|
94
|
+
message = code
|
95
|
+
raise ExceptionClass, message
|
96
|
+
end
|
97
|
+
|
98
|
+
...
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
If you want to limit automatic exception handling to only certain methods, you can override `should_rescue_in?`. It will be called every time a new method is defined, receiving its name as single argument; if it returns `false` or `nil`, the method is left untouched.
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
|
106
|
+
class Service
|
107
|
+
extend RescueFrom::Rescuable
|
108
|
+
|
109
|
+
ACTIONS = [:create, :update, :delete]
|
110
|
+
|
111
|
+
def self.should_rescue_in? method_name
|
112
|
+
ACTIONS.include? method_name
|
113
|
+
end
|
114
|
+
|
115
|
+
...
|
116
|
+
end
|
117
|
+
```
|
118
|
+
|
119
|
+
## Version numbers
|
120
|
+
|
121
|
+
Rescue From loosely follows [Semantic Versioning](https://semver.org/), with a hard guarantee that breaking changes to the public API will always coincide with an increase to the `MAJOR` number.
|
122
|
+
|
123
|
+
Version numbers are in three parts: `MAJOR.MINOR.PATCH`.
|
124
|
+
|
125
|
+
- Breaking changes to the public API increment the `MAJOR`. There may also be changes that would otherwise increase the `MINOR` or the `PATCH`.
|
126
|
+
- Additions, deprecations, and "big" non breaking changes to the public API increment the `MINOR`. There may also be changes that would otherwise increase the `PATCH`.
|
127
|
+
- Bug fixes and "small" non breaking changes to the public API increment the `PATCH`.
|
128
|
+
|
129
|
+
Notice that any feature deprecated by a minor release can be expected to be removed by the next major release.
|
130
|
+
|
131
|
+
## Changelog
|
132
|
+
|
133
|
+
Full list of changes in [CHANGELOG.md](CHANGELOG.md)
|
134
|
+
|
135
|
+
## Contributing
|
136
|
+
|
137
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/moku-io/rescue_from.
|
138
|
+
|
139
|
+
## License
|
140
|
+
|
141
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
2
|
+
|
3
|
+
module RescueFrom
|
4
|
+
module Overrider
|
5
|
+
def handlers
|
6
|
+
@handlers ||= {}
|
7
|
+
end
|
8
|
+
|
9
|
+
# rubocop:disable Style/CaseEquality
|
10
|
+
def handler_for exception
|
11
|
+
handlers
|
12
|
+
.find { |pattern, _handler| pattern === exception }
|
13
|
+
&.last
|
14
|
+
end
|
15
|
+
# rubocop:enable Style/CaseEquality
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require_relative 'overrider'
|
2
|
+
|
3
|
+
module RescueFrom
|
4
|
+
module Rescuable
|
5
|
+
def self.extended base
|
6
|
+
overrider = Module.new do
|
7
|
+
extend Overrider
|
8
|
+
end
|
9
|
+
|
10
|
+
base.const_set :RescueFrom_Overrider, overrider
|
11
|
+
base.prepend overrider
|
12
|
+
end
|
13
|
+
|
14
|
+
def inherited subclass
|
15
|
+
super
|
16
|
+
|
17
|
+
puts 'inside the inherited'
|
18
|
+
|
19
|
+
subclass.extend Rescuable
|
20
|
+
end
|
21
|
+
|
22
|
+
def rescue_from *patterns, &handler
|
23
|
+
raise ArgumentError, 'always provide a handler when calling rescue_from' unless block_given?
|
24
|
+
|
25
|
+
overrider = const_get :RescueFrom_Overrider
|
26
|
+
|
27
|
+
patterns.each do |pattern|
|
28
|
+
overrider.handlers[pattern] = handler
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def relabel *patterns, to:, &message_generator
|
33
|
+
rescue_from(*patterns) do |e|
|
34
|
+
raise to, message_generator.call(e)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def should_rescue_in? _method_name
|
39
|
+
true
|
40
|
+
end
|
41
|
+
|
42
|
+
def method_added name
|
43
|
+
super
|
44
|
+
|
45
|
+
return unless should_rescue_in? name
|
46
|
+
|
47
|
+
overrider = const_get :RescueFrom_Overrider
|
48
|
+
|
49
|
+
return if overrider.method_defined? name
|
50
|
+
|
51
|
+
method_without_rescue = instance_method name
|
52
|
+
|
53
|
+
overrider.define_method :"#{name}_without_rescue" do |*args, **kwargs, &block|
|
54
|
+
method_without_rescue.bind_call(self, *args, **kwargs, &block)
|
55
|
+
end
|
56
|
+
|
57
|
+
# rubocop:disable Lint/RescueException
|
58
|
+
overrider.define_method name do |*args, **kwargs, &block|
|
59
|
+
super(*args, **kwargs, &block)
|
60
|
+
rescue Exception => e
|
61
|
+
self.class
|
62
|
+
.ancestors
|
63
|
+
.lazy
|
64
|
+
.filter_map { |mod| mod.const_get :RescueFrom_Overrider if mod.const_defined? :RescueFrom_Overrider }
|
65
|
+
.filter_map { |ancestor_overrider| ancestor_overrider.handler_for e }
|
66
|
+
.first
|
67
|
+
.otherwise { raise e }
|
68
|
+
.therefore { |handler| handler.call e }
|
69
|
+
end
|
70
|
+
# rubocop:enable Lint/RescueException
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/lib/rescue_from.rb
ADDED
data/sig/rescue_from.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rescue_from
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Moku S.r.l.
|
8
|
+
- Riccardo Agatea
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2023-05-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 7.0.0
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 7.0.0
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: therefore
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '1.0'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '1.0'
|
42
|
+
description: An imitation of ActiveSupport::Rescuable that relies on Ruby's callbacks
|
43
|
+
to reduce boilerplate code.
|
44
|
+
email:
|
45
|
+
- info@moku.io
|
46
|
+
executables: []
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- ".rspec"
|
51
|
+
- ".rubocop.yml"
|
52
|
+
- CHANGELOG.md
|
53
|
+
- Gemfile
|
54
|
+
- LICENSE
|
55
|
+
- README.md
|
56
|
+
- Rakefile
|
57
|
+
- lib/rescue_from.rb
|
58
|
+
- lib/rescue_from/overrider.rb
|
59
|
+
- lib/rescue_from/rescuable.rb
|
60
|
+
- lib/rescue_from/version.rb
|
61
|
+
- sig/rescue_from.rbs
|
62
|
+
homepage: https://github.com/moku-io/rescue_from
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata:
|
66
|
+
homepage_uri: https://github.com/moku-io/rescue_from
|
67
|
+
source_code_uri: https://github.com/moku-io/rescue_from
|
68
|
+
changelog_uri: https://github.com/moku-io/rescue_from/blob/master/CHANGELOG.md
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 2.6.0
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubygems_version: 3.4.6
|
85
|
+
signing_key:
|
86
|
+
specification_version: 4
|
87
|
+
summary: An imitation of ActiveSupport::Rescuable that relies on Ruby's callbacks
|
88
|
+
to reduce boilerplate code.
|
89
|
+
test_files: []
|