pretty_trace 0.2.5 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -10
- data/lib/pretty_trace/backtrace_item.rb +11 -10
- data/lib/pretty_trace/colors.rb +10 -3
- data/lib/pretty_trace/handler.rb +51 -51
- data/lib/pretty_trace/module_methods.rb +34 -24
- data/lib/pretty_trace/structured_backtrace.rb +28 -20
- data/lib/pretty_trace/version.rb +2 -2
- data/lib/pretty_trace.rb +1 -1
- metadata +9 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a8414a3aea1ebb47b1e194b2b9aefcbb8086064032148609d42b287b2f4eff6
|
4
|
+
data.tar.gz: 22649b6a4c529271f418c7212cfe735fa16f468c6a54a0e4057af6a88cf8c9d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba665c8242bcf0eb200f25e6413f45cbf49073ab4da2700cf808dd879305fb301b0db9b608c26ab8491d4e247d4398a9d8d706e9c64dfeeb55bef88423428bd9
|
7
|
+
data.tar.gz: fd8e854d010fc604ce3d21c67acaa0b81d6a665f0361f1d8b678776902933395767c639e207bb947537958fe6cdf40a1c92533590c18267ea5e66211b0bec4b6
|
data/README.md
CHANGED
@@ -1,11 +1,19 @@
|
|
1
|
+
<div align='center'>
|
2
|
+
|
1
3
|
# Pretty Trace - Pretty Errors and Backtrace
|
2
4
|
|
3
5
|
[![Gem Version](https://badge.fury.io/rb/pretty_trace.svg)](https://badge.fury.io/rb/pretty_trace)
|
4
6
|
[![Build Status](https://github.com/DannyBen/pretty_trace/workflows/Test/badge.svg)](https://github.com/DannyBen/pretty_trace/actions?query=workflow%3ATest)
|
5
7
|
[![Maintainability](https://api.codeclimate.com/v1/badges/c9db6ec58ec7ac1484aa/maintainability)](https://codeclimate.com/github/DannyBen/pretty_trace/maintainability)
|
6
8
|
|
9
|
+
![screenshot](support/screenshot/enabled.svg)
|
10
|
+
|
11
|
+
</div>
|
12
|
+
|
7
13
|
---
|
8
14
|
|
15
|
+
|
16
|
+
|
9
17
|
Make your Ruby backtrace pretty again. Just require `pretty_trace/enable`
|
10
18
|
in your ruby script, and errors will become clearer and more readable.
|
11
19
|
|
@@ -32,22 +40,16 @@ gem 'pretty_trace', require: 'pretty_trace/enable-trim'
|
|
32
40
|
```
|
33
41
|
|
34
42
|
|
35
|
-
##
|
36
|
-
|
37
|
-
### Create this ruby file:
|
43
|
+
## Quick Start
|
38
44
|
|
39
45
|
```ruby
|
40
46
|
# test.rb
|
41
47
|
require "pretty_trace/enable-trim"
|
42
48
|
require "fileutils"
|
49
|
+
|
43
50
|
FileUtils.rm 'no_such_file'
|
44
51
|
```
|
45
52
|
|
46
|
-
### Run it:
|
47
|
-
|
48
|
-
![screenshot](/screenshot.gif)
|
49
|
-
|
50
|
-
|
51
53
|
## Usage
|
52
54
|
|
53
55
|
The easiest way to use Pretty Trace is to require its activation script in
|
@@ -82,10 +84,12 @@ PrettyTrace.disable
|
|
82
84
|
|
83
85
|
PrettyTrace.enable
|
84
86
|
PrettyTrace.trim
|
85
|
-
|
87
|
+
PrettyTrace.reverse
|
88
|
+
# Exceptions here will be formatted, trimmed and in reverse order
|
86
89
|
|
87
90
|
PrettyTrace.no_trim
|
88
|
-
|
91
|
+
PrettyTrace.no_reverse
|
92
|
+
# Exceptions here will not be trimmed or reversed
|
89
93
|
```
|
90
94
|
|
91
95
|
|
@@ -6,17 +6,18 @@ module PrettyTrace
|
|
6
6
|
|
7
7
|
def initialize(original_line)
|
8
8
|
@original_line = original_line
|
9
|
-
@path, @file, @line, @dir, @full_dir = nil, nil, nil, nil, nil
|
10
9
|
@formatted = false
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
11
|
+
return unless @original_line =~ /(.+):(-?\d+):in `(.+)'/
|
12
|
+
|
13
|
+
@formatted = true
|
14
|
+
@path = $1
|
15
|
+
@line = $2
|
16
|
+
@method = $3
|
17
|
+
@full_dir = File.dirname(@path)
|
18
|
+
@dir = @full_dir.split('/').last
|
19
|
+
@dir = @dir == '.' ? '' : "#{dir}/"
|
20
|
+
@file = File.basename @path
|
20
21
|
end
|
21
22
|
|
22
23
|
def formatted?
|
@@ -28,7 +29,7 @@ module PrettyTrace
|
|
28
29
|
end
|
29
30
|
|
30
31
|
def colored_line
|
31
|
-
"line %{
|
32
|
+
"line %{green_bold}#{line.to_s.ljust 4}%{reset} in %{cyan}#{dir}%{magenta}#{file}%{reset} > %{blue}#{method}%{reset}" % colors
|
32
33
|
end
|
33
34
|
end
|
34
35
|
end
|
data/lib/pretty_trace/colors.rb
CHANGED
@@ -2,9 +2,16 @@ module PrettyTrace
|
|
2
2
|
module Colors
|
3
3
|
def colors
|
4
4
|
{
|
5
|
-
reset:
|
6
|
-
|
7
|
-
|
5
|
+
reset: "\e[0m",
|
6
|
+
bold: "\e[1m",
|
7
|
+
black: "\e[30m", black_bold: "\e[1;30m",
|
8
|
+
red: "\e[31m", red_bold: "\e[1;31m",
|
9
|
+
green: "\e[32m", green_bold: "\e[1;32m",
|
10
|
+
yellow: "\e[33m", yellow_bold: "\e[1;33m",
|
11
|
+
blue: "\e[34m", blue_bold: "\e[1;34m",
|
12
|
+
magenta: "\e[35m", magenta_bold: "\e[1;35m",
|
13
|
+
cyan: "\e[36m", cyan_bold: "\e[1;36m",
|
14
|
+
white: "\e[37m", white_bold: "\e[1;37m",
|
8
15
|
}
|
9
16
|
end
|
10
17
|
end
|
data/lib/pretty_trace/handler.rb
CHANGED
@@ -1,70 +1,70 @@
|
|
1
|
-
require 'singleton'
|
2
|
-
|
3
1
|
module PrettyTrace
|
4
2
|
class Handler
|
5
|
-
|
6
|
-
|
3
|
+
class << self
|
4
|
+
include Colors
|
5
|
+
|
6
|
+
attr_writer :options
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
8
|
+
def enable
|
9
|
+
@enabled = true
|
10
|
+
# :nocov: - this is actually covered through an external process
|
11
|
+
at_exit do
|
12
|
+
if @enabled && $! && !ignored.include?($!.class)
|
13
|
+
show_errors $!
|
14
|
+
$stderr.reopen IO::NULL
|
15
|
+
$stdout.reopen IO::NULL
|
16
|
+
end
|
16
17
|
end
|
18
|
+
# :nocov:
|
17
19
|
end
|
18
|
-
# :nocov:
|
19
|
-
end
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
def disable
|
22
|
+
@enabled = false
|
23
|
+
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
def enabled?
|
26
|
+
@enabled
|
27
|
+
end
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
def debug_tip?
|
30
|
+
!!(options[:debug_tip] and ENV['PRETTY_TRACE'] != 'full')
|
31
|
+
end
|
32
32
|
|
33
|
-
|
34
|
-
|
35
|
-
|
33
|
+
def options
|
34
|
+
@options ||= default_options
|
35
|
+
end
|
36
36
|
|
37
|
-
|
37
|
+
private
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
39
|
+
def ignored
|
40
|
+
# :nocov:
|
41
|
+
[SystemExit]
|
42
|
+
# :nocov:
|
43
|
+
end
|
44
44
|
|
45
|
-
|
46
|
-
|
47
|
-
|
45
|
+
def show_errors(exception)
|
46
|
+
# :nocov:
|
47
|
+
backtrace = StructuredBacktrace.new exception.backtrace, options
|
48
48
|
|
49
|
-
|
49
|
+
puts "\n#{backtrace}" unless backtrace.empty?
|
50
50
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
51
|
+
message = exception.message
|
52
|
+
puts "\n%{red}█ %{reset}%{bold}#{exception.class}%{reset}\n" % colors
|
53
|
+
unless message.empty?
|
54
|
+
puts "%{red}█ %{reset}%{yellow}#{message}%{reset}\n" % colors
|
55
|
+
end
|
57
56
|
|
58
|
-
|
59
|
-
|
60
|
-
|
57
|
+
if debug_tip?
|
58
|
+
puts "\nTip: Run with %{cyan}PRETTY_TRACE=full%{reset} (or %{cyan}off%{reset}) for debug information" % colors
|
59
|
+
end
|
61
60
|
|
62
|
-
|
63
|
-
|
64
|
-
|
61
|
+
$stdout.flush
|
62
|
+
# :nocov:
|
63
|
+
end
|
65
64
|
|
66
|
-
|
67
|
-
|
65
|
+
def default_options
|
66
|
+
{ filter: [] }
|
67
|
+
end
|
68
68
|
end
|
69
69
|
end
|
70
|
-
end
|
70
|
+
end
|
@@ -1,33 +1,43 @@
|
|
1
1
|
module PrettyTrace
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
class << self
|
3
|
+
def enable
|
4
|
+
Handler.enable unless ENV['PRETTY_TRACE'] == 'off'
|
5
|
+
end
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
def disable
|
8
|
+
Handler.disable
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
def filter(filter)
|
12
|
+
if filter.is_a? Array
|
13
|
+
Handler.options[:filter] += filter
|
14
|
+
else
|
15
|
+
Handler.options[:filter] << filter
|
16
|
+
end
|
15
17
|
end
|
16
|
-
end
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
19
|
+
def debug_tip
|
20
|
+
Handler.options[:debug_tip] = true
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
def no_debug_tip
|
24
|
+
Handler.options[:debug_tip] = false
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
27
|
+
def trim
|
28
|
+
Handler.options[:trim] = true
|
29
|
+
end
|
30
|
+
|
31
|
+
def no_trim
|
32
|
+
Handler.options[:trim] = false
|
33
|
+
end
|
34
|
+
|
35
|
+
def reverse
|
36
|
+
Handler.options[:reverse] = true
|
37
|
+
end
|
29
38
|
|
30
|
-
|
31
|
-
|
39
|
+
def no_reverse
|
40
|
+
Handler.options[:reverse] = false
|
41
|
+
end
|
32
42
|
end
|
33
|
-
end
|
43
|
+
end
|
@@ -2,33 +2,20 @@ module PrettyTrace
|
|
2
2
|
class StructuredBacktrace
|
3
3
|
attr_reader :options, :backtrace
|
4
4
|
|
5
|
-
def initialize(backtrace, options={})
|
5
|
+
def initialize(backtrace, options = {})
|
6
6
|
@options = options
|
7
7
|
@backtrace = backtrace
|
8
8
|
end
|
9
9
|
|
10
10
|
def structure
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
unless ENV['PRETTY_TRACE'] == 'full'
|
17
|
-
filter.each do |expression|
|
18
|
-
result.reject! { |trace| trace =~ expression }
|
19
|
-
end
|
11
|
+
result = backtrace_list.map { |line| BacktraceItem.new line }
|
12
|
+
result = if should_trim? result
|
13
|
+
result.group_by(&:path).flat_map { |_, items| [items.first, items.last].uniq }
|
14
|
+
else
|
15
|
+
result
|
20
16
|
end
|
21
17
|
|
22
|
-
|
23
|
-
first_line = result[0]
|
24
|
-
result.reverse!
|
25
|
-
result.uniq!(&:path) if should_trim? result
|
26
|
-
|
27
|
-
if first_line and first_line.original_line != result[-1].original_line
|
28
|
-
result.push first_line
|
29
|
-
end
|
30
|
-
|
31
|
-
result
|
18
|
+
should_reverse? ? result.reverse : result
|
32
19
|
end
|
33
20
|
|
34
21
|
def formatted_backtrace
|
@@ -45,8 +32,29 @@ module PrettyTrace
|
|
45
32
|
|
46
33
|
private
|
47
34
|
|
35
|
+
def backtrace_list
|
36
|
+
result = backtrace.dup
|
37
|
+
|
38
|
+
unless ENV['PRETTY_TRACE'] == 'full'
|
39
|
+
filters.each do |expression|
|
40
|
+
result.reject! { |trace| trace =~ expression }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
result.reverse
|
45
|
+
end
|
46
|
+
|
47
|
+
def filters
|
48
|
+
result = options[:filter] || []
|
49
|
+
result.is_a?(Array) ? result : [result]
|
50
|
+
end
|
51
|
+
|
48
52
|
def should_trim?(backtrace)
|
49
53
|
options[:trim] and ENV['PRETTY_TRACE'] != 'full' and backtrace.size > 3
|
50
54
|
end
|
55
|
+
|
56
|
+
def should_reverse?
|
57
|
+
options[:reverse]
|
58
|
+
end
|
51
59
|
end
|
52
60
|
end
|
data/lib/pretty_trace/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module PrettyTrace
|
2
|
-
VERSION =
|
3
|
-
end
|
2
|
+
VERSION = '0.3.0'
|
3
|
+
end
|
data/lib/pretty_trace.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pretty_trace
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-08-25 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Display clean and colorful error messages and backtrace
|
14
14
|
email: db@dannyben.com
|
@@ -29,8 +29,9 @@ files:
|
|
29
29
|
homepage: https://github.com/DannyBen/pretty_trace
|
30
30
|
licenses:
|
31
31
|
- MIT
|
32
|
-
metadata:
|
33
|
-
|
32
|
+
metadata:
|
33
|
+
rubygems_mfa_required: 'true'
|
34
|
+
post_install_message:
|
34
35
|
rdoc_options: []
|
35
36
|
require_paths:
|
36
37
|
- lib
|
@@ -38,15 +39,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
39
|
requirements:
|
39
40
|
- - ">="
|
40
41
|
- !ruby/object:Gem::Version
|
41
|
-
version:
|
42
|
+
version: '3.0'
|
42
43
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
44
|
requirements:
|
44
45
|
- - ">="
|
45
46
|
- !ruby/object:Gem::Version
|
46
47
|
version: '0'
|
47
48
|
requirements: []
|
48
|
-
rubygems_version: 3.
|
49
|
-
signing_key:
|
49
|
+
rubygems_version: 3.5.17
|
50
|
+
signing_key:
|
50
51
|
specification_version: 4
|
51
52
|
summary: Pretty backtrace and error messages
|
52
53
|
test_files: []
|