email_error_reporter 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e8f1858125082a54222230c31cc9f86977225bb52e4d58250415c6b0cb7c47d
4
- data.tar.gz: 2c6a173d38e6d7b58cfea1291dfd5aa2dce6dd5b31e5907fe72772c96055ab8f
3
+ metadata.gz: b83248a211bd2ca5e29e1ea4b7f9cb2abd840ce07043da60b983d5d66b85a426
4
+ data.tar.gz: 346f2ff677450545ab25b6b4ab054d48d020482369ee66deabd1169a5f30d06e
5
5
  SHA512:
6
- metadata.gz: 928f4ed5561244626c6968949f531931ac2d24daa8d85912346d6c80ea4b06789a678af0f03767c4170fe8d0021392a09ab60dafa1a5d4a50827e41893460376
7
- data.tar.gz: e4c5f4044e603434cd92504f37c386af900cbb1351abc6b7270a34268aed6d4a2d73a05b138f583b37ff9229583bd6d2c899c0fa8bf8321a989b833eadee5c74
6
+ metadata.gz: 8e4b317060de34be9cdd274c2d1820fcd807dc4e693138d9f831f87b58f08e8d1e457e21e235a39b1cce5684f50fcf385ab9c7702441ae3cd0fe0ea0b255a99c
7
+ data.tar.gz: 0320b65b635fea9be9589873bcd2744a946e3b015c39e5b6b70167d5545d04f3a4edf7e60d1408cb70b49bfd962c0362af803628fd0f438413ea1de070782e85
data/README.md CHANGED
@@ -1,28 +1,60 @@
1
1
  # EmailErrorReporter
2
- Short description and motivation.
3
2
 
4
- ## Usage
5
- How to use my plugin.
3
+ > [!CAUTION]
4
+ > This is a very early prototype
5
+
6
+ `email_error_reporter` uses the new [Rails error reporting API](https://guides.rubyonrails.org/error_reporting.html#error-reporting) to send emails whenever an exception is being reported. It works out of the box with HTTP requests, jobs and the rails runner.
6
7
 
7
8
  ## Installation
8
- Add this line to your application's Gemfile:
9
+
10
+ Add the gem:
11
+
12
+ ```bash
13
+ bundle add email_error_reporter
14
+ ```
15
+
16
+ and configure the email addresses that should receive an email in the case of an exception:
9
17
 
10
18
  ```ruby
11
- gem "email_error_reporter"
19
+ # application.rb
20
+ config.email_error_reporter.to = ["youremail@example.com"]
12
21
  ```
13
22
 
14
- And then execute:
15
- ```bash
16
- $ bundle
23
+ `email_error_reporter` will reuse your environment specific ActionMailer configuration for delivering emails.
24
+
25
+ ## Usage
26
+
27
+ All exceptions are reported automatically. No additional code required.
28
+
29
+ Please consult the [official guides](https://guides.rubyonrails.org/error_reporting.html) for an introduction to the error reporting API.
30
+
31
+ ## Optional configuration
32
+
33
+ Set a custom from address.
34
+
35
+ ```ruby
36
+ # default: "no-reply@example.com"
37
+ config.email_error_reporter.from = "your-custom-email@example.com"
17
38
  ```
18
39
 
19
- Or install it yourself as:
20
- ```bash
21
- $ gem install email_error_reporter
40
+ Disables the email reports for specific environments. Mails are enabled by default on all envs.
41
+
42
+ ```ruby
43
+ # e.g. in development.rb
44
+ # default: true
45
+ config.email_error_reporter.enabled = false
22
46
  ```
23
47
 
24
- ## Contributing
25
- Contribution directions go here.
48
+ ## Test your setup
49
+
50
+ You can use the built-in rake task `rake email_error_reporter:check` to check if everything works correctly in your setup.
51
+
52
+ If you're using Kamal, you can run the following command to test the setup in production:
53
+
54
+ ```
55
+ kamal app exec -p 'bundle exec rake email_error_reporter:test_email'
56
+ ```
26
57
 
27
58
  ## License
59
+
28
60
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,3 @@
1
+ <tr>
2
+ <td style="font-family: monospace; padding: 2px;"><%= frame %></td>
3
+ </tr>
@@ -0,0 +1 @@
1
+ <tr><td>No backtrace available</td></tr>
@@ -0,0 +1,22 @@
1
+ <html>
2
+ <body style="max-width: 768px; margin: 0 auto;">
3
+ <h1 style="margin-top: 12px; font-family: sans-serif; font-size: 19px;"><%= @error.class %></h1>
4
+ <h2 style="font-family: sans-serif; font-size: 15px;"><%= @error.message %></h2>
5
+
6
+ <p data-test-id="source">Source: <%= @source ? @source : "No source present" %></p>
7
+ <p data-test-id="handled">Handled: <%= @handled ? "✅" : "❌" %></p>
8
+
9
+ <details style="margin-bottom: 12px;">
10
+ <summary>Stacktrace</summary>
11
+
12
+ <table data-test-id="backtrace" border="1" width="100%" style="margin-top: 12px; margin-bottom: 12px; border-collapse: collapse; border-color: #000000">
13
+ <%= render(partial: "frame", collection: @backtrace) || render("no_backtrace") -%>
14
+ </table>
15
+ </details>
16
+
17
+ <details>
18
+ <summary>Context</summary>
19
+ <pre data-test-id="context" style="font-family: mono; background: #eeeeee;"><code><%= @context %></code></pre>
20
+ </details>
21
+ </body>
22
+ </html>
@@ -0,0 +1,21 @@
1
+ module EmailErrorReporter
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace EmailErrorReporter
4
+
5
+ config.email_error_reporter = ActiveSupport::OrderedOptions.new
6
+ config.email_error_reporter.to = []
7
+ config.email_error_reporter.from = "no-reply@example.com"
8
+ config.email_error_reporter.enabled = true
9
+
10
+ initializer "email_error_reporter.error_subscribe" do |app|
11
+ if app.config.email_error_reporter.enabled
12
+ Rails.error.subscribe(Subscriber.new)
13
+ end
14
+ end
15
+
16
+ # ActiveJob cannot (de)-serialize exceptions by default
17
+ initializer "email_error_reporter.exception_serializer" do |app|
18
+ app.config.active_job.custom_serializers << ExceptionSerializer
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ module EmailErrorReporter
2
+ class ErrorMailer < ActionMailer::Base
3
+ def error(error, handled:, severity:, context:, source: nil)
4
+ @error = error
5
+ @handled = handled
6
+ @severity = severity
7
+ @context = context
8
+ @source = source
9
+
10
+ @backtrace = Array.wrap(error.backtrace)
11
+
12
+ severity_to_emoji = {
13
+ error: "🔥",
14
+ warning: "⚠️",
15
+ info: "ℹ️"
16
+ }
17
+
18
+ mail(
19
+ subject: "#{severity_to_emoji.fetch(@severity)} #{error.class}",
20
+ from: Rails.application.config.email_error_reporter.from,
21
+ to: Rails.application.config.email_error_reporter.to
22
+ )
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ module EmailErrorReporter
2
+ class ExceptionSerializer < ActiveJob::Serializers::ObjectSerializer
3
+ def serialize(exception)
4
+ super(
5
+ exception_class: exception.class.to_s,
6
+ message: exception.message,
7
+ backtrace: exception.backtrace
8
+ )
9
+ end
10
+
11
+ def deserialize(hash)
12
+ hash[:exception_class].constantize.new(hash[:message]).tap do |e|
13
+ e.set_backtrace(hash[:backtrace])
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def klass
20
+ Exception
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ module EmailErrorReporter
2
+ class Subscriber
3
+ def report(error, handled:, severity:, context:, source: nil)
4
+ ErrorMailer.error(error,
5
+ handled: handled,
6
+ context: context,
7
+ severity: severity,
8
+ source: source).deliver_later
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module EmailErrorReporter
2
- VERSION = "0.1.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,6 +1,10 @@
1
1
  require "email_error_reporter/version"
2
- require "email_error_reporter/railtie"
2
+ require "email_error_reporter/engine"
3
3
 
4
4
  module EmailErrorReporter
5
- # Your code goes here...
5
+ extend ActiveSupport::Autoload
6
+
7
+ autoload :ErrorMailer
8
+ autoload :Subscriber
9
+ autoload :ExceptionSerializer
6
10
  end
@@ -1,4 +1,12 @@
1
- # desc "Explaining what the task does"
2
- # task :email_error_reporter do
3
- # # Task goes here
4
- # end
1
+ desc "Report a test exception"
2
+ namespace :email_error_reporter do
3
+ task check: :environment do
4
+ Rails.error.handle { raise "This is a test!" }
5
+ $stdout.puts <<~TXT
6
+ Test exception triggered.
7
+
8
+ Recipients:
9
+ #{Rails.application.config.email_error_reporter.to.join("\n").gsub(/^/, "* ")}
10
+ TXT
11
+ end
12
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: email_error_reporter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Niklas Haeusele
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-02 00:00:00.000000000 Z
11
+ date: 2024-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 7.1.2
19
+ version: 7.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 7.1.2
26
+ version: 7.0.0
27
27
  description: Report your Rails errors via email.
28
28
  email:
29
29
  - niklas.haeusele@hey.com
@@ -34,8 +34,14 @@ files:
34
34
  - MIT-LICENSE
35
35
  - README.md
36
36
  - Rakefile
37
+ - app/views/email_error_reporter/error_mailer/_frame.html.erb
38
+ - app/views/email_error_reporter/error_mailer/_no_backtrace.html.erb
39
+ - app/views/email_error_reporter/error_mailer/error.html.erb
37
40
  - lib/email_error_reporter.rb
38
- - lib/email_error_reporter/railtie.rb
41
+ - lib/email_error_reporter/engine.rb
42
+ - lib/email_error_reporter/error_mailer.rb
43
+ - lib/email_error_reporter/exception_serializer.rb
44
+ - lib/email_error_reporter/subscriber.rb
39
45
  - lib/email_error_reporter/version.rb
40
46
  - lib/tasks/email_error_reporter_tasks.rake
41
47
  homepage: https://github.com/codergeek121/email_error_reporter
@@ -53,7 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
53
59
  requirements:
54
60
  - - ">="
55
61
  - !ruby/object:Gem::Version
56
- version: '0'
62
+ version: 3.1.0
57
63
  required_rubygems_version: !ruby/object:Gem::Requirement
58
64
  requirements:
59
65
  - - ">="
@@ -1,4 +0,0 @@
1
- module EmailErrorReporter
2
- class Railtie < ::Rails::Railtie
3
- end
4
- end