From d7a5efe74a8fe6f20cd77a87dd8b5404c9fa669d Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Sat, 27 Jun 2020 18:26:48 +0100 Subject: [PATCH] Stop logging 404-type errors to slack --- .env.example | 2 +- app/Exceptions/Handler.php | 16 +++++++++++----- config/logging.php | 2 ++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/.env.example b/.env.example index 55c27150..114a68a0 100644 --- a/.env.example +++ b/.env.example @@ -63,6 +63,6 @@ APP_LANG=en APP_LOG=daily SECURE_SESSION_COOKIE=true -SLACK_WEBHOOK_URL= +LOG_SLACK_WEBHOOK_URL= FONT_LINK= diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 57f44619..3b156bc4 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -3,11 +3,13 @@ namespace App\Exceptions; use Exception; +use GuzzleHttp\Client; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Session\TokenMismatchException; use Illuminate\Support\Facades\Route; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Throwable; /** @@ -21,7 +23,7 @@ class Handler extends ExceptionHandler * @var array */ 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); - $guzzle = new \GuzzleHttp\Client([ + if ($throwable instanceof NotFoundHttpException) { + return; + } + + $guzzle = new Client([ 'headers' => [ 'Content-Type' => 'application/json', ], ]); $guzzle->post( - env('SLACK_WEBHOOK_URL'), + config('logging.slack'), [ 'body' => json_encode([ 'attachments' => [[ @@ -64,8 +70,8 @@ class Handler extends ExceptionHandler 'author_name' => app()->environment(), 'author_link' => config('app.url'), 'fields' => [[ - 'title' => get_class($this) ?? 'Unknown Exception', - 'value' => $throwable->getMessage() ?? '', + 'title' => get_class($throwable) ?? 'Unknown Exception', + 'value' => $throwable->getTraceAsString() ?? '', ]], 'ts' => time(), ]], diff --git a/config/logging.php b/config/logging.php index 0df82129..61d002e7 100644 --- a/config/logging.php +++ b/config/logging.php @@ -97,4 +97,6 @@ return [ ], ], + 'slack' => env('LOG_SLACK_WEBHOOK_URL'), + ];