-
-
Notifications
You must be signed in to change notification settings - Fork 158
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
Revamp error handler/action #903
Conversation
0a86782
to
a11e4fd
Compare
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.
Gonna try to merge this fairly quickly so I can do the Lucky CLI side of things and test this in some real world scenarios
|
||
private class FakeErrorAction < Lucky::ErrorAction | ||
def handle_error(error : FakeError) : Lucky::Response | ||
def render(error : CustomError) : Lucky::Response |
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.
Renamed handle_error
to render
. I think this is simpler and reads better
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.
Does this cause any issues with calling the render
method from actions? I guess it shouldn't if their argument types are different.
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.
I think it will work (or it wouldn't compile) but this could be kind of confusing or lead to unexpected compile errors since there could be tons of different overloads. I think for now I'll rename it render_error
I've been thinking of renaming render
to html
or something. Since everything else (like json
) has the type in the name. E.g. html Users::IndexPage, users: UserQuery.new
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.
I like the idea of using html
. It's an easy change.
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.
Done! I like it. And should be a pretty easy thing to upgrade
plain_text "This is not a debug page", status: 500 | ||
end | ||
|
||
def report(error : Exception) |
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.
Added an abstract report
method for reporting methods however you want!
private def call_error_action(context : HTTP::Server::Context, error : Exception) : HTTP::Server::Context | ||
status_code = status_code_by_error(error) | ||
context.response.status_code = status_code | ||
settings.logger.error(exception: error.inspect_with_backtrace) |
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.
Always log the exception
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.
Looks all good here from what I can tell. Feel free to merge, and we can fix up whatever issues come up.
it "calls the report method on the error action" do | ||
io = IO::Memory.new | ||
FakeErrorAction.temp_config(output: io) do | ||
handle_error(error: CustomError.new) do |_context, _output| |
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.
Should this be render
too?
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.
oh, nevermind. I see this method from below.
|
||
private class FakeErrorAction < Lucky::ErrorAction | ||
def handle_error(error : FakeError) : Lucky::Response | ||
def render(error : CustomError) : Lucky::Response |
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.
Does this cause any issues with calling the render
method from actions? I guess it shouldn't if their argument types are different.
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.
I think that's a good point about render
. I'll change that up for now and maybe revisit later if we rename render
(for html pages) to something else
|
||
private class FakeErrorAction < Lucky::ErrorAction | ||
def handle_error(error : FakeError) : Lucky::Response | ||
def render(error : CustomError) : Lucky::Response |
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.
I think it will work (or it wouldn't compile) but this could be kind of confusing or lead to unexpected compile errors since there could be tons of different overloads. I think for now I'll rename it render_error
I've been thinking of renaming render
to html
or something. Since everything else (like json
) has the type in the name. E.g. html Users::IndexPage, users: UserQuery.new
a11e4fd
to
2b7d9aa
Compare
2b7d9aa
to
3600ecb
Compare
Part of #868. See that issue for details