Skip to content

Commit e9a65af

Browse files
authored
Merge pull request #8 from hankz1108/main
Release v0.2.1
2 parents baa78fa + f046118 commit e9a65af

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

docs/en/11/index.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Run the Composer require command from the Terminal:
88
composer require hankz/laravel-plus-api
99
```
1010

11+
> [!TIP]
12+
> After Laravel 11, you may need to use `php artisan install:api` first to utilize the API.
13+
1114
### 2. Setup
1215

1316
This package supports Laravel's auto-discovery feature and it's ready to use once installed.
@@ -57,6 +60,14 @@ Set exception Handler.
5760
in `bootstrap/app.php`,add:
5861

5962
```php
63+
use Hankz\LaravelPlusApi\Classes\ApiResponseBuilder;
64+
use Illuminate\Auth\AuthenticationException;
65+
use Illuminate\Http\Request;
66+
use Illuminate\Validation\ValidationException;
67+
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
68+
69+
// other code...
70+
6071
->withExceptions(function (Exceptions $exceptions) {
6172
$exceptions->render(function (Throwable $throwable, Request $request) {
6273

src/Classes/ApiResponseBuilder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ public static function validationError(ValidationException $e): JsonResponse
2323
->build();
2424
}
2525

26+
/**
27+
* @param object|array|null $data
28+
*/
2629
public static function exceptionError(
2730
Throwable $throwable = null,
2831
string $apiCode = null,
2932
int $httpCode,
3033
string $message = null,
31-
string $data = null,
34+
mixed $data = null,
3235
array $headers = []
3336
): JsonResponse {
3437
$apiCode = $apiCode ?? config('laravel-plus-api.default_response.error.api_code');

src/Exceptions/ApiException.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@
1111

1212
abstract class ApiException extends Exception implements HttpExceptionInterface
1313
{
14+
// return api code, override if needed
1415
public const API_CODE = Response::HTTP_INTERNAL_SERVER_ERROR;
1516

17+
// return http code, override if needed
1618
public const HTTP_CODE = Response::HTTP_INTERNAL_SERVER_ERROR;
1719

20+
// return message, override if needed
21+
public const API_MESSAGE = '';
22+
1823
/**
1924
* message.
2025
*
@@ -33,13 +38,9 @@ public function __construct(string $message = null, $data = null)
3338
{
3439
parent::__construct($message);
3540

36-
if (!is_null($message)) {
37-
$this->message = $message;
38-
}
41+
$this->message = $message ?? static::API_MESSAGE;
3942

40-
if (!is_null($data)) {
41-
$this->data = $data;
42-
}
43+
$this->data = $data;
4344
}
4445

4546
/**

0 commit comments

Comments
 (0)