From cca6cba56698e5514d6702d7f4b78aef41d2ba83 Mon Sep 17 00:00:00 2001 From: Meezaan-ud-Din Date: Tue, 16 Apr 2019 07:01:48 +0100 Subject: [PATCH] Update error handler for better logging. --- src/AlAdhanApi/Handler/AlAdhanHandler.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/AlAdhanApi/Handler/AlAdhanHandler.php b/src/AlAdhanApi/Handler/AlAdhanHandler.php index 808439d..42e5e4d 100644 --- a/src/AlAdhanApi/Handler/AlAdhanHandler.php +++ b/src/AlAdhanApi/Handler/AlAdhanHandler.php @@ -5,10 +5,11 @@ use AlAdhanApi\Helper\Log; use Slim\Http\Request; use Slim\Http\Response; +use Exception; class AlAdhanHandler { - public function __invoke(Request $request, Response $response, $exception = null) { + public function __invoke(Request $request, Response $response, Exception $exception) { if ($exception instanceof WafKeyMismatchException) { $r = [ @@ -21,15 +22,21 @@ public function __invoke(Request $request, Response $response, $exception = null }; $r = [ - 'code' => 500, + 'code' => $exception->getCode(), 'status' => 'Internal Server Error', 'data' => 'Something went wrong when the server tried to process this request. Sorry!' ]; $log = new Log(); - $log->error( $exception->getCode() . ' : ' . $exception->getMessage() . ' | ' . $exception->getTraceAsString()); - - return $response->withJson($r, 500); + $log->error('AlAdhan Exception Triggered.', + [ + 'code' => $exception->getCode(), + 'message' => $exception->getMessage(), + 'trace' => $exception->getTraceAsString() + ] + ); + + return $response->withJson($r, $exception->getCode()); } }