Comments (19)
There's a snippet in the making that may be just what you need: hanami/snippets#10
from controller.
from controller.
@mcansky I'm sorry about the confusion. I see two problems here: 1. the expectations regarding this feature 2. how to make it work.
Let me first clarify the goal of exception handling feature. It's meant to catch an exception and turn it into a HTTP error status (usually 4xx). Because it's managed (aka expected), you usually don't want to report it to Sentry, but sent unmanaged (aka unexpected) exceptions instead.
This is the typical usage of the feature. If you still want to send to Sentry all the exceptions, even the ones caught via handle_exception
, I suggest to have a look at this paragraph: https://guides.hanamirb.org/actions/exception-handling/#custom-handlers
Via a custom handler you can manually send exceptions to Sentry: https://docs.sentry.io/clients/ruby/#reporting-failures
class MyAction
include Hanami::Action
handle_exception FooError => :handle_foo_error
def call(params)
# ...
end
private
def handle_foo_error(exception)
Raven.capture_exception(exception)
status 400, exception.message
end
end
If you need to handle FooError
across all the actions, you can share the handling like this: https://guides.hanamirb.org/actions/share-code/
Now getting back to your exception raised from Redis. How's your code looking like?
from controller.
thanks both for your answer, I think indeed there is a bit of confusion about what we all mean and expect.
Let me take a clearer example.
Let's say we add a feature that does some work with an API, pull some data in as JSON, unmarshall it into objects and .. boom, for some reason the data in the JSON isn't as expected and an exception is raised because the code was never meant to handle this case (yet).
So yes, that refers to unmanaged exceptions.
I have tried adding a module and using the application configuration to get all the actions to include a custom method to call with handle_exception StandardError => :handle_errors
but that's to no avail. Exceptions happening don't bubble up and I am still left with a 500.
So I guess my issue is more with making sure exceptions triggered through an action and within methods and libs called by the actions do bubble up to Sentry for triage, inspection, and fix. And I would expect that things happening in the view and template also can be caught and passed to sentry if it's an unexpected exception.
here is the extract from apps/web/application.rb
controller.prepare do
include RavenHanami
handle_exception StandardError => :capture_exception
end
and the module :
module RavenHanami
private
def capture_exception(exception)
LOGGER.info("!!!!! EXCEPTION Caught for Raven/Sentry !!!!!")
Raven.capture_exception(exception)
redirect_to '/'
end
end
is there something missing ?
from controller.
I'm confused about this:
Exceptions happening don't bubble up and I am still left with a 500.
If the exception does not bubble up, then it must be handled by someone and you would not get a 500. The fact that you get a 500 page shows that Hanami is handling the exception at the controller level.
It just seems like your custom handler is not triggered..
But your code looks fine. Maybe the exception you're raising isn't a kind of StandardError
?
Also make sure that - if you're testing this in development - you configured this in your application.rb:
configure :development do
handle_exceptions true
end
from controller.
@kaikuchn : most exceptions in Ruby and its frameworks are inheriting from StandardError afaik, so that should go ok (I suppose ?).
I will do another round of checks ... and report back.
from controller.
@mcansky 👋 any news on this?
from controller.
@mcansky Did you manage to solve this?
from controller.
@DangerDawson do you have the same problem?
from controller.
apologies everyone it looks like I moved on into another project and could not report back;
from controller.
So I have got to the bottom of what is happening, any exceptions being raised in the controller produces both a stack_trace and send the exception message to HoneyBadger, although if the exception happens in the view object it does not.
Is this expected behaviour?
from controller.
Docs say this:
Handle the given exception with an HTTP status code.
When the exception is raise during #call execution, it will be
translated into the associated HTTP status.
However, as can be seen in Callable the Rack response is prepared outside of the rescue block. And the result of Action#call
is passed into the renderer when using a full stack hanami app.
I.e., the view is rendered only after the action finished executing. (See here, middleware
would be the action in most cases.)
Anyway, IMHO this is not very user-friendly. As a user I'd expect exception handling for an endpoint to handle all exceptions that happen when hitting that endpoint, as long as it occurs in user code. As a user I would be very surprised that exceptions in views/templates are not handled.
WDYT? @jodosha
from controller.
So it looks as though this PR #866 is responsible for the behaviour I am witnessing, specifically this line.
I managed to solve my specific issue by inserting the HoneyBadger middleware in my config.ru
require './config/environment'
use Honeybadger::Rack::ErrorNotifier
run Hanami.app
Although I still find the behaviour confusing that exceptions in the view are handled differently from those that manifest in the controller / services / etc.
from controller.
Any update on this?
Although I am able to receive exceptions in Honeybadger, I do not have any context, and the logs do not provide any stacktrace either.
Like I said, this is completely different behaviour from any exceptions happening before the view rendering e.g. services / controllers, which do provide a full stacktrace and context.
from controller.
We also struggled getting exceptions reported to Rollbar. They were reported for anything job related but not for request. Something was preventing the exception to bubble up so we ended up setting handle_exceptions false
for production and fixed it.
from controller.
We ended up fixing this by putting the rack middleware in config.ru e.g.
use Honeybadger::Rack::ErrorNotifier
use Honeybadger::Rack::UserFeedback
use Honeybadger::Rack::UserInformer
from controller.
I got an email notification for someone posting a comment on this thread, which it seems like they have deleted. Not going to tag them since they deleted it for a reason, but I think the comment is a helpful piece of feedback:
I agree that the default behavior is confusing and ends up being a really bad UX, since the defaults make it so your error reporting system doesn’t see 500s in production. All unhandled exceptions should go to error reporting system, yet we should be able to render custom views for these errors.
I think we’re going to have to monkeypatch this behavior for now 😞
from controller.
Closing as stale. Feel free to reopen, in case.
from controller.
so @jodosha , what's the way to handle exceptions towards an exception tracker ?
I guess the "simplest" is to plug into https://github.com/hanami/controller#exceptions-management
has a standard way to do so in an Hanami app been documented ?
from controller.
Related Issues (20)
- Hanami::Action::Flash to use swappable JSON backend HOT 2
- Flash message appears twice (redux) HOT 3
- RuntimeError: can't add a new key into hash during iteration HOT 8
- Compatability issue with hanami-controller 2.0.0.alpha1 HOT 2
- Is `#request` supposed to be memoized for actions? HOT 2
- Standalone actions prevent using non-keyword parameters in `#initialize` (v2) HOT 3
- [2.0.0.alpha1] Uninitialized constant Hanami::Controller::Configuration (NameError) HOT 2
- Handling `config` and other class-level macros HOT 3
- accept syntax in README no longer seems supported HOT 1
- Action params are not coerced? HOT 5
- Badly encoded cookie strings from Safari results in an error HOT 3
- HTTP status 418 is missing HOT 1
- Issue with multipart/form-data format HOT 1
- Support CSRF by HTTP header HOT 4
- Support for hanami-validations/dry-validations `rule(...)`
- Documentation about Controller/Router integration is incorrect HOT 7
- Fix Response#send_file implementation
- handle_exception should allow exception names as strings
- Where `application/x-www-form-urlencoded` and `multipart/form-data`? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from controller.