From f17b7a758a5c1c92e688a053a7cf0c9368736e06 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 24 Feb 2025 12:41:34 -0300 Subject: [PATCH] withJson con error --- app/src/Controller/API/withJson.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/app/src/Controller/API/withJson.php b/app/src/Controller/API/withJson.php index cc4c051..3d769ea 100644 --- a/app/src/Controller/API/withJson.php +++ b/app/src/Controller/API/withJson.php @@ -1,6 +1,7 @@ getBody()->write(json_encode($data)); return $response->withStatus($statusCode)->withHeader('Content-Type', 'application/json'); } + public function parseError(Throwable $exception): array + { + return [ + 'code' => $exception->getCode(), + 'message' => $exception->getMessage(), + 'file' => $exception->getFile(), + 'line' => $exception->getLine(), + 'trace' => $exception->getTraceAsString() + ]; + } + public function withError(ResponseInterface $response, Throwable $exception): ResponseInterface + { + $output = $this->parseError($exception); + + $response->getBody()->write(json_encode(['error' => $output])); + return $response->withHeader('Content-Type', 'application/json'); + } + public function withErrors(ResponseInterface $response, array $errors): ResponseInterface + { + foreach ($errors as $error) { + $response = $this->withError($response, $error); + } + + return $response; + } + }