justified 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +40 -0
  2. data/README_FULL.md +125 -0
  3. data/VERSION +1 -0
  4. metadata +7 -1
@@ -0,0 +1,40 @@
1
+ # Justified
2
+
3
+ [![Build Status](https://travis-ci.org/pitr-ch/justified.png?branch=master)](https://travis-ci.org/pitr-ch/justified)
4
+
5
+ A mini-gem to add missing **causes** to exception `backtrace`-s like Java has. This gem will add following
6
+ at the bottom of a `backtrace`:
7
+
8
+ from caused by: (AnError) an ugly bug
9
+ from justified.rb:83:in `bad_code'
10
+ from ... skipped 4 lines
11
+
12
+ Exception cause can be also accessed with `#cause` method which returns `nil` or an `Exception`.
13
+
14
+ ## Example
15
+
16
+ ```ruby
17
+ require 'justified/standard_error'
18
+
19
+ begin
20
+ raise 'a cause'
21
+ rescue
22
+ raise 'an exception'
23
+ end
24
+ ```
25
+
26
+ will print
27
+
28
+ ```
29
+ file.rb:6:in `rescue in <main>': an exception (RuntimeError)
30
+ from file.rb:3:in `<main>'
31
+ from caused by: (RuntimeError) a cause
32
+ from file.rb:4:in `<main>'
33
+ from ... skipped 0 lines
34
+ ```
35
+
36
+ ## Links
37
+
38
+ - Documentation: <http://blog.pitr.ch/justified>
39
+ - Source: <https://github.com/pitr-ch/justified>
40
+ - Blog: <http://blog.pitr.ch/blog/categories/justified/>
@@ -0,0 +1,125 @@
1
+ # Justified
2
+
3
+ A mini-gem to add missing **causes** to exception `backtrace`-s like Java has.
4
+
5
+ from caused by: (AnError) an ugly bug
6
+ from justified.rb:83:in `bad_code'
7
+ from ... skipped 4 lines
8
+
9
+ - Documentation: <http://blog.pitr.ch/justified>
10
+ - Source: <https://github.com/pitr-ch/justified>
11
+ - Blog: <http://blog.pitr.ch/blog/categories/justified/>
12
+
13
+
14
+ ## Example
15
+
16
+ Let's have following snippet:
17
+
18
+ ```ruby
19
+ def bad_code
20
+ raise AnError, 'an ugly bug'
21
+ end
22
+
23
+ def handle_error(error)
24
+ raise AnError, 'something went wrong'
25
+ end
26
+
27
+ def do_something
28
+ bad_code
29
+ rescue => error
30
+ handle_error error
31
+ end
32
+
33
+ do_something
34
+ ```
35
+
36
+ When called it will produce:
37
+
38
+ justified.rb:93:in `handle_error': something went wrong (AnError)
39
+ from justified.rb:89:in `rescue in do_something'
40
+ from justified.rb:87:in `do_something'
41
+ from justified.rb:96:in `<top (required)>'
42
+ from -e:1:in `load'
43
+ from -e:1:in `<main>'
44
+
45
+ The real problem `an ugly bug` is **hidden**. What will happen when `justified` is used?
46
+
47
+ ```ruby
48
+ require 'justified/stadard_error'
49
+ class AnError < StadardError; end
50
+
51
+ # ... rest of the snipper
52
+ ```
53
+
54
+ It will produce:
55
+
56
+ justified.rb:93:in `handle_error': something went wrong (AnError)
57
+ from justified.rb:89:in `rescue in do_something'
58
+ from justified.rb:87:in `do_something'
59
+ from justified.rb:96:in `<top (required)>'
60
+ from -e:1:in `load'
61
+ from -e:1:in `<main>'
62
+ from caused by: (AnError) an ugly bug
63
+ from justified.rb:83:in `bad_code'
64
+ from ... skipped 4 lines
65
+
66
+ Of course causes can be concatenated.
67
+
68
+ ## Usage
69
+
70
+ * `require 'justified'` to include `Justified::Error` to any exception you need manually
71
+ * `require 'justified/standard_error'` to have causes in all exceptions which are kind of `StandardError`
72
+
73
+ ### Behavior
74
+
75
+ When an exception is risen inside rescue block a cause is automatically recorded.
76
+
77
+ ```ruby
78
+ e = begin
79
+ raise 'bug'
80
+ rescue => error
81
+ raise 'this does not work'
82
+ end rescue $!
83
+ e.cause.message == 'bug' # => true
84
+ ```
85
+
86
+ Cause can be set explicitly.
87
+
88
+ ```ruby
89
+ e = begin
90
+ raise 'bug'
91
+ rescue => error
92
+ raise StandardError.new('this does not work', error)
93
+ end rescue $!
94
+ e.cause.message == 'bug' # => true
95
+ ```
96
+
97
+ Or if signature of `.new` is changed cause can be set with a setter `#cause=`
98
+
99
+ ```ruby
100
+ class InspectingError < StandardError
101
+ def initialize(object)
102
+ super object.inspect
103
+ end
104
+ end
105
+
106
+ e = begin
107
+ raise 'bug'
108
+ rescue => error
109
+ raise InspectingError.new(a: 'b').tap { |e| e.cause = error }
110
+ end rescue $!
111
+ e.cause.message == 'bug' # => true
112
+ ```
113
+
114
+ and
115
+
116
+ ```ruby
117
+ e = begin
118
+ raise 'bug'
119
+ rescue => error
120
+ raise InspectingError.new(a: 'b')
121
+ end rescue $!
122
+ e.cause.message == 'bug' # => true
123
+ ```
124
+
125
+ will work as well.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.4
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: justified
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -115,10 +115,16 @@ executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files:
117
117
  - MIT-LICENSE
118
+ - VERSION
119
+ - README.md
120
+ - README_FULL.md
118
121
  files:
119
122
  - lib/justified/standard_error.rb
120
123
  - lib/justified.rb
121
124
  - MIT-LICENSE
125
+ - VERSION
126
+ - README.md
127
+ - README_FULL.md
122
128
  - test/justified_test.rb
123
129
  homepage: https://github.com/pitr-ch/justified
124
130
  licenses: []