Skip to content

Commit

Permalink
Merge branch 'master' into 'develop'
Browse files Browse the repository at this point in the history
Master

See merge request php/packages/api-exceptions!18
  • Loading branch information
Pooria Arab committed Jan 16, 2021
2 parents edf5c85 + 52a113e commit 6c790cb
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ overwrite `render` method of `App\Exceptions\Handler` like this :
public function render($request, Throwable $exception)
{
return ApiException::handle($exception)->render();
return ApiException::handle($exception);
}
```

Expand Down
24 changes: 18 additions & 6 deletions src/Contracts/ApiExceptionAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,33 @@ public function render()
{
$this->setCode($this->exception->getCode());
$this->setMessage($this->exception->getMessage());
$this->SetErrors();
$response = new FailureResponse($this->getCode(), $this->getMessage());
return $response
->setResponseKey('error')
->setResponseValue($this->getErrors())
->render();
->setResponseKey('error')
->setResponseValue($this->getErrors())
->render();
}

public function setErrors($errors)
/**
* @param $code
* @return $this
*/
public function setErrors($errors = [])
{
if (! is_array($errors)) {
$errors = [$errors];
}

$this->errors = array_merge($errors, $this->errors);

if (env('APP_DEBUG')) {
$this->errors= [
$this->errors = array_merge([
'trace' => $this->exception->getTrace(),
'line' => $this->exception->getLine(),
];
], $this->errors);
}

return $this;
}

Expand Down
12 changes: 6 additions & 6 deletions src/Exceptions/CustomValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function __construct(Exception $exception)
*/
public function setCode($code = null): self
{
$this->code = $code ?? Response::HTTP_BAD_REQUEST;

$this->code = ($code) ? $code : Response::HTTP_UNPROCESSABLE_ENTITY;
return $this;
}

Expand All @@ -49,14 +50,13 @@ public function setMessage($message = null)
* @param array $errors
* @return $this
*/
public function setErrors($errors = null)
public function setErrors($errors = [])
{
if (! empty($errors)) {
$this->errors = $errors;
return $this;
if (! is_array($errors)) {
$errors = [$errors];
}

$this->errors = $this->exception->validdator->getMessageBag()->all();
$this->errors = array_merge($this->exception->validator->getMessageBag()->messages(), $errors);
return $this;
}
}
2 changes: 1 addition & 1 deletion src/Handlers/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function handle(Throwable $exception)
$customException = static::getCustomException($exception);
$exceptionObject = (class_exists($customException)) ? new $customException($exception) : new CustomDefaultException($exception);

return $exceptionObject;
return $exceptionObject->render();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/ApiExceptionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ApiExceptionServiceProvider extends ServiceProvider
public function boot()
{
$this->publishes([
__DIR__ . '/config/exceptions.php' => config_path('exceptions.php'),
__DIR__ . '/../config/exceptions.php' => config_path('exceptions.php'),
]);
}
}
14 changes: 7 additions & 7 deletions tests/Feature/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function test_it_can_handle_unauthenticated_exception(): void
new CustomAuthenticationException(
new Exception($fakeTest, $code)
)
)->render();
);

self::assertInstanceOf(JsonResponse::class, $response);
self::assertEquals($fakeTest, $response->getData()->message);
Expand All @@ -46,7 +46,7 @@ public function test_it_can_handle_default_exception(): void

$response = ApiException::handle(
new Exception($fakeTest, $code)
)->render();
);

self::assertInstanceOf(JsonResponse::class, $response);
self::assertEquals($fakeTest, $response->getData()->message);
Expand All @@ -63,7 +63,7 @@ public function test_it_can_handle_model_not_found_exception(): void
new CustomModelNotFoundException(
new Exception($fakeTest, $code)
)
)->render();
);

self::assertInstanceOf(JsonResponse::class, $response);
self::assertEquals($fakeTest, $response->getData()->message);
Expand All @@ -80,7 +80,7 @@ public function test_it_can_handle_not_found_http_exception(): void
new CustomNotFoundHttpException(
new Exception($fakeTest, $code)
)
)->render();
);

self::assertInstanceOf(JsonResponse::class, $response);
self::assertEquals($fakeTest, $response->getData()->message);
Expand All @@ -97,7 +97,7 @@ public function test_it_can_handle_route_not_found_exception(): void
new CustomRouteNotFoundException(
new Exception($fakeTest, $code)
)
)->render();
);

self::assertInstanceOf(JsonResponse::class, $response);
self::assertEquals($fakeTest, $response->getData()->message);
Expand All @@ -114,7 +114,7 @@ public function test_it_can_handle_unauthorized_exception(): void
new CustomUnauthorizedException(
new Exception($fakeTest, $code)
)
)->render();
);

self::assertInstanceOf(JsonResponse::class, $response);
self::assertEquals($fakeTest, $response->getData()->message);
Expand All @@ -131,7 +131,7 @@ public function test_it_can_handle_unexpected_exception(): void
new CustomUnexpectedException(
new Exception($fakeTest, $code)
)
)->render();
);

self::assertInstanceOf(JsonResponse::class, $response);
self::assertEquals($fakeTest, $response->getData()->message);
Expand Down

0 comments on commit 6c790cb

Please sign in to comment.