Error message here!

Hide Error message here!

忘记密码?

Error message here!

请输入正确邮箱

Hide Error message here!

密码丢失?请输入您的电子邮件地址。您将收到一个重设密码链接。

Error message here!

返回登录

Close

Laravel8 custom error exception, such as {"message": "unauthenticated."}

wx5bffdbaf11b62 2021-07-26 11:17:34 阅读数:1 评论数:0 点赞数:0 收藏数:0

demand : Always return... In unauthorized status {“message“:“Unauthenticated.“}, Want to customize

Solution : stay app/Exceptions/Handler.php Add render Method ( That is, inherit the parent class render Method and customize )

 

<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
use Illuminate\Routing\Router;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Validation\ValidationException;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'current_password',
'password',
'password_confirmation',
];
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
$this->reportable(function (Throwable $e) {
//
});
}
/**
* Rewrite exception
* @param \Illuminate\Http\Request $request
* @param Throwable $e
* @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
* @throws \ReflectionException
*/
public function render($request, Throwable $e)
{
if (method_exists($e, 'render') && $response = $e->render($request)) {
return Router::toResponse($request, $response);
} elseif ($e instanceof Responsable) {
return $e->toResponse($request);
}
$e = $this->prepareException($this->mapException($e));
foreach ($this->renderCallbacks as $renderCallback) {
if (is_a($e, $this->firstClosureParameterType($renderCallback))) {
$response = $renderCallback($e, $request);
if (! is_null($response)) {
return $response;
}
}
}
if ($e instanceof HttpResponseException) {
return $e->getResponse();
} elseif ($e instanceof AuthenticationException) {
return apiResponse(403,$e->getMessage());
// return $this->unauthenticated($request, $e);
} elseif ($e instanceof ValidationException) {
return $this->convertValidationExceptionToResponse($e, $request);
}
return apiResponse(500, $e->getMessage());
// return $request->expectsJson()
// ? $this->prepareJsonResponse($request, $e)
// : $this->prepareResponse($request, $e);
}
}

 

After modification , Response information :

{
"code": 403,
"message": " unauthorized ",
"data": [ ]
}
{
"code": "500",
"message": "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ...",
"data": [ ]
}

 

Copyright statement
In this paper,the author:[wx5bffdbaf11b62],Reprint please bring the original link, thank you

编程之旅,人生之路,不止于编程,还有诗和远方。
阅代码原理,看框架知识,学企业实践;
赏诗词,读日记,踏人生之路,观世界之行;