Stop logging 404-type errors to slack

This commit is contained in:
Jonny Barnes 2020-06-27 18:26:48 +01:00
parent f200ad3640
commit d7a5efe74a
3 changed files with 14 additions and 6 deletions

View file

@ -63,6 +63,6 @@ APP_LANG=en
APP_LOG=daily APP_LOG=daily
SECURE_SESSION_COOKIE=true SECURE_SESSION_COOKIE=true
SLACK_WEBHOOK_URL= LOG_SLACK_WEBHOOK_URL=
FONT_LINK= FONT_LINK=

View file

@ -3,11 +3,13 @@
namespace App\Exceptions; namespace App\Exceptions;
use Exception; use Exception;
use GuzzleHttp\Client;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Illuminate\Session\TokenMismatchException; use Illuminate\Session\TokenMismatchException;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable; use Throwable;
/** /**
@ -21,7 +23,7 @@ class Handler extends ExceptionHandler
* @var array * @var array
*/ */
protected $dontReport = [ protected $dontReport = [
\Symfony\Component\HttpKernel\Exception\HttpException::class, \Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class,
]; ];
/** /**
@ -47,14 +49,18 @@ class Handler extends ExceptionHandler
{ {
parent::report($throwable); parent::report($throwable);
$guzzle = new \GuzzleHttp\Client([ if ($throwable instanceof NotFoundHttpException) {
return;
}
$guzzle = new Client([
'headers' => [ 'headers' => [
'Content-Type' => 'application/json', 'Content-Type' => 'application/json',
], ],
]); ]);
$guzzle->post( $guzzle->post(
env('SLACK_WEBHOOK_URL'), config('logging.slack'),
[ [
'body' => json_encode([ 'body' => json_encode([
'attachments' => [[ 'attachments' => [[
@ -64,8 +70,8 @@ class Handler extends ExceptionHandler
'author_name' => app()->environment(), 'author_name' => app()->environment(),
'author_link' => config('app.url'), 'author_link' => config('app.url'),
'fields' => [[ 'fields' => [[
'title' => get_class($this) ?? 'Unknown Exception', 'title' => get_class($throwable) ?? 'Unknown Exception',
'value' => $throwable->getMessage() ?? '', 'value' => $throwable->getTraceAsString() ?? '',
]], ]],
'ts' => time(), 'ts' => time(),
]], ]],

View file

@ -97,4 +97,6 @@ return [
], ],
], ],
'slack' => env('LOG_SLACK_WEBHOOK_URL'),
]; ];