dlnk_error_notifier 0.1.0

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: 2178d7bb0a7c5153f80e8d5dc8d442e00c5ab2d927afe686f043e1eff2014d74
4
+ data.tar.gz: 833f685e21a4f5c2c9fdf57d4603e95e9e1784f965e85fcc034da521f9dbedd1
5
+ SHA512:
6
+ metadata.gz: 88502ec1c3810b695a3cd13e6fe1504e08893cc7f2743466d186d82ce967cfb4c7dbdbbadbaf1a75dae5d47959b516c8607a96f555816d5d4a38eec99f8e0443
7
+ data.tar.gz: cf01907227f3466529aeefe905394156e77f3f65a4affab8d4f36a2c8d58a4329337e71a54f29f4a42a8e7500e866b09fa2241201f34f6408c4ca67a782c2c45
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 dalink
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,43 @@
1
+ # ErrorNotifier
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/error_notifier`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/error_notifier. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/error_notifier/blob/main/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the ErrorNotifier project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/error_notifier/blob/main/CODE_OF_CONDUCT.md).
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "error_notifier"
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
+ require "irb"
11
+ IRB.start(__FILE__)
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,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_job"
4
+ require "httparty"
5
+
6
+ module ErrorNotifier
7
+ # HttpPostJob is an ActiveJob that performs an HTTP POST request.
8
+ # This job is enqueued to run in the background, preventing the main thread
9
+ # from being blocked by the HTTP request.
10
+ #
11
+ # @example Enqueue the job
12
+ # ErrorNotifier::HttpPostJob.perform_later('https://your-webhook-url.com', { key: 'value' }.to_json)
13
+ #
14
+ # @param url [String] the URL to which the POST request is sent
15
+ # @param body [String] the body of the POST request, typically in JSON format
16
+ class HttpPostJob < ActiveJob::Base
17
+ queue_as :default
18
+
19
+ def perform(url, body)
20
+ HTTParty.post(
21
+ url,
22
+ body: body,
23
+ headers: { "Content-Type" => "application/json" }
24
+ )
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rack/request"
4
+ require "error_notifier/jobs/http_post_job"
5
+
6
+ module ErrorNotifier
7
+ # Middleware to catch exceptions and send them to a webhook
8
+ class Middleware
9
+ def initialize(app)
10
+ @app = app
11
+ end
12
+
13
+ def call(env)
14
+ @app.call(env)
15
+ rescue Exception => e
16
+ notify_error(env, e)
17
+ raise e
18
+ end
19
+
20
+ private
21
+
22
+ def notify_error(env, exception)
23
+ request = Rack::Request.new(env)
24
+ error_details = {
25
+ webhook_event: {
26
+ # name: "error",
27
+ error: exception.message,
28
+ application_trace: application_trace(exception),
29
+ method: env["REQUEST_METHOD"],
30
+ path: env["PATH_INFO"],
31
+ params: request.params
32
+ # headers: env.select { |k, _| k.start_with?("HTTP_") }
33
+ }
34
+ }
35
+
36
+ send_webhook(error_details)
37
+ end
38
+
39
+ def send_webhook(error_details)
40
+ ErrorNotifier::HttpPostJob.perform_later(
41
+ ENV["WEBHOOK_URL"] || "http://localhost:3000/webhook",
42
+ error_details.to_json
43
+ )
44
+ end
45
+
46
+ def application_trace(exception)
47
+ exception.backtrace.select { |line| line.include?(Rails.root.to_s) }
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ErrorNotifier
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "error_notifier/version"
4
+ require_relative "error_notifier/middleware"
5
+ require "error_notifier/jobs/http_post_job"
6
+
7
+ module ErrorNotifier
8
+ class Error < StandardError; end
9
+ # Your code goes here...
10
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dlnk_error_notifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - dalink
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-11-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.18'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.18'
27
+ description: Send a mail (coming soon) or a webhook when an error occurs in your Rails
28
+ application.
29
+ email:
30
+ - cyril@dalink.fr
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - LICENSE.txt
36
+ - README.md
37
+ - bin/console
38
+ - bin/setup
39
+ - lib/error_notifier.rb
40
+ - lib/error_notifier/jobs/http_post_job.rb
41
+ - lib/error_notifier/middleware.rb
42
+ - lib/error_notifier/version.rb
43
+ homepage: https://github.com/dalink/error_notifier
44
+ licenses:
45
+ - MIT
46
+ metadata:
47
+ allowed_push_host: https://rubygems.org
48
+ homepage_uri: https://github.com/dalink/error_notifier
49
+ source_code_uri: https://github.com/dalink/error_notifier
50
+ changelog_uri: https://github.com/dalink/error_notifier/changelog.md
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 3.0.0
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubygems_version: 3.5.23
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Simple error notifier for Rails applications.
70
+ test_files: []