Notify Slack on Exceptions (issue #97)

Squashed commit of the following:

commit 104a106d735f586f893dffb57e07f15b2e20d901
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Feb 1 21:54:06 2019 +0000

    Remove unused class use statement, add array trailing comma

commit 8fbc46e816789d26447a01cbd0d542c62a287671
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Feb 1 21:27:42 2019 +0000

    Set default values for slack message

commit b54650d85d73702dfb836436a964dd882844fff5
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Feb 1 21:26:06 2019 +0000

    Post exceptions to Slack

commit 74f394ab2b5e66eac8c3f75ccf13aae0a7bc8e11
Author: Jonny Barnes <jonny@jonnybarnes.uk>
Date:   Fri Feb 1 21:25:47 2019 +0000

    Slack Webhook env variable
This commit is contained in:
Jonny Barnes 2019-02-01 21:59:59 +00:00
parent 5ff469fe21
commit 45a2c01c48
2 changed files with 30 additions and 1 deletions

View file

@ -60,3 +60,5 @@ APP_TIMEZONE=UTC
APP_LANG=en
APP_LOG=daily
SECURE_SESSION_COOKIE=true
SLACK_WEBHOOK_URL=

View file

@ -2,6 +2,7 @@
namespace App\Exceptions;
use App;
use Exception;
use Illuminate\Support\Facades\Route;
use Illuminate\Session\TokenMismatchException;
@ -18,7 +19,7 @@ class Handler extends ExceptionHandler
* @var array
*/
protected $dontReport = [
//
\Symfony\Component\HttpKernel\Exception\HttpException::class,
];
/**
@ -41,6 +42,32 @@ class Handler extends ExceptionHandler
*/
public function report(Exception $exception)
{
$guzzle = new \GuzzleHttp\Client([
'headers' => [
'Content-Type' => 'application/json',
],
]);
$guzzle->post(
env('SLACK_WEBHOOK_URL'),
[
'body' => json_encode([
'attachments' => [[
'fallback' => 'There was an exception.',
'pretext' => 'There was an exception.',
'color' => '#d00000',
'author_name' => App::environment(),
'author_link' => config('app.url'),
'fields' => [[
'title' => get_class($exception) ?? 'Unkown Exception',
'value' => $exception->getMessage() ?? '',
]],
'ts' => time(),
]],
]),
]
);
parent::report($exception);
}