From c99fe76dbdf198d85a225ff0d3bacfbb55e1c477 Mon Sep 17 00:00:00 2001 From: MRizki28 Date: Fri, 9 Aug 2024 16:25:46 +0800 Subject: [PATCH] add error method --- src/ApiResponse/ApiResponse.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/ApiResponse/ApiResponse.php b/src/ApiResponse/ApiResponse.php index cd7ca2e..b726e15 100644 --- a/src/ApiResponse/ApiResponse.php +++ b/src/ApiResponse/ApiResponse.php @@ -49,4 +49,24 @@ public static function notFound($message = 'Data not found', int $code = 404) ['Content-Type' => 'application/json'] ); } + + /** + * Format an error response. + * + * @param string $message Message to include in the response. + * @param int $code HTTP status code. + * @return string JSON formatted response. + */ + + public static function error( \Throwable $th = null, $message = 'Error', int $code = 500,){ + return new Response( + json_encode([ + 'status' => 'error', + 'message' => $message, + 'error' => $th + ]), + $code, + ['Content-Type' => 'application/json'] + ); + } }