验证器异常捕捉按规定格式返回响应

This commit is contained in:
古俊杰 2021-12-27 22:06:31 +08:00
parent 30b5456cc2
commit 473910fc6b

View File

@ -3,6 +3,7 @@
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Validation\ValidationException;
use Throwable;
class Handler extends ExceptionHandler
@ -37,5 +38,11 @@ class Handler extends ExceptionHandler
$this->reportable(function (Throwable $e) {
//
});
$this->renderable(function (ValidationException $e) {
return response()->json([
'code' => ErrorCode('ARGS'),
'error' => $e->errors()[0] ?? $e->getMessage()
], $e->status);
});
}
}