Merge branch 'develop' of https://gitee.com/zi-chunsheng-e-commerce/mall-server into develop
commit
38bcf4e9d0
|
|
@ -26,6 +26,10 @@ return (new Config())
|
||||||
'no_unused_imports' => true,
|
'no_unused_imports' => true,
|
||||||
'no_whitespace_before_comma_in_array' => true,
|
'no_whitespace_before_comma_in_array' => true,
|
||||||
'object_operator_without_whitespace' => true,
|
'object_operator_without_whitespace' => true,
|
||||||
|
'ordered_imports' => [
|
||||||
|
'imports_order' => ['class', 'function', 'const'],
|
||||||
|
'sort_algorithm' => 'alpha',
|
||||||
|
],
|
||||||
'single_quote' => true,
|
'single_quote' => true,
|
||||||
'trailing_comma_in_multiline' => [
|
'trailing_comma_in_multiline' => [
|
||||||
'elements' => ['arrays'],
|
'elements' => ['arrays'],
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
class BizException extends Exception
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 用于响应的 HTTP 状态代码
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
public $status = 400;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置用于响应的 HTTP 状态代码
|
||||||
|
*
|
||||||
|
* @param int $status
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function status(int $status)
|
||||||
|
{
|
||||||
|
$this->status = $status;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 报告异常
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function report()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,7 +2,11 @@
|
||||||
|
|
||||||
namespace App\Exceptions;
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
use Illuminate\Auth\AuthenticationException;
|
||||||
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
class Handler extends ExceptionHandler
|
class Handler extends ExceptionHandler
|
||||||
|
|
@ -34,8 +38,78 @@ class Handler extends ExceptionHandler
|
||||||
*/
|
*/
|
||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
$this->reportable(function (Throwable $e) {
|
$this->map(ModelNotFoundException::class, function ($e) {
|
||||||
//
|
$resource = __($key = 'models.'.$e->getModel());
|
||||||
|
|
||||||
|
if ($key === $resource) {
|
||||||
|
$resource = str_ireplace(['App\\Models\\Admin\\', 'App\\Models\\'], '', $e->getModel());
|
||||||
|
}
|
||||||
|
|
||||||
|
return new HttpException(404, __(':resource not found', ['resource' => $resource]), $e);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->renderable(function (BizException $e, $request) {
|
||||||
|
if ($this->shouldReturnJson($request, $e)) {
|
||||||
|
return response()->json($this->convertExceptionToArray($e), $e->status);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! config('app.debug')) {
|
||||||
|
$e = new HttpException($e->status, $e->getMessage(), $e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->prepareResponse($request, $e);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function unauthenticated($request, AuthenticationException $exception)
|
||||||
|
{
|
||||||
|
return $this->shouldReturnJson($request, $exception)
|
||||||
|
? response()->json([
|
||||||
|
'errcode' => 401,
|
||||||
|
'message' => $exception->getMessage(),
|
||||||
|
], 401)
|
||||||
|
: redirect()->guest($exception->redirectTo() ?? route('login'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function invalidJson($request, ValidationException $exception)
|
||||||
|
{
|
||||||
|
return response()->json([
|
||||||
|
'errcode' => 422,
|
||||||
|
'message' => $exception->getMessage(),
|
||||||
|
'errors' => $exception->errors(),
|
||||||
|
], $exception->status);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function convertExceptionToArray(Throwable $e)
|
||||||
|
{
|
||||||
|
return array_merge(
|
||||||
|
parent::convertExceptionToArray($e),
|
||||||
|
$this->isBizException($e) ? [
|
||||||
|
'errcode' => $e->getCode(),
|
||||||
|
'message' => $e->getMessage(),
|
||||||
|
] : [
|
||||||
|
'errcode' => $this->isHttpException($e) ? $e->getStatusCode() : 500,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 确认给定的异常是否是自定义业务异常
|
||||||
|
*
|
||||||
|
* @param Throwable $e
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function isBizException(Throwable $e): bool
|
||||||
|
{
|
||||||
|
return $e instanceof BizException;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
":resource not found": ":resource 未找到"
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
return [
|
||||||
|
// App\Models\User::class => '用户',
|
||||||
|
];
|
||||||
Loading…
Reference in New Issue