-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/rails error handling improved #108
Open
MarcGrimme
wants to merge
2
commits into
shadabahmed:main
Choose a base branch
from
MarcGrimme:feature/rails-error-handling-improved
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'action_dispatch' | ||
require 'active_support/all' | ||
|
||
module LogStasher | ||
module ActionDispatch | ||
|
||
class DebugExceptions < ::ActionDispatch::DebugExceptions | ||
def self.build_exception_hash(wrapper) | ||
exception = wrapper.exception | ||
trace = wrapper.application_trace | ||
trace = wrapper.framework_trace if trace.empty? | ||
|
||
{ error: | ||
({ exception: exception.class.name, message: exception.message, trace: trace}. | ||
merge!( exception.respond_to?(:annotated_source_code) && { annotated_source_code: exception.annoted_source_code } || {} )) | ||
} | ||
end | ||
|
||
def initialize(app, routes_app = nil) | ||
@app = app | ||
@routes_app = routes_app | ||
end | ||
|
||
def call(env) | ||
begin | ||
status, header, body = @app.call(env) | ||
if header['X-Cascade'] == 'pass' | ||
raise ::ActionController::RoutingError, "No route matches [#{env['REQUEST_METHOD']}] #{env['PATH_INFO'].inspect}" | ||
end | ||
rescue Exception => exception | ||
log_error(env, ::ActionDispatch::ExceptionWrapper.new(env, exception)) | ||
end | ||
[status, header, body] | ||
end | ||
|
||
private | ||
|
||
def log_error(env, wrapper) | ||
LogStasher.logger << LogStasher.build_logstash_event(DebugExceptions.build_exception_hash(wrapper), ["exception"]).to_json + "\n" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
spec/lib/logstasher/rails_ext/rack/debug_exceptions_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require 'spec_helper' | ||
|
||
shared_examples 'MyApp' do | ||
before do | ||
class MyApp | ||
def initialize() | ||
end | ||
def call(*args) | ||
raise Exception.new("My Exception") | ||
end | ||
end | ||
end | ||
|
||
let(:app) { MyApp.new } | ||
let(:environment) { { 'action_dispatch.show_exceptions' => true } } | ||
let(:logger) { double } | ||
subject{ described_class.new(app) } | ||
|
||
before(:each) do | ||
allow(LogStasher).to receive(:logger).and_return(logger) | ||
allow(LogStasher.logger).to receive(:'<<').and_return(true) | ||
end | ||
end | ||
|
||
describe ::LogStasher::ActionDispatch::DebugExceptions do | ||
include_examples 'MyApp' | ||
|
||
describe '#build_exception_hash' do | ||
let (:wrapper) { double(exception: Exception.new("My Exception"), application_trace: [ "line5" ]) } | ||
it do | ||
hash = described_class.build_exception_hash(wrapper) | ||
|
||
expect(hash).to match({:error=>{:exception=>"Exception", :message=>"My Exception", :trace=>["line5"]}}) | ||
end | ||
end | ||
|
||
describe 'calls LogStasher.logger with json format exception' do | ||
describe '#log_error' do | ||
it do | ||
expect(LogStasher).to receive(:build_logstash_event) | ||
expect(LogStasher.logger).to receive(:'<<').and_return(true) | ||
subject.call(environment) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might want to use ternary operator (
?
) here.merge
method to make it more clear.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. For the quick first try I didn't have a better idea. But the merge! will be changed.