humanist-errors 1.0.4 → 1.0.5
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 +4 -4
- data/README.md +31 -46
- data/Rakefile +14 -3
- data/dictionaries/humanist_errors.en.yml +4 -1
- data/dictionaries/humanist_errors.oops.yml +2 -2
- data/lib/humanist_errors.rb +3 -4
- data/lib/humanist_errors/color.rb +20 -0
- data/lib/humanist_errors/error_mapper.rb +6 -0
- data/lib/humanist_errors/exception_extensions.rb +3 -3
- data/lib/humanist_errors/regex_hash.rb +4 -5
- data/lib/humanist_errors/search.rb +3 -1
- data/lib/humanist_errors/version.rb +1 -1
- data/test/backlog/load_error_test.rb +1 -0
- data/test/color_test.rb +28 -0
- data/test/error_mapper/load_error_test.rb +9 -0
- data/test/error_mapper/syntax_error_test.rb +1 -1
- data/test/features/syntax_error_test.rb +14 -2
- data/test/monkey_test.rb +0 -1
- data/test/search_test.rb +3 -2
- metadata +8 -4
- data/lib/humanist_errors/colors.rb +0 -37
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bbf346aeb9290ca662aa37a4c5f0815fe30a95c6
|
4
|
+
data.tar.gz: deb0fbba11bfeb7ea38fd644129b8b0a6400699e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb0f5cf17010946944916589e897f5d70cc99b82f28b4e2634b90141e3813c70f4376c3766c229bc30dc088e2c987d9642ba99adb3c067ef8aaedb32ac6addab
|
7
|
+
data.tar.gz: fc528cada09c47f49084adff41fd5c47d8baa20158fac93d079e0d6ac159a4b3e22319c7e7048fc4eb45ca33244176a4a088245fdf73ab7d54ef39e8f3ba2de3
|
data/README.md
CHANGED
@@ -4,8 +4,14 @@
|
|
4
4
|
|
5
5
|
[](https://gitter.im/bgreg/humanist-errors?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
6
6
|
|
7
|
-
Humanist errors is a system for extending ruby exception messages.
|
8
|
-
|
7
|
+
Humanist errors is a system for extending ruby exception messages. It will prepend the standard output with text
|
8
|
+
defined in the humanist errors dictionary that hopefully explains what is going on a little better.
|
9
|
+
|
10
|
+
Some examples:
|
11
|
+
|
12
|
+

|
13
|
+

|
14
|
+
|
9
15
|
|
10
16
|
## Installation
|
11
17
|
|
@@ -25,64 +31,43 @@ Or install it yourself as:
|
|
25
31
|
|
26
32
|
## Usage
|
27
33
|
|
34
|
+
You can use humanist errors globally in your project, or only around specific pieces of code.
|
35
|
+
To use globally:
|
36
|
+
|
28
37
|
Require the library and monkey file
|
29
38
|
|
30
|
-
require humanist_errors
|
31
|
-
require humanist_errors/monkey
|
39
|
+
require 'humanist_errors'
|
40
|
+
require 'humanist_errors/monkey'
|
41
|
+
|
42
|
+
Thats it! Now when ruby tries to evaluate an error like when you say: `1/0` you will see a new error message.
|
43
|
+
|
44
|
+
|
45
|
+
If you only want to see new errors in an isolated area you can include the library and execute
|
46
|
+
your code inside a special code block.
|
47
|
+
|
48
|
+
1. Add these require statements to your file:
|
32
49
|
|
33
|
-
|
50
|
+
`require 'humanist_errors'`
|
34
51
|
|
35
|
-
|
52
|
+
`require 'humanist_errors/monkey'`
|
36
53
|
|
37
|
-
|
54
|
+
2. Include the humanist errors module:
|
38
55
|
|
56
|
+
`include HumanistErrors`
|
57
|
+
|
58
|
+
3. Then your code in a human block:
|
39
59
|
```ruby
|
40
|
-
with_human_errors do
|
41
|
-
|
42
|
-
end
|
60
|
+
with_human_errors do
|
61
|
+
#....
|
62
|
+
end
|
43
63
|
```
|
44
64
|
|
45
65
|
This gem is intended for use in development and test environments only.
|
46
66
|
|
47
67
|
## Contributing
|
48
68
|
|
49
|
-
1. Fork it ( https://github.com/
|
69
|
+
1. Fork it ( https://github.com/bgreg/humanist-errors/fork )
|
50
70
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
51
71
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
52
72
|
4. Push to the branch (`git push origin my-new-feature`)
|
53
73
|
5. Create a new Pull Request
|
54
|
-
|
55
|
-
|
56
|
-
## TODO
|
57
|
-
1) add more errors to error_map
|
58
|
-
|
59
|
-
##### High level exception tree:
|
60
|
-
|
61
|
-
```ruby
|
62
|
-
Exception
|
63
|
-
NoMethodError
|
64
|
-
ScriptError
|
65
|
-
LoadError
|
66
|
-
NotImplementedError
|
67
|
-
SyntaxError
|
68
|
-
SignalException
|
69
|
-
Interrupt
|
70
|
-
StandardError
|
71
|
-
ArgumentError
|
72
|
-
IOError
|
73
|
-
EOFError
|
74
|
-
IndexError
|
75
|
-
LocalJumpError
|
76
|
-
NameError
|
77
|
-
NoMethodError
|
78
|
-
RangeError
|
79
|
-
FloatDomainError
|
80
|
-
RuntimeError
|
81
|
-
SecurityError
|
82
|
-
SystemCallError
|
83
|
-
SystemStackError
|
84
|
-
ThreadError
|
85
|
-
ZeroDivisionError
|
86
|
-
SystemExit
|
87
|
-
fatal
|
88
|
-
```
|
data/Rakefile
CHANGED
@@ -9,17 +9,28 @@ $LOAD_PATH.unshift(test) unless $LOAD_PATH.include?(test)
|
|
9
9
|
task default: :test
|
10
10
|
|
11
11
|
task :test do
|
12
|
-
Dir.glob([
|
12
|
+
Dir.glob([
|
13
|
+
'./test/*_test.rb',
|
14
|
+
'./test/error_mapper/*_test.rb',
|
15
|
+
'./test/features/*_test.rb'
|
16
|
+
]).each do |file|
|
17
|
+
|
18
|
+
require file
|
19
|
+
end
|
13
20
|
end
|
14
21
|
|
15
22
|
task :test_error_mapper do
|
16
|
-
Dir.glob('./test/error_mapper/*_test.rb').each { |file| require file}
|
23
|
+
Dir.glob('./test/error_mapper/*_test.rb').each { |file| require file }
|
17
24
|
end
|
18
25
|
|
19
26
|
task :test_features do
|
20
|
-
Dir.glob('./test/features/*_test.rb').each { |file| require file}
|
27
|
+
Dir.glob('./test/features/*_test.rb').each { |file| require file }
|
21
28
|
end
|
22
29
|
|
23
30
|
task :search_test do
|
24
31
|
require './test/search_test.rb'
|
25
32
|
end
|
33
|
+
|
34
|
+
task :regex_test do
|
35
|
+
require './test/regex_hash_test.rb'
|
36
|
+
end
|
@@ -4,13 +4,16 @@
|
|
4
4
|
:string_formatter: "% string formatter requires a string argument on the left, and a format argument on the right"
|
5
5
|
:missing_argument: "You indicated that you wanted to send another argument, but we did not see anything."
|
6
6
|
:missing_block_closer: "A block was started, but you forgot to add 'end'. Ruby tries to match every `do` with an `end`, but it reached the end of the file and a matching 'end' token was not found. To fix this, review all the 'do..end' pairs."
|
7
|
+
:t_identifier: "Ok, wow, I'm not sure what is going on here"
|
7
8
|
|
8
9
|
:no_method_error:
|
9
10
|
:undefined_method_for_nil: "You defined a variable with good intentions, then along the way, something turned it to nil."
|
10
11
|
|
11
12
|
:name_error:
|
12
|
-
:undefined_word: "You typed a random string that wasn't defined. It is not a method or variable in this class, or
|
13
|
+
:undefined_word: "You typed a random string that wasn't defined. It is not a method or variable in this class, or its ancestors"
|
13
14
|
|
14
15
|
:zero_division_error:
|
15
16
|
:divide_by_zero: "Science has not figured out how to divide by zero yet. Damn you science!!!"
|
16
17
|
|
18
|
+
:load_error:
|
19
|
+
:no_file: "You tried to load a file that doesn't exist - at least, not where you think it is!"
|
data/lib/humanist_errors.rb
CHANGED
@@ -3,12 +3,11 @@ require 'humanist_errors/version'
|
|
3
3
|
require 'humanist_errors/exception_extensions'
|
4
4
|
require 'humanist_errors/regex_hash'
|
5
5
|
require 'humanist_errors/search'
|
6
|
-
require 'humanist_errors/
|
6
|
+
require 'humanist_errors/color'
|
7
7
|
|
8
8
|
module HumanistErrors
|
9
|
-
|
10
|
-
|
11
|
-
ENDING_TOKEN = color.green("Here's the error from Ruby: \n")
|
9
|
+
STARTING_TOKEN = "Hi!\n"
|
10
|
+
ENDING_TOKEN = "Here's the error from Ruby: \n"
|
12
11
|
MESSAGE_DICTIONARY = YAML.load(File.read(File.expand_path('../../dictionaries/humanist_errors.en.yml', __FILE__)))
|
13
12
|
|
14
13
|
def with_human_errors
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module HumanistErrors
|
2
|
+
class Color
|
3
|
+
PREFIX = '\033'
|
4
|
+
POSTFIX = '\033[0m'
|
5
|
+
COLORS = {
|
6
|
+
black: '[30m',
|
7
|
+
red: '[31m',
|
8
|
+
green: '[32m',
|
9
|
+
brown: '[33m',
|
10
|
+
blue: '[34m',
|
11
|
+
magenta: '[35m',
|
12
|
+
cyan: '[36m',
|
13
|
+
gray: '[37m',
|
14
|
+
}
|
15
|
+
|
16
|
+
def colorize_with(color, text)
|
17
|
+
"#{PREFIX}#{COLORS[color]}#{text}#{POSTFIX}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -6,6 +6,7 @@ module HumanistErrors
|
|
6
6
|
/unterminated quoted string meets end of file/ => MESSAGE_DICTIONARY[:syntax_error][:string_formatter],
|
7
7
|
/syntax error, unexpected end-of-input, expecting keyword_end/ => MESSAGE_DICTIONARY[:syntax_error][:missing_block_closer],
|
8
8
|
/syntax error, unexpected end-of-input/ => MESSAGE_DICTIONARY[:syntax_error][:missing_argument],
|
9
|
+
/syntax error, unexpected tIDENTIFIER/ => MESSAGE_DICTIONARY[:syntax_error][:t_identifier],
|
9
10
|
/.*/ => :no_result
|
10
11
|
],
|
11
12
|
no_method_error: HumanistErrors::RegexHash[
|
@@ -19,6 +20,11 @@ module HumanistErrors
|
|
19
20
|
zero_division_error: HumanistErrors::RegexHash[
|
20
21
|
/divided by 0/ => MESSAGE_DICTIONARY[:zero_division_error][:divide_by_zero],
|
21
22
|
/.*/ => :no_result
|
23
|
+
],
|
24
|
+
|
25
|
+
load_error: HumanistErrors::RegexHash[
|
26
|
+
/no such file to load/ => MESSAGE_DICTIONARY[:load_error][:no_file],
|
27
|
+
/.*/ => :no_result
|
22
28
|
]
|
23
29
|
}
|
24
30
|
end
|
@@ -6,9 +6,9 @@ module HumanistErrors
|
|
6
6
|
super
|
7
7
|
else
|
8
8
|
color = Color.new
|
9
|
-
"\n\n#{STARTING_TOKEN}" \
|
10
|
-
" #{color.cyan
|
11
|
-
"#{ENDING_TOKEN}\n" + super
|
9
|
+
"\n\n#{color.colorize_with(:green, STARTING_TOKEN)}" \
|
10
|
+
" #{color.colorize_with(:cyan, error)}\n" \
|
11
|
+
"#{color.colorize_with(:green, ENDING_TOKEN)}\n" + super
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
@@ -1,11 +1,10 @@
|
|
1
1
|
module HumanistErrors
|
2
2
|
class RegexHash < Hash
|
3
3
|
def [](value)
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
result.shift[1]
|
4
|
+
detect(-> { :no_result }) do |k,v|
|
5
|
+
if k =~ value
|
6
|
+
return v
|
7
|
+
end
|
9
8
|
end
|
10
9
|
end
|
11
10
|
end
|
@@ -13,15 +13,17 @@ module HumanistErrors
|
|
13
13
|
|
14
14
|
def find
|
15
15
|
error = keyify(error_object)
|
16
|
-
return :no_result unless ERROR_MAPPER
|
16
|
+
return :no_result unless ERROR_MAPPER[error]
|
17
17
|
ERROR_MAPPER[error][ruby_error_message]
|
18
18
|
end
|
19
19
|
|
20
20
|
private
|
21
|
+
|
21
22
|
attr_reader :error_object, :ruby_error_message, :found_error
|
22
23
|
|
23
24
|
# turn a CamelCase word into an underscored
|
24
25
|
# symbol to make it nice for hash keys.
|
26
|
+
# Basically the same as ActiveSupport#underscore
|
25
27
|
def keyify(error)
|
26
28
|
error.to_s
|
27
29
|
.gsub(/::/, '__')
|
data/test/color_test.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ColorTest < Minitest::Test
|
4
|
+
def colors
|
5
|
+
{ black: '[30m',
|
6
|
+
red: '[31m',
|
7
|
+
green: '[32m',
|
8
|
+
brown: '[33m',
|
9
|
+
blue: '[34m',
|
10
|
+
magenta: '[35m',
|
11
|
+
cyan: '[36m',
|
12
|
+
gray: '[37m', }
|
13
|
+
end
|
14
|
+
|
15
|
+
def colorizer
|
16
|
+
@colorizer ||= HumanistErrors::Color.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_wraps_given_string_with_color
|
20
|
+
test_string = "Test"
|
21
|
+
|
22
|
+
colors.each do |color_name, code|
|
23
|
+
assert_equal(
|
24
|
+
colorizer.colorize_with(color_name, test_string),
|
25
|
+
"\\033#{code}#{test_string}\\033[0m")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -8,7 +8,7 @@ class SyntaxErrorTest < Minitest::Test
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def test_message_for_unterminated_string_meets_end_of_file
|
11
|
-
|
11
|
+
assert_error_map("unterminated string meets end of file", :syntax_error, :open_quote)
|
12
12
|
end
|
13
13
|
|
14
14
|
def test_message_for_single_percent_sign
|
@@ -7,15 +7,27 @@ class SyntaxErrorTest < Minitest::Test
|
|
7
7
|
assert_match(expected_message, error_message)
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
10
|
+
def test_message_for_missing_end
|
11
11
|
error_message = human_fork(SyntaxError) do
|
12
12
|
eval(
|
13
13
|
" def stuff
|
14
|
-
|
14
|
+
oops = 'I did it again'
|
15
15
|
"
|
16
16
|
)
|
17
17
|
end
|
18
18
|
expected_message = HumanistErrors::MESSAGE_DICTIONARY[:syntax_error][:missing_block_closer]
|
19
19
|
assert_match(expected_message, error_message)
|
20
20
|
end
|
21
|
+
|
22
|
+
def test_message_for_unexpected_identifier
|
23
|
+
error_message = human_fork(SyntaxError) do
|
24
|
+
eval %q(
|
25
|
+
one = 1
|
26
|
+
two = 2
|
27
|
+
product = one * two _2
|
28
|
+
)
|
29
|
+
end
|
30
|
+
expected_message = HumanistErrors::MESSAGE_DICTIONARY[:syntax_error][:t_identifier]
|
31
|
+
assert_match(expected_message, error_message)
|
32
|
+
end
|
21
33
|
end
|
data/test/monkey_test.rb
CHANGED
data/test/search_test.rb
CHANGED
@@ -10,9 +10,10 @@ class SearchTest < Minitest::Test
|
|
10
10
|
assert(error == expected_result, "#{error.inspect} does not equal: #{expected_result.inspect}")
|
11
11
|
end
|
12
12
|
|
13
|
-
class
|
13
|
+
class DefinitelyNotARealException; end
|
14
|
+
|
14
15
|
def test_cannot_find_a_error_message
|
15
|
-
error = HumanistErrors::Search.run(
|
16
|
+
error = HumanistErrors::Search.run(DefinitelyNotARealException, "blah blah blah")
|
16
17
|
assert(error == :no_result, "#{error.inspect} does not equal: :no_result")
|
17
18
|
end
|
18
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: humanist-errors
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Greg McGuirk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-07-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -57,7 +57,7 @@ files:
|
|
57
57
|
- dictionaries/humanist_errors.oops.yml
|
58
58
|
- humanist-errors.gemspec
|
59
59
|
- lib/humanist_errors.rb
|
60
|
-
- lib/humanist_errors/
|
60
|
+
- lib/humanist_errors/color.rb
|
61
61
|
- lib/humanist_errors/error_mapper.rb
|
62
62
|
- lib/humanist_errors/exception_extensions.rb
|
63
63
|
- lib/humanist_errors/monkey.rb
|
@@ -88,6 +88,8 @@ files:
|
|
88
88
|
- test/backlog/system_stack_error_test.rb
|
89
89
|
- test/backlog/thread_error_test.rb
|
90
90
|
- test/backlog/zero_division_error_test.rb
|
91
|
+
- test/color_test.rb
|
92
|
+
- test/error_mapper/load_error_test.rb
|
91
93
|
- test/error_mapper/name_error_test.rb
|
92
94
|
- test/error_mapper/no_method_error_test.rb
|
93
95
|
- test/error_mapper/syntax_error_test.rb
|
@@ -117,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
119
|
version: '0'
|
118
120
|
requirements: []
|
119
121
|
rubyforge_project:
|
120
|
-
rubygems_version: 2.
|
122
|
+
rubygems_version: 2.5.1
|
121
123
|
signing_key:
|
122
124
|
specification_version: 4
|
123
125
|
summary: More readable and friendly errors, for humans.
|
@@ -146,6 +148,8 @@ test_files:
|
|
146
148
|
- test/backlog/system_stack_error_test.rb
|
147
149
|
- test/backlog/thread_error_test.rb
|
148
150
|
- test/backlog/zero_division_error_test.rb
|
151
|
+
- test/color_test.rb
|
152
|
+
- test/error_mapper/load_error_test.rb
|
149
153
|
- test/error_mapper/name_error_test.rb
|
150
154
|
- test/error_mapper/no_method_error_test.rb
|
151
155
|
- test/error_mapper/syntax_error_test.rb
|
@@ -1,37 +0,0 @@
|
|
1
|
-
module HumanistErrors
|
2
|
-
class Color
|
3
|
-
|
4
|
-
def black(string)
|
5
|
-
"\033[30m#{string}\033[0m"
|
6
|
-
end
|
7
|
-
|
8
|
-
def red(string)
|
9
|
-
"\033[31m#{string}\033[0m"
|
10
|
-
end
|
11
|
-
|
12
|
-
def green(string)
|
13
|
-
"\033[32m#{string}\033[0m"
|
14
|
-
end
|
15
|
-
|
16
|
-
def brown(string)
|
17
|
-
"\033[33m#{string}\033[0m"
|
18
|
-
end
|
19
|
-
|
20
|
-
def blue(string)
|
21
|
-
"\033[34m#{string}\033[0m"
|
22
|
-
end
|
23
|
-
|
24
|
-
def magenta(string)
|
25
|
-
"\033[35m#{string}\033[0m"
|
26
|
-
end
|
27
|
-
|
28
|
-
def cyan(string)
|
29
|
-
"\033[36m#{string}\033[0m"
|
30
|
-
end
|
31
|
-
|
32
|
-
def gray(string)
|
33
|
-
"\033[37m#{string}\033[0m"
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
37
|
-
end
|