Skip to content

Commit

Permalink
Added function exception()
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianzofcin committed Apr 13, 2023
1 parent 237bc86 commit f379b3d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 2 deletions.
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```
Expand All @@ -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);
```
Expand Down Expand Up @@ -149,11 +183,11 @@ Response:
"data": [
{
"id": 1,
"name": "Jhon Jhonson",
"name": "Jhon Jhonson"
},
{
"id": 2,
"name": "Patrick Jhonson",
"name": "Patrick Jhonson"
}
],
"links": {
Expand Down
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
"Wame\\ApiResponse\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Wame\\ApiResponse\\LaravelApiResponseServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
9 changes: 9 additions & 0 deletions src/Helpers/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
28 changes: 28 additions & 0 deletions src/LaravelApiResponseServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types = 1);

namespace Wame\ApiResponse;

use Illuminate\Support\ServiceProvider;

class LaravelApiResponseServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
* @return void
*/
public function boot():void
{
// Export translations
$this->publishTranslations();
}

/**
* @return void
*/
protected function registerTranslations(): void
{
$this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'wame-auth');
}
}
5 changes: 5 additions & 0 deletions src/resources/lang/en/wamesk-api-response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
"exception-message" => "Exception found in file :file on line :line",
];
5 changes: 5 additions & 0 deletions src/resources/lang/sk/wamesk-api-response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
"exception-message" => "Chyba nájdená v súbore :file na riadku :line",
];

0 comments on commit f379b3d

Please sign in to comment.