From d3a7473243cac7e34ea4b3a103dccb50cb695f7e Mon Sep 17 00:00:00 2001 From: Valentin Sickert <17144397+Lapotor@users.noreply.github.com> Date: Sun, 26 Nov 2023 05:03:31 +0100 Subject: [PATCH 1/3] feat: add default response consturct Signed-off-by: Valentin Sickert <17144397+Lapotor@users.noreply.github.com> --- app/Http/Responses/ApiErrorResponse.php | 50 +++++++++++++++++++++++ app/Http/Responses/ApiSuccessResponse.php | 41 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 app/Http/Responses/ApiErrorResponse.php create mode 100644 app/Http/Responses/ApiSuccessResponse.php diff --git a/app/Http/Responses/ApiErrorResponse.php b/app/Http/Responses/ApiErrorResponse.php new file mode 100644 index 00000000..3f912775 --- /dev/null +++ b/app/Http/Responses/ApiErrorResponse.php @@ -0,0 +1,50 @@ + $this->message]; + + if (! is_null($this->exception) && config('app.debug')) { + $response['debug'] = [ + 'message' => $this->exception->getMessage(), + 'file' => $this->exception->getFile(), + 'line' => $this->exception->getLine(), + 'trace' => $this->exception->getTrace(), + ]; + } + + return response()->json([ + $response, + 'status' => $this->status, + 'timestamp' => now()->toDateTimeString(), + ], $this->status, $this->headers); + } + +} diff --git a/app/Http/Responses/ApiSuccessResponse.php b/app/Http/Responses/ApiSuccessResponse.php new file mode 100644 index 00000000..b8deb1ed --- /dev/null +++ b/app/Http/Responses/ApiSuccessResponse.php @@ -0,0 +1,41 @@ +json( + [ + 'status' => $this->status, + 'data' => $this->data, + 'timestamp' => now()->toDateTimeString(), + ], + $this->status, + $this->headers + ); + } + +} From e161fc5e966cd95568861aabc3508208daf18900 Mon Sep 17 00:00:00 2001 From: Valentin Sickert <17144397+Lapotor@users.noreply.github.com> Date: Sun, 26 Nov 2023 05:04:05 +0100 Subject: [PATCH 2/3] change trace to string Signed-off-by: Valentin Sickert <17144397+Lapotor@users.noreply.github.com> --- app/Http/Responses/ApiErrorResponse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Responses/ApiErrorResponse.php b/app/Http/Responses/ApiErrorResponse.php index 3f912775..d440e8f7 100644 --- a/app/Http/Responses/ApiErrorResponse.php +++ b/app/Http/Responses/ApiErrorResponse.php @@ -36,7 +36,7 @@ public function toResponse($request) 'message' => $this->exception->getMessage(), 'file' => $this->exception->getFile(), 'line' => $this->exception->getLine(), - 'trace' => $this->exception->getTrace(), + 'trace' => $this->exception->getTraceAsString(), ]; } From 29835836ba0e4e6b16c159719e934a67b9ea18f2 Mon Sep 17 00:00:00 2001 From: Valentin Sickert <17144397+Lapotor@users.noreply.github.com> Date: Sun, 26 Nov 2023 05:05:33 +0100 Subject: [PATCH 3/3] clear whitespace Signed-off-by: Valentin Sickert <17144397+Lapotor@users.noreply.github.com> --- app/Http/Responses/ApiErrorResponse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Responses/ApiErrorResponse.php b/app/Http/Responses/ApiErrorResponse.php index d440e8f7..4ad3845f 100644 --- a/app/Http/Responses/ApiErrorResponse.php +++ b/app/Http/Responses/ApiErrorResponse.php @@ -31,7 +31,7 @@ public function toResponse($request) { $response = ['message' => $this->message]; - if (! is_null($this->exception) && config('app.debug')) { + if (!is_null($this->exception) && config('app.debug')) { $response['debug'] = [ 'message' => $this->exception->getMessage(), 'file' => $this->exception->getFile(),