diff --git a/README.md b/README.md index a6b12f8..feb1965 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ For basic response use class and call response() function and pass status code n This will not send any data itself, this function is used last to generate response and set status code. +Usage: + ```php return ApiResponse::response(201); ``` @@ -28,8 +30,40 @@ Response: } ``` +For exceptions use `exception()` function and pass whole **Exception**; + +It will check your `.env` file if you have `APP_DEBUG` enabled. +If true it will dump the exception using `dd()` php function. +If false it will return response with custom message. +Custom message contains file and line information. + +**You can use all other functions with this except `response()`.** + +Usage: + +```php +try { + // code +} catch (\Exception $exception) { + return ApiResponse::exception($exception); +} + +``` + +Response: +```json +{ + "data": null, + "code": null, + "errors": null, + "message": "Exception found in file C:\\projects\\project\\app\\Http\\Controllers\\v1\\UserController.php on line 33" +} +``` + You can also pass message in your response by adding `message()` function before response function. +Usage: + ```php return ApiResponse::message('Hello')->response(201); ``` @@ -149,11 +183,11 @@ Response: "data": [ { "id": 1, - "name": "Jhon Jhonson", + "name": "Jhon Jhonson" }, { "id": 2, - "name": "Patrick Jhonson", + "name": "Patrick Jhonson" } ], "links": { diff --git a/composer.json b/composer.json index 6d2d04a..6064aac 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,13 @@ "Wame\\ApiResponse\\": "src/" } }, + "extra": { + "laravel": { + "providers": [ + "Wame\\ApiResponse\\LaravelApiResponseServiceProvider" + ] + } + }, "minimum-stability": "dev", "prefer-stable": true } diff --git a/src/Helpers/ApiResponse.php b/src/Helpers/ApiResponse.php index 939e71d..ed77249 100644 --- a/src/Helpers/ApiResponse.php +++ b/src/Helpers/ApiResponse.php @@ -159,4 +159,13 @@ public static function response(int $statusCode = 200): \Illuminate\Http\JsonRes return response()->json($response)->setStatusCode($statusCode); } + + public static function exception(\Exception $exception): \Illuminate\Http\JsonResponse + { + if (env('app_debug')) dd($exception); + + self::$message = __('wamesk-api-response.exception-message', ['file' => $exception->getFile(), 'line' => $exception->getLine()]); + + return self::response(500); + } } diff --git a/src/LaravelApiResponseServiceProvider.php b/src/LaravelApiResponseServiceProvider.php new file mode 100644 index 0000000..ffc99d4 --- /dev/null +++ b/src/LaravelApiResponseServiceProvider.php @@ -0,0 +1,28 @@ +publishTranslations(); + } + + /** + * @return void + */ + protected function registerTranslations(): void + { + $this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'wame-auth'); + } +} diff --git a/src/resources/lang/en/wamesk-api-response.php b/src/resources/lang/en/wamesk-api-response.php new file mode 100644 index 0000000..3ddb7ab --- /dev/null +++ b/src/resources/lang/en/wamesk-api-response.php @@ -0,0 +1,5 @@ + "Exception found in file :file on line :line", +]; diff --git a/src/resources/lang/sk/wamesk-api-response.php b/src/resources/lang/sk/wamesk-api-response.php new file mode 100644 index 0000000..5ede2f0 --- /dev/null +++ b/src/resources/lang/sk/wamesk-api-response.php @@ -0,0 +1,5 @@ + "Chyba nájdená v súbore :file na riadku :line", +];