Sentry-laravel: Laravel 5.8 'POST method is not supported for this route' exception not caught by Sentry

Created on 26 Jun 2019  路  11Comments  路  Source: getsentry/sentry-laravel

screencapture-dev-paclw-mro-16-2019-06-24-14_01_11 (1)

support

All 11 comments

End-user reported this on production and I was able to replicate on my dev system. Thank you!

cc @stayallive

This is "expected". Laravel ignores this exception by default:

https://github.com/laravel/framework/blob/7b2ba1657dbfa86776bbb13e43360580e6b826ff/src/Illuminate/Foundation/Exceptions/Handler.php#L61

(MethodNotAllowedHttpException extends the HttpException)

In the setup guide we do the following:

public function report(Exception $exception)
{
    if (app()->bound('sentry') && $this->shouldReport($exception)) {
        app('sentry')->captureException($exception);
    }

    parent::report($exception);
}

The $this->shouldReport($exception) call checks for that list.

Hope that explains it!

Not really. Why would Laravel/Sentry ignore an exception that breaks the app? Isn't the whole idea of Sentry to help catch ALL exceptions?

Anyway, thank you for taking a look at it.

Well, there are different definitions of "breaking the app" here.

This is a very common error when someone is just poking at your app or something in the front-end goes wrong. Usually it is nothing more than that, a configuration error or user error. It's a bit like a 404, you probably don't want to know about every one of them either.

This is the default in Laravel, this code I mentioned is in your own error handler, you can ofcourse change it to whatever you want to include in the reporting. So if you do want to catch them ALL remove the $this->shouldReport($exception) check but be careful since that could send a lot of events to Sentry most of which you might not care about.

OT: It's actually a problem I've had a few times in Laravel that I haven't been able to track down yet. Sometimes when I make an edit form and use @method('PATCH'), it throws an error that it can't use the method, so I switch it to standard post. Then days later, it throws this error about using Post for patching and I have to re-add @method('PATCH'). No idea what's going on there. Some kind of caching problem maybe.

Might be route caching, but that might be a better question for the Laravel folks :)

But now you know how to do report this exception if you want to until you figure it out 馃槃:

use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;

if (app()->bound('sentry') && ($this->shouldReport($exception) || $exception instanceof MethodNotAllowedHttpException)) {
    app('sentry')->captureException($exception);
}

I appreciate all of your replies, good sir!

@stayallive FWIW I had to use:

\Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException

instead of just MethodNotAllowedHttpException for that to work.

@futzlarson jep I forget to include the use statement needed, sorry about that, thanks for letting me know!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

IMM9O picture IMM9O  路  5Comments

andrey-helldar picture andrey-helldar  路  7Comments

jstolp picture jstolp  路  4Comments

carlosrgzm picture carlosrgzm  路  7Comments

smknstd picture smknstd  路  4Comments