give_back_my_traces 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b3b90f146fc69d87b26e750c8488df198790455e7ef9df8ef3ebd47a5a0b9486
4
+ data.tar.gz: 119c08f92ef0953874ff6577ba11eb5967fff7980e8a208a4df0a2c39c7bd983
5
+ SHA512:
6
+ metadata.gz: d85098ced00e20dc0eae0c602d71b9a25be76d26bad5756011f676a5ecfa473c625f766864b6768b5ac29de6e1c1827f9d94e6f79c7c2f1eff02a097c236cd26
7
+ data.tar.gz: 1c721fb4231841608335e5291853d53920b2057f3af4aa7ed7e5a10f9db904e545cbfe0485c5401cba8a2d529261e27e6fbafb9db4f6662957d823dd858f322b
@@ -0,0 +1,17 @@
1
+ name: Ruby
2
+
3
+ on: [push, workflow_dispatch]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.7
14
+ bundler-cache: true
15
+ cache-version: 1
16
+ - name: Run the default task
17
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,23 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
3
+ Exclude:
4
+ - 'node_modules/**/*'
5
+ - 'tmp/**/*'
6
+ - 'vendor/**/*'
7
+ - '.git/**/*'
8
+ - 'examples/*.rb'
9
+
10
+ Style/StringLiterals:
11
+ Enabled: true
12
+ EnforcedStyle: double_quotes
13
+
14
+ Style/StringLiteralsInInterpolation:
15
+ Enabled: true
16
+ EnforcedStyle: double_quotes
17
+
18
+ Layout/LineLength:
19
+ Max: 120
20
+
21
+ Metrics/BlockLength:
22
+ Exclude:
23
+ - spec/**/*_spec.rb
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in give_back_my_traces.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.36"
13
+
14
+ gem "pry-byebug"
data/Gemfile.lock ADDED
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ give_back_my_traces (0.1.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ byebug (11.1.3)
11
+ coderay (1.1.3)
12
+ diff-lcs (1.4.4)
13
+ json (2.6.2)
14
+ method_source (1.0.0)
15
+ parallel (1.22.1)
16
+ parser (3.1.2.1)
17
+ ast (~> 2.4.1)
18
+ pry (0.14.1)
19
+ coderay (~> 1.1)
20
+ method_source (~> 1.0)
21
+ pry-byebug (3.8.0)
22
+ byebug (~> 11.0)
23
+ pry (~> 0.10)
24
+ rainbow (3.1.1)
25
+ rake (13.0.6)
26
+ regexp_parser (2.6.0)
27
+ rexml (3.2.5)
28
+ rspec (3.10.0)
29
+ rspec-core (~> 3.10.0)
30
+ rspec-expectations (~> 3.10.0)
31
+ rspec-mocks (~> 3.10.0)
32
+ rspec-core (3.10.1)
33
+ rspec-support (~> 3.10.0)
34
+ rspec-expectations (3.10.1)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.10.0)
37
+ rspec-mocks (3.10.2)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.10.0)
40
+ rspec-support (3.10.2)
41
+ rubocop (1.37.0)
42
+ json (~> 2.3)
43
+ parallel (~> 1.10)
44
+ parser (>= 3.1.2.1)
45
+ rainbow (>= 2.2.2, < 4.0)
46
+ regexp_parser (>= 1.8, < 3.0)
47
+ rexml (>= 3.2.5, < 4.0)
48
+ rubocop-ast (>= 1.22.0, < 2.0)
49
+ ruby-progressbar (~> 1.7)
50
+ unicode-display_width (>= 1.4.0, < 3.0)
51
+ rubocop-ast (1.22.0)
52
+ parser (>= 3.1.1.0)
53
+ ruby-progressbar (1.11.0)
54
+ unicode-display_width (2.3.0)
55
+
56
+ PLATFORMS
57
+ x86_64-linux
58
+
59
+ DEPENDENCIES
60
+ give_back_my_traces!
61
+ pry-byebug
62
+ rake (~> 13.0)
63
+ rspec (~> 3.0)
64
+ rubocop (~> 1.36)
65
+
66
+ BUNDLED WITH
67
+ 2.3.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Carlos Atkinson
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # GiveBackMyTraces
2
+
3
+ GiveBackMyTraces let you discover those sneaky errors in ruby apps.
4
+
5
+ ## Usage
6
+
7
+ Suppose you have the following code in your rails app:
8
+
9
+ ```ruby
10
+ class ApplicationController < ActionController::Base
11
+ rescue_from 'BaseError' do
12
+ head 422
13
+ end
14
+ end
15
+ ```
16
+
17
+ After a huge refactoring or upgrade you may face some errors like:
18
+
19
+ ```shell
20
+ ❯ rspec spec/requests/my_action_spec.rb
21
+ F
22
+
23
+ Failures:
24
+
25
+ 1) MyActions GET /index returns http success
26
+ Failure/Error: expect(response).to have_http_status(:success)
27
+ expected the response to have a success status code (2xx) but it was 422
28
+ # ./spec/requests/my_action_spec.rb:7:in `block (3 levels) in <top (required)>'
29
+
30
+ Finished in 0.07235 seconds (files took 0.80333 seconds to load)
31
+ 1 example, 1 failure
32
+
33
+ ....
34
+ ```
35
+
36
+ And then to discover the real error you may need to start debugging to find the real issue.
37
+
38
+ Enter `give_back_my_traces`, just add in your Gemfile, eg:
39
+
40
+ ```ruby
41
+ gem 'give_back_my_traces'
42
+ ```
43
+
44
+ And add `GBMT.init` in your spec_helper:
45
+
46
+ ```ruby
47
+ require 'give_back_my_traces'
48
+
49
+ RSpec.configure do |config|
50
+ config.around(:each) do |example|
51
+ GBMT.init
52
+
53
+ example.call
54
+
55
+ GBMT.clear
56
+ end
57
+ ```
58
+
59
+ And then you can see the errors running it with some GBMT envs:
60
+
61
+ ```
62
+ GBMT_ENABLE=1 rspec spec/requests/my_action_spec.rb 2>&1 | head -10
63
+ ----------------------------------------------------
64
+ Error: BaseError
65
+ Message: Test error
66
+ Backtrace:
67
+ /home/foo/projetos/test-gbmt/app/controllers/my_action_controller.rb:5:in `index'
68
+ /home/foo/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/actionpack-7.0.4/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
69
+ /home/foo/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/actionpack-7.0.4/lib/abstract_controller/base.rb:215:in `process_action'
70
+ /home/foo/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/actionpack-7.0.4/lib/action_controller/metal/rendering.rb:53:in `process_action'
71
+ /home/foo/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/actionpack-7.0.4/lib/abstract_controller/callbacks.rb:234:in `block in process_action'
72
+ ...
73
+
74
+ ```
75
+
76
+ See more in the examples folder.
77
+
78
+ ## Development
79
+
80
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
81
+
82
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
83
+
84
+ ## Contributing
85
+
86
+ Bug reports and pull requests are welcome on GitHub at https://github.com/catks/give_back_my_traces.
87
+
88
+ ## License
89
+
90
+ 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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "give_back_my_traces"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ require "pry"
12
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,19 @@
1
+ require 'give_back_my_traces'
2
+
3
+ # Usage:
4
+ # ruby simple.rb
5
+ # GBMT_MODE=normal GBMT_BACKTRACE_MAX_LINES=2 ruby multiple.rb
6
+ # GBMT_MODE=verbose GBMT_BACKTRACE_MAX_LINES=5 ruby multiple.rb
7
+ # GBMT_MODE=silent ruby simple.rb
8
+
9
+ GiveBackMyTraces.start
10
+
11
+ def bar(baz:)
12
+ end
13
+
14
+ def some_method
15
+ bar rescue StandardError
16
+ bar(baz: 42, invalid_key: 42) rescue StandardError
17
+ end
18
+
19
+ some_method
@@ -0,0 +1,20 @@
1
+ require 'give_back_my_traces'
2
+
3
+ # Usage:
4
+ # ruby simple.rb
5
+ # GBMT_MODE=normal GBMT_BACKTRACE_MAX_LINES=2 ruby simple.rb
6
+ # GBMT_MODE=verbose GBMT_BACKTRACE_MAX_LINES=5 ruby simple.rb
7
+ # GBMT_MODE=silent ruby simple.rb
8
+
9
+ GiveBackMyTraces.start
10
+
11
+ def bar(baz:)
12
+ end
13
+
14
+ def some_method
15
+ bar
16
+ rescue StandardError
17
+ # Nothing to see
18
+ end
19
+
20
+ some_method
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/give_back_my_traces/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "give_back_my_traces"
7
+ spec.version = GiveBackMyTraces::VERSION
8
+ spec.authors = ["Carlos Atkinson"]
9
+ spec.email = ["carlos.atks@gmail.com"]
10
+
11
+ spec.summary = "GiveBackMyTraces let's you get your traces back"
12
+ spec.homepage = "https://github.com/catks/give_back_my_traces"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.7.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/catks/give_back_my_traces"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ # Uncomment to register a new dependency of your gem
29
+ # spec.add_dependency "example-gem", "~> 1.0"
30
+
31
+ # For more information and examples about making a new gem, checkout our
32
+ # guide at: https://bundler.io/guides/creating_gem.html
33
+ end
data/lib/gbmt.rb ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ GBMT = GiveBackMyTraces
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+ require "give_back_my_traces/helpers"
5
+
6
+ module GiveBackMyTraces
7
+ # class to handle errors collected by GBMT
8
+ class ErrorsCollection
9
+ include Enumerable
10
+ include Comparable
11
+ extend Forwardable
12
+
13
+ delegate %i[each last <<] => :@errors
14
+
15
+ def initialize(errors = [])
16
+ @errors = errors
17
+ end
18
+
19
+ def pretty_print
20
+ GiveBackMyTraces::Helpers.pretty_print(@errors)
21
+ end
22
+
23
+ def <=>(other)
24
+ @errors <=> other
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GiveBackMyTraces
4
+ # Helper class with error formating methods
5
+ module Helpers
6
+ class << self
7
+ def pretty_print(errors, verbose: false)
8
+ errors.each do |error|
9
+ $stdout.puts pretty_format(error, verbose: verbose)
10
+ end
11
+ end
12
+
13
+ def pretty_format(error, verbose: false)
14
+ backtrace_max_lines = verbose ? 1000 : config[:backtrace][:max_lines]
15
+
16
+ <<~FORMAT
17
+ ----------------------------------------------------
18
+ Error: #{error.class}
19
+ Message: #{error.message}
20
+ Backtrace:
21
+ #{format_backtrace(error.backtrace, max_lines: backtrace_max_lines)}
22
+ ----------------------------------------------------
23
+ FORMAT
24
+ end
25
+
26
+ private
27
+
28
+ def format_backtrace(backtrace, max_lines:)
29
+ backtrace_lines = backtrace.first(max_lines).join("\n ")
30
+
31
+ return "#{backtrace_lines}\n ..." if backtrace.size > max_lines
32
+
33
+ backtrace_lines
34
+ end
35
+
36
+ def config
37
+ GiveBackMyTraces.config
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GiveBackMyTraces
4
+ VERSION = "0.1.1"
5
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "give_back_my_traces/version"
4
+ require_relative "give_back_my_traces/errors_collection"
5
+ require_relative "gbmt"
6
+
7
+ # Main module to handle GBMT api
8
+ #
9
+ # Usage:
10
+ #
11
+ # # With ENV variables to enable (aka: GBMT_ENABLE=1)
12
+ # GiveBackMyTraces.init(mode: :verbose)
13
+ # # # ...Some code...
14
+ # GiveBackMyTraces.errors # See all the errors rescued so far
15
+ #
16
+ # # Explicity enabling it
17
+ # GiveBackMyTraces.start(mode: :silent)
18
+ # # # ...Some code...
19
+ # GiveBackMyTraces.errors # See all the errors rescued so far
20
+ #
21
+ #
22
+ # GiveBackMyTraces.errors.pretty_print # Print errors
23
+ module GiveBackMyTraces
24
+ class Error < StandardError; end
25
+
26
+ DEFAULT_CONFIG = {
27
+ mode: ENV.fetch("GBMT_MODE", :normal).to_sym,
28
+ backtrace: {
29
+ max_lines: ENV.fetch("GBMT_BACKTRACE_MAX_LINES", 5).to_i
30
+ }
31
+ }.freeze
32
+
33
+ TRACKERS = {
34
+ normal: ->(error) { warn GiveBackMyTraces::Helpers.pretty_format(error) },
35
+ silent: ->(_) {},
36
+ verbose: ->(error) { warn GiveBackMyTraces::Helpers.pretty_format(error, verbose: true) }
37
+ }.freeze
38
+
39
+ class << self
40
+ def init(**options)
41
+ start(**options) if ENV.key?("GBMT_ENABLE")
42
+ end
43
+
44
+ def start(**options)
45
+ config.merge!(options)
46
+
47
+ @trace = TracePoint.new(:raise) do |tp|
48
+ error = tp.raised_exception
49
+
50
+ errors << error
51
+
52
+ GiveBackMyTraces::TRACKERS[config[:mode]].call(error)
53
+ end
54
+
55
+ @trace.enable
56
+
57
+ return unless block_given?
58
+
59
+ yield
60
+
61
+ stop
62
+ end
63
+
64
+ def stop
65
+ @trace&.disable
66
+ end
67
+
68
+ def clear
69
+ self.errors = GiveBackMyTraces::ErrorsCollection.new
70
+ end
71
+
72
+ def errors
73
+ @errors ||= GiveBackMyTraces::ErrorsCollection.new
74
+ end
75
+
76
+ def config
77
+ @config ||= DEFAULT_CONFIG.dup
78
+ end
79
+
80
+ private
81
+
82
+ attr_writer :errors
83
+ end
84
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: give_back_my_traces
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Carlos Atkinson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-10-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - carlos.atks@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".github/workflows/main.yml"
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".rubocop.yml"
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - examples/multiple.rb
32
+ - examples/simple.rb
33
+ - give_back_my_traces.gemspec
34
+ - lib/gbmt.rb
35
+ - lib/give_back_my_traces.rb
36
+ - lib/give_back_my_traces/errors_collection.rb
37
+ - lib/give_back_my_traces/helpers.rb
38
+ - lib/give_back_my_traces/version.rb
39
+ homepage: https://github.com/catks/give_back_my_traces
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ homepage_uri: https://github.com/catks/give_back_my_traces
44
+ source_code_uri: https://github.com/catks/give_back_my_traces
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.7.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.2.33
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: GiveBackMyTraces let's you get your traces back
64
+ test_files: []