dislogger 0.1.0
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 +7 -0
- data/.coveralls.yml +3 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +17 -0
- data/LICENSE.txt +21 -0
- data/README.md +198 -0
- data/Rakefile +12 -0
- data/dislogger.gemspec +51 -0
- data/lib/dislogger/configuration.rb +32 -0
- data/lib/dislogger/error_handler.rb +73 -0
- data/lib/dislogger/errors/custom_errors.rb +8 -0
- data/lib/dislogger/formatters/base_formatter.rb +22 -0
- data/lib/dislogger/formatters/discord_formatter.rb +41 -0
- data/lib/dislogger/notifiers/base_notifier.rb +29 -0
- data/lib/dislogger/notifiers/discord_notifier.rb +39 -0
- data/lib/dislogger/railtie.rb +11 -0
- data/lib/dislogger/version.rb +5 -0
- data/lib/dislogger.rb +38 -0
- data/sig/dislogger.rbs +4 -0
- metadata +223 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2e0ad26ebcd9ef58a09780dd4eb7ae44dabc0ffae42abbcb7a0c4b91a5f3b7b4
|
4
|
+
data.tar.gz: 72fddc3ab2e214c0aef2763c6d9037ec21d68201a6a2d39e19f7375ff4556740
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a55c9cb951d2629533872199b951f5faa5175a0666af3a665bbcd79bf6170518e2e2fdba0986fe1feed196929aed99c201e41328b0ee1de0ae430d6412a3dc30
|
7
|
+
data.tar.gz: 171980de490dc3b71c3de9fb17ca7bca44be65a7fb4243cda24a8c919eac3437b0ad3a1b8f176025029c8e61089aab6f0b4e05454f62fad17ac7327782baed11
|
data/.coveralls.yml
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at nelson.duartex@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in dislogger.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
|
10
|
+
gem "rspec", "~> 3.0"
|
11
|
+
|
12
|
+
gem "rubocop", "~> 1.21"
|
13
|
+
|
14
|
+
gem "pry"
|
15
|
+
gem "simplecov"
|
16
|
+
gem "webmock"
|
17
|
+
gem 'coveralls', require: false
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Nelson
|
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,198 @@
|
|
1
|
+
# Dislogger
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/dislogger)
|
4
|
+
[](https://coveralls.io/github/nduartex/dislogger?branch=master)
|
5
|
+
|
6
|
+
A Ruby on Rails gem for elegant error handling and Discord notifications. Automatically capture and notify errors through Discord webhooks while maintaining clean error responses for your API.
|
7
|
+
|
8
|
+
## Features
|
9
|
+
|
10
|
+
- 🚨 Automatic error handling for Rails applications
|
11
|
+
- 🎯 Discord notifications for errors with customizable formatting
|
12
|
+
- 📝 Detailed error information including backtrace
|
13
|
+
- 🎨 Customizable error colors for different status codes
|
14
|
+
- 🔧 Environment-based configuration
|
15
|
+
- 🛡️ Built-in support for common Rails exceptions
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'dislogger'
|
23
|
+
```
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
```bash
|
28
|
+
$ bundle install
|
29
|
+
```
|
30
|
+
|
31
|
+
Or install it yourself as:
|
32
|
+
|
33
|
+
```bash
|
34
|
+
$ gem install dislogger
|
35
|
+
```
|
36
|
+
|
37
|
+
## Configuration
|
38
|
+
|
39
|
+
### Setting up Discord Webhook
|
40
|
+
|
41
|
+
Before configuring the gem, you need to set up a webhook in your Discord server:
|
42
|
+
|
43
|
+
1. Open your Discord server
|
44
|
+
2. Right-click on the channel where you want to receive error notifications
|
45
|
+
3. Select "Edit Channel"
|
46
|
+
4. Click on "Integrations"
|
47
|
+
5. Click on "Create Webhook"
|
48
|
+
6. Give your webhook a name (e.g., "Error Logger")
|
49
|
+
7. Click "Copy Webhook URL" - This is the URL you'll need for configuration
|
50
|
+
8. Click "Save"
|
51
|
+
|
52
|
+
For security, it's recommended to store the webhook URL in your environment variables:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
# config/initializers/dislogger.rb
|
56
|
+
Dislogger.configure do |config|
|
57
|
+
config.discord_webhook_url = ENV['DISCORD_WEBHOOK_URL']
|
58
|
+
# ... rest of your configuration
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
Then add the webhook URL to your environment:
|
63
|
+
|
64
|
+
```bash
|
65
|
+
# .env
|
66
|
+
DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/your-webhook-url
|
67
|
+
```
|
68
|
+
|
69
|
+
Make sure to add `.env` to your `.gitignore` file to keep your webhook URL secure.
|
70
|
+
|
71
|
+
### Additional Configuration Options
|
72
|
+
|
73
|
+
Create an initializer file at `config/initializers/dislogger.rb` with all available options:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
Dislogger.configure do |config|
|
77
|
+
# Required: Your Discord webhook URL (preferably from environment variables)
|
78
|
+
config.discord_webhook_url = ENV['DISCORD_WEBHOOK_URL']
|
79
|
+
|
80
|
+
# Optional: Custom bot username (default: 'Error Logger')
|
81
|
+
config.bot_username = 'My App Error Logger'
|
82
|
+
|
83
|
+
# Optional: Number of backtrace lines to include (default: 5)
|
84
|
+
config.backtrace_lines_limit = 10
|
85
|
+
|
86
|
+
# Optional: Environments where notifications are enabled (default: ['production', 'staging'])
|
87
|
+
config.enabled_environments = ['production', 'staging', 'development']
|
88
|
+
|
89
|
+
# Optional: Current environment (default: Rails.env if available)
|
90
|
+
config.environment = 'production'
|
91
|
+
|
92
|
+
# Optional: Custom error colors (default values shown below)
|
93
|
+
config.error_color_map = {
|
94
|
+
500 => 15158332, # Red
|
95
|
+
404 => 3447003, # Blue
|
96
|
+
422 => 16776960, # Yellow
|
97
|
+
403 => 15105570, # Orange
|
98
|
+
default: 10181046 # Gray
|
99
|
+
}
|
100
|
+
end
|
101
|
+
```
|
102
|
+
|
103
|
+
## Usage
|
104
|
+
|
105
|
+
### Basic Setup
|
106
|
+
|
107
|
+
The gem automatically includes error handling in your Rails controllers. No additional setup is required!
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
class ApplicationController < ActionController::Base
|
111
|
+
# Error handling is automatically included
|
112
|
+
end
|
113
|
+
```
|
114
|
+
|
115
|
+
### Handled Exceptions
|
116
|
+
|
117
|
+
The gem automatically handles the following exceptions:
|
118
|
+
|
119
|
+
- `ActiveRecord::RecordNotFound` (404)
|
120
|
+
- `ActiveRecord::RecordInvalid` (422)
|
121
|
+
- `ActionController::ParameterMissing` (422)
|
122
|
+
- `Pundit::NotAuthorizedError` (403)
|
123
|
+
- `CanCan::AccessDenied` (403)
|
124
|
+
- `StandardError` (500)
|
125
|
+
|
126
|
+
### JSON Response Format
|
127
|
+
|
128
|
+
Errors are automatically rendered as JSON with a consistent format:
|
129
|
+
|
130
|
+
```json
|
131
|
+
{
|
132
|
+
"status": "not_found",
|
133
|
+
"message": "Record not found",
|
134
|
+
"details": ["Additional error details if available"]
|
135
|
+
}
|
136
|
+
```
|
137
|
+
|
138
|
+
### Discord Notifications
|
139
|
+
|
140
|
+
When an error occurs, a Discord notification will be sent with:
|
141
|
+
|
142
|
+
- Error title and status code
|
143
|
+
- Environment information
|
144
|
+
- Error message
|
145
|
+
- Backtrace (configurable number of lines)
|
146
|
+
- Custom color based on error type
|
147
|
+
|
148
|
+
### Manual Error Handling
|
149
|
+
|
150
|
+
You can also manually use the error handler in your controllers:
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
class CustomController < ApplicationController
|
154
|
+
def some_action
|
155
|
+
# Your code here
|
156
|
+
rescue SomeError => e
|
157
|
+
notify_and_render_error(
|
158
|
+
message: 'Custom error message',
|
159
|
+
status: :unprocessable_entity,
|
160
|
+
details: ['Additional details'],
|
161
|
+
backtrace: e.backtrace
|
162
|
+
)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
```
|
166
|
+
|
167
|
+
## Development
|
168
|
+
|
169
|
+
After checking out the repo, run:
|
170
|
+
|
171
|
+
```bash
|
172
|
+
$ bundle install
|
173
|
+
```
|
174
|
+
|
175
|
+
To run the tests:
|
176
|
+
|
177
|
+
```bash
|
178
|
+
$ bundle exec rspec
|
179
|
+
```
|
180
|
+
|
181
|
+
## Contributing
|
182
|
+
|
183
|
+
1. Fork it
|
184
|
+
2. Create your feature branch (`git checkout -b feature/my-new-feature`)
|
185
|
+
3. Add tests for your changes
|
186
|
+
4. Make your changes
|
187
|
+
5. Run the tests (`bundle exec rspec`)
|
188
|
+
6. Commit your changes (`git commit -am 'Add some feature'`)
|
189
|
+
7. Push to the branch (`git push origin feature/my-new-feature`)
|
190
|
+
8. Create a new Pull Request
|
191
|
+
|
192
|
+
## License
|
193
|
+
|
194
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
195
|
+
|
196
|
+
## Code of Conduct
|
197
|
+
|
198
|
+
Everyone interacting in the Dislogger project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/dislogger.gemspec
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/dislogger/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "dislogger"
|
7
|
+
spec.version = Dislogger::VERSION
|
8
|
+
spec.authors = ["Nelson"]
|
9
|
+
spec.email = ["nelson.duartex@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "A Rails gem for standardized error handling with Discord notifications"
|
12
|
+
spec.description = "DisLogger provides a robust error handling system for Rails applications with automatic Discord notifications and standardized JSON responses"
|
13
|
+
spec.homepage = "https://github.com/nelsonduarte/dislogger"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.7.0"
|
16
|
+
|
17
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
18
|
+
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = "https://github.com/nelsonduarte/dislogger"
|
21
|
+
spec.metadata["changelog_uri"] = "https://github.com/nelsonduarte/dislogger/blob/main/CHANGELOG.md"
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(__dir__) do
|
26
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
(File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
|
28
|
+
end
|
29
|
+
end
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ["lib"]
|
33
|
+
|
34
|
+
# Dependencies
|
35
|
+
spec.add_dependency "rails", ">= 6.0"
|
36
|
+
spec.add_dependency "httparty", "~> 0.21.0"
|
37
|
+
spec.add_dependency "activesupport", ">= 6.0"
|
38
|
+
spec.add_dependency "actionpack", ">= 6.0"
|
39
|
+
|
40
|
+
# Development dependencies
|
41
|
+
spec.add_development_dependency "rspec", "~> 3.12"
|
42
|
+
spec.add_development_dependency "rubocop", "~> 1.50"
|
43
|
+
spec.add_development_dependency "rubocop-rspec", "~> 2.20"
|
44
|
+
spec.add_development_dependency "pry", "~> 0.14"
|
45
|
+
spec.add_development_dependency "webmock", "~> 3.18"
|
46
|
+
spec.add_development_dependency "simplecov", "~> 0.22"
|
47
|
+
spec.add_development_dependency "activerecord", ">= 6.0"
|
48
|
+
|
49
|
+
# For more information and examples about making a new gem, check out our
|
50
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
51
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Dislogger
|
4
|
+
class Configuration
|
5
|
+
attr_accessor :discord_webhook_url,
|
6
|
+
:environment,
|
7
|
+
:bot_username,
|
8
|
+
:error_color_map,
|
9
|
+
:backtrace_lines_limit,
|
10
|
+
:enabled_environments
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@discord_webhook_url = nil
|
14
|
+
@environment = nil
|
15
|
+
@bot_username = 'Error Logger'
|
16
|
+
@backtrace_lines_limit = 5
|
17
|
+
@enabled_environments = %w[production staging]
|
18
|
+
@error_color_map = {
|
19
|
+
500 => 15158332, # Red for server errors
|
20
|
+
404 => 3447003, # Blue for not found
|
21
|
+
422 => 16776960, # Yellow for validation errors
|
22
|
+
403 => 15105570, # Orange for forbidden
|
23
|
+
default: 10181046 # Gray for others
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
def enabled?
|
28
|
+
return false if environment.nil? || enabled_environments.empty?
|
29
|
+
enabled_environments.include?(environment)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
module Dislogger
|
6
|
+
module ErrorHandler
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
rescue_from StandardError, with: :handle_internal_server_error
|
11
|
+
rescue_from ActiveRecord::RecordNotFound, with: :handle_not_found
|
12
|
+
rescue_from ActiveRecord::RecordInvalid, with: :handle_unprocessable_entity
|
13
|
+
rescue_from ActionController::ParameterMissing, with: :handle_unprocessable_entity
|
14
|
+
rescue_from Pundit::NotAuthorizedError, with: :handle_forbidden if defined?(Pundit)
|
15
|
+
rescue_from CanCan::AccessDenied, with: :handle_forbidden if defined?(CanCan)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def handle_not_found(exception)
|
21
|
+
notify_and_render_error(
|
22
|
+
message: exception.message.presence || 'Resource not found',
|
23
|
+
status: :not_found
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
def handle_unprocessable_entity(exception)
|
28
|
+
errors = exception.respond_to?(:record) ? exception.record.errors.full_messages : [exception.message]
|
29
|
+
notify_and_render_error(
|
30
|
+
message: 'Validation failed',
|
31
|
+
status: :unprocessable_entity,
|
32
|
+
details: errors
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
def handle_forbidden(exception)
|
37
|
+
notify_and_render_error(
|
38
|
+
message: exception.message.presence || 'You are not authorized to perform this action',
|
39
|
+
status: :forbidden
|
40
|
+
)
|
41
|
+
end
|
42
|
+
|
43
|
+
def handle_internal_server_error(exception)
|
44
|
+
notify_and_render_error(
|
45
|
+
message: 'Internal Server Error',
|
46
|
+
status: :internal_server_error,
|
47
|
+
backtrace: exception.backtrace
|
48
|
+
)
|
49
|
+
end
|
50
|
+
|
51
|
+
def notify_and_render_error(message:, status:, details: nil, backtrace: nil)
|
52
|
+
status_code = Rack::Utils::SYMBOL_TO_STATUS_CODE[status]
|
53
|
+
|
54
|
+
Notifiers::DiscordNotifier.new.notify(
|
55
|
+
message: message,
|
56
|
+
status: status_code,
|
57
|
+
backtrace: backtrace
|
58
|
+
)
|
59
|
+
|
60
|
+
render_error(message, status, details)
|
61
|
+
end
|
62
|
+
|
63
|
+
def render_error(message, status, details = nil)
|
64
|
+
error_response = {
|
65
|
+
status: status,
|
66
|
+
message: message
|
67
|
+
}
|
68
|
+
error_response[:details] = details if details
|
69
|
+
|
70
|
+
render json: error_response, status: status
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Dislogger
|
4
|
+
module Formatters
|
5
|
+
class BaseFormatter
|
6
|
+
def initialize(message:, status:, backtrace: nil, config: Dislogger.configuration)
|
7
|
+
@message = message
|
8
|
+
@status = status
|
9
|
+
@backtrace = backtrace
|
10
|
+
@config = config
|
11
|
+
end
|
12
|
+
|
13
|
+
def format
|
14
|
+
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
attr_reader :message, :status, :backtrace, :config
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Dislogger
|
4
|
+
module Formatters
|
5
|
+
class DiscordFormatter < BaseFormatter
|
6
|
+
def format
|
7
|
+
{
|
8
|
+
username: @config.bot_username,
|
9
|
+
embeds: [
|
10
|
+
{
|
11
|
+
title: "#{@config.environment.capitalize} - Error Notification (#{@status})",
|
12
|
+
description: @message,
|
13
|
+
color: @config.error_color_map[@status] || @config.error_color_map[:default],
|
14
|
+
fields: build_fields,
|
15
|
+
timestamp: Time.current.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
|
16
|
+
}
|
17
|
+
]
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def build_fields
|
24
|
+
fields = [
|
25
|
+
{ name: 'Status Code', value: @status.to_s, inline: true },
|
26
|
+
{ name: 'Environment', value: @config.environment, inline: true }
|
27
|
+
]
|
28
|
+
|
29
|
+
if @backtrace
|
30
|
+
fields << {
|
31
|
+
name: 'Backtrace',
|
32
|
+
value: @backtrace.first(@config.backtrace_lines_limit).join("\n"),
|
33
|
+
inline: false
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
fields
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Dislogger
|
4
|
+
module Notifiers
|
5
|
+
class BaseNotifier
|
6
|
+
def initialize(config = Dislogger.configuration)
|
7
|
+
@config = config
|
8
|
+
end
|
9
|
+
|
10
|
+
def notify(message:, status:, backtrace: nil)
|
11
|
+
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
|
12
|
+
end
|
13
|
+
|
14
|
+
protected
|
15
|
+
|
16
|
+
def enabled?
|
17
|
+
@config.enabled?
|
18
|
+
end
|
19
|
+
|
20
|
+
def format_message(message, status, backtrace)
|
21
|
+
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
|
22
|
+
end
|
23
|
+
|
24
|
+
def send_notification(payload)
|
25
|
+
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Dislogger
|
4
|
+
module Notifiers
|
5
|
+
class DiscordNotifier < BaseNotifier
|
6
|
+
def notify(message:, status:, backtrace: nil)
|
7
|
+
return unless enabled? && @config.discord_webhook_url.present?
|
8
|
+
|
9
|
+
formatted_message = format_message(message, status, backtrace)
|
10
|
+
send_notification(formatted_message)
|
11
|
+
end
|
12
|
+
|
13
|
+
protected
|
14
|
+
|
15
|
+
def format_message(message, status, backtrace)
|
16
|
+
Formatters::DiscordFormatter.new(
|
17
|
+
message: message,
|
18
|
+
status: status,
|
19
|
+
backtrace: backtrace,
|
20
|
+
config: @config
|
21
|
+
).format
|
22
|
+
end
|
23
|
+
|
24
|
+
def send_notification(payload)
|
25
|
+
HTTParty.post(
|
26
|
+
@config.discord_webhook_url,
|
27
|
+
body: payload.to_json,
|
28
|
+
headers: { 'Content-Type' => 'application/json' }
|
29
|
+
)
|
30
|
+
rescue StandardError => e
|
31
|
+
if defined?(Rails) && Rails.logger
|
32
|
+
Rails.logger.error("Discord notification failed: #{e.message}")
|
33
|
+
else
|
34
|
+
warn("Discord notification failed: #{e.message}")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Dislogger
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
initializer 'dislogger.configure_rails_initialization' do
|
6
|
+
ActiveSupport.on_load(:action_controller) do
|
7
|
+
ActionController::Base.include Dislogger::ErrorHandler
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/lib/dislogger.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails"
|
4
|
+
require "active_support"
|
5
|
+
require "active_support/concern"
|
6
|
+
require "active_support/core_ext/string"
|
7
|
+
require "active_support/core_ext/module/delegation"
|
8
|
+
require "httparty"
|
9
|
+
require_relative "dislogger/version"
|
10
|
+
require_relative "dislogger/configuration"
|
11
|
+
require_relative "dislogger/error_handler"
|
12
|
+
require_relative "dislogger/notifiers/base_notifier"
|
13
|
+
require_relative "dislogger/notifiers/discord_notifier"
|
14
|
+
require_relative "dislogger/formatters/base_formatter"
|
15
|
+
require_relative "dislogger/formatters/discord_formatter"
|
16
|
+
require_relative "dislogger/errors/custom_errors"
|
17
|
+
|
18
|
+
module Dislogger
|
19
|
+
class Error < StandardError; end
|
20
|
+
|
21
|
+
class << self
|
22
|
+
attr_writer :configuration
|
23
|
+
|
24
|
+
def configuration
|
25
|
+
@configuration ||= Configuration.new
|
26
|
+
end
|
27
|
+
|
28
|
+
def configure
|
29
|
+
yield(configuration)
|
30
|
+
end
|
31
|
+
|
32
|
+
def reset
|
33
|
+
@configuration = Configuration.new
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
require "dislogger/railtie" if defined?(Rails)
|
data/sig/dislogger.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,223 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dislogger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nelson
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-03-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: httparty
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.21.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.21.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '6.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '6.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: actionpack
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '6.0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '6.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.12'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.12'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.50'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.50'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop-rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '2.20'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '2.20'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: pry
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0.14'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0.14'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: webmock
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '3.18'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '3.18'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: simplecov
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0.22'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0.22'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: activerecord
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '6.0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '6.0'
|
167
|
+
description: DisLogger provides a robust error handling system for Rails applications
|
168
|
+
with automatic Discord notifications and standardized JSON responses
|
169
|
+
email:
|
170
|
+
- nelson.duartex@gmail.com
|
171
|
+
executables: []
|
172
|
+
extensions: []
|
173
|
+
extra_rdoc_files: []
|
174
|
+
files:
|
175
|
+
- ".coveralls.yml"
|
176
|
+
- ".rspec"
|
177
|
+
- ".rubocop.yml"
|
178
|
+
- CHANGELOG.md
|
179
|
+
- CODE_OF_CONDUCT.md
|
180
|
+
- Gemfile
|
181
|
+
- LICENSE.txt
|
182
|
+
- README.md
|
183
|
+
- Rakefile
|
184
|
+
- dislogger.gemspec
|
185
|
+
- lib/dislogger.rb
|
186
|
+
- lib/dislogger/configuration.rb
|
187
|
+
- lib/dislogger/error_handler.rb
|
188
|
+
- lib/dislogger/errors/custom_errors.rb
|
189
|
+
- lib/dislogger/formatters/base_formatter.rb
|
190
|
+
- lib/dislogger/formatters/discord_formatter.rb
|
191
|
+
- lib/dislogger/notifiers/base_notifier.rb
|
192
|
+
- lib/dislogger/notifiers/discord_notifier.rb
|
193
|
+
- lib/dislogger/railtie.rb
|
194
|
+
- lib/dislogger/version.rb
|
195
|
+
- sig/dislogger.rbs
|
196
|
+
homepage: https://github.com/nelsonduarte/dislogger
|
197
|
+
licenses:
|
198
|
+
- MIT
|
199
|
+
metadata:
|
200
|
+
allowed_push_host: https://rubygems.org
|
201
|
+
homepage_uri: https://github.com/nelsonduarte/dislogger
|
202
|
+
source_code_uri: https://github.com/nelsonduarte/dislogger
|
203
|
+
changelog_uri: https://github.com/nelsonduarte/dislogger/blob/main/CHANGELOG.md
|
204
|
+
post_install_message:
|
205
|
+
rdoc_options: []
|
206
|
+
require_paths:
|
207
|
+
- lib
|
208
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
209
|
+
requirements:
|
210
|
+
- - ">="
|
211
|
+
- !ruby/object:Gem::Version
|
212
|
+
version: 2.7.0
|
213
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
214
|
+
requirements:
|
215
|
+
- - ">="
|
216
|
+
- !ruby/object:Gem::Version
|
217
|
+
version: '0'
|
218
|
+
requirements: []
|
219
|
+
rubygems_version: 3.2.3
|
220
|
+
signing_key:
|
221
|
+
specification_version: 4
|
222
|
+
summary: A Rails gem for standardized error handling with Discord notifications
|
223
|
+
test_files: []
|