Global error handler for ace
commands
#4941
JustasStarred
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Handling errors in a HTTP context is easy with a global exception handler, I'm looking to do the same with Ace commands. Ultimately, my goal is to get all errors from failed
node ace
calls into Sentry. I've see this discussion and am already using that package, however it only handles HTTP context errors.I looked into
bin/console.ts
, which already comes with an example to hook into the lifecycle. I thought it would be as easy as initialising Sentry in theinitiation
hook and then callingSentry.captureException
in thecatch()
.Given a test command:
The
catch()
inbin/console.ts
does not get called. Upon further investigation it's because theace
Kernel handles the error by callingerrorHandler.render
. There is no way to override this error handler, nor does the default ExceptionHandler offer a way to hook into it.I did however notice that the ace Kernel keeps a reference to the "main command" which is an instance of
BaseCommand
that itself keeps a reference to error thrown while executing. I then modified thebin/console.ts
to:While this works, it has to rely on the
getMainCommand
which I do not believe is documented anywhere. It does not work for built-in commands likemigration:run
since they have their own error handling. Given that it will only work for custom-commands, it's easier to simply wrapBaseCommand
with error handling and making all commands inherit from it.Additionally, the approach gets more complicated because by default ace commands do not boot the application.
In a perfect scenario, I would argue that the ace kernel should come with it's own equivalent of
exceptions/handler.ts
, or atleast the ability to set one. Even if that won't address commands likemigration:run
which do not throw on error.Beta Was this translation helpful? Give feedback.
All reactions