diff --git a/src/Exceptions/HandlerRenderTrait.php b/src/Exceptions/HandlerRenderTrait.php deleted file mode 100644 index 3e9ba3c..0000000 --- a/src/Exceptions/HandlerRenderTrait.php +++ /dev/null @@ -1,141 +0,0 @@ - $e->getHeaders()['Retry-After']]); - - if ($request->expectsJson()) { - $response->setData(['message' => $e->getMessage(), 'errors' => ['error' => [$error]]]); - } else { - //return redirect()->back()->withErrors(['error' => $error])->withInput(); - } - - } - - // NotFoundHttpException, ModelNotFoundException - if ($e instanceof NotFoundHttpException || $e instanceof ModelNotFoundException) { - - if ($request->expectsJson()) { - $message = $e->getMessage(); - if (stripos($message, 'No query results') !== false) { - $message = 'No query results.'; - } - if (! $message) { - $message = 'Not Found.'; - } - - $response->setData(['message' => $message, 'errors' => []]); - } - - } - - // InvalidSignatureException - if ($e instanceof \Illuminate\Routing\Exceptions\InvalidSignatureException) { - - if ($request->expectsJson()) { - $response->setData(['message' => $e->getMessage(), 'errors' => []]); - } - - } - - // MethodNotAllowedHttpException - if ($e instanceof MethodNotAllowedHttpException) { - - if ($request->expectsJson()) { - $response->setData(['message' => ($e->getMessage() ?: 'Method Not Allowed.'), 'errors' => []]); - } - - } - - // AuthorizationException - if ($e instanceof \Illuminate\Auth\Access\AuthorizationException) { - - if ($request->expectsJson()) { - $response->setData(['message' => $e->getMessage(), 'errors' => ['error' => [trans($e->getMessage())]]]); - } - - } - - // AuthenticationException - if ($e instanceof \Illuminate\Auth\AuthenticationException) { - - if ($request->expectsJson()) { - $response->setData(['message' => $e->getMessage(), 'errors' => []]); - } - - } - - // HttpException => verified - if ($e instanceof HttpException && $e->getMessage() == 'Your email address is not verified.') { - - if ($request->expectsJson()) { - $response->setData(['message' => $e->getMessage(), 'errors' => ['error' => [trans($e->getMessage())]]]); - } - - } - - // TokenMismatchException - if ($e instanceof TokenMismatchException) { - - if ($request->expectsJson()) { - $response->setData(['message' => $e->getMessage(), 'errors' => []]); - } - - } - - // JsonEncodingException [on json columns], InvalidArgumentException [on json content-type], QueryException [etc] - if ( - $e instanceof JsonEncodingException - || ($e instanceof \InvalidArgumentException && stripos($e->getMessage(), 'Malformed UTF-8 characters') !== false) - || ($e instanceof QueryException && stripos($e->getMessage(), 'invalid byte sequence for encoding "UTF8"') !== false) - ) { - - if ($request->expectsJson()) { - return response(['message' => 'Malformed UTF-8 characters, possibly incorrectly encoded.'], 400); - } else { - return response('Malformed UTF-8 characters, possibly incorrectly encoded.', 400); - } - - } - - // MassAssignmentException - if ($e instanceof \Illuminate\Database\Eloquent\MassAssignmentException) { - - if ($request->expectsJson()) { - $response->setStatusCode(400)->setData(['message' => $e->getMessage(), 'errors' => []]); - } - - } - - - return $response; - } -} diff --git a/src/Providers/LaravelAtomServiceProvider.php b/src/Providers/LaravelAtomServiceProvider.php index 22f36e1..7721ab0 100644 --- a/src/Providers/LaravelAtomServiceProvider.php +++ b/src/Providers/LaravelAtomServiceProvider.php @@ -3,6 +3,13 @@ namespace AnourValar\LaravelAtom\Providers; use Illuminate\Support\ServiceProvider; +use Illuminate\Database\Eloquent\JsonEncodingException; +use Illuminate\Database\Eloquent\ModelNotFoundException; +use Illuminate\Database\QueryException; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; +use Illuminate\Auth\Access\AuthorizationException; +use Symfony\Component\HttpKernel\Exception\HttpException; class LaravelAtomServiceProvider extends ServiceProvider { @@ -19,6 +26,9 @@ public function register() $this->app->singleton(\AnourValar\LaravelAtom\Service::class, function ($app) { return new \AnourValar\LaravelAtom\Service(new \AnourValar\LaravelAtom\Registry()); }); + + // Exception handler + $this->exceptionHandler(); } /** @@ -38,4 +48,102 @@ public function boot() // migrations $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); } + + /** + * @return void + */ + private function exceptionHandler() + { + $exceptionHandler = resolve(\Illuminate\Contracts\Debug\ExceptionHandler::class); + + // ThrottleRequestsException + $exceptionHandler->renderable(function (\Illuminate\Http\Exceptions\ThrottleRequestsException $e, $request) { + $error = trans('laravel-atom::exception.throttle', ['seconds' => $e->getHeaders()['Retry-After']]); + + if ($request->expectsJson()) { + return response()->json(['message' => $e->getMessage(), 'errors' => ['error' => [$error]]], $e->getStatusCode()); + } else { + //return redirect()->back()->withErrors(['error' => $error])->withInput(); + } + }); + + // NotFoundHttpException, ModelNotFoundException + $exceptionHandler->renderable(function (NotFoundHttpException|ModelNotFoundException $e, $request) { + if ($request->expectsJson()) { + $message = $e->getMessage(); + if (stripos($message, 'No query results') !== false) { + $message = 'No query results.'; + } + if (! $message) { + $message = 'Not Found.'; + } + + return response()->json(['message' => $message, 'errors' => []], $e->getStatusCode()); + } + }); + + // InvalidSignatureException + $exceptionHandler->renderable(function (\Illuminate\Routing\Exceptions\InvalidSignatureException $e, $request) { + if ($request->expectsJson()) { + return response()->json(['message' => $e->getMessage(), 'errors' => []], $e->getStatusCode()); + } + }); + + // MethodNotAllowedHttpException + $exceptionHandler->renderable(function (\Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException $e, $request) { + if ($request->expectsJson()) { + return response()->json(['message' => ($e->getMessage() ?: 'Method Not Allowed.'), 'errors' => []], $e->getStatusCode()); + } + }); + + // AuthorizationException + $exceptionHandler->renderable(function (AuthorizationException|AccessDeniedHttpException $e, $request) { + if ($request->expectsJson()) { + return response()->json(['message' => $e->getMessage(), 'errors' => ['error' => [trans($e->getMessage())]]], $e->getStatusCode()); + } + }); + + // AuthenticationException + $exceptionHandler->renderable(function (\Illuminate\Auth\AuthenticationException $e, $request) { + if ($request->expectsJson()) { + return response()->json(['message' => $e->getMessage(), 'errors' => []], 401); + } + }); + + // HttpException => verified + $exceptionHandler->renderable(function (HttpException $e, $request) { + if ($request->expectsJson() && $e->getMessage() == 'Your email address is not verified.') { + return response()->json(['message' => $e->getMessage(), 'errors' => ['error' => [trans($e->getMessage())]]], $e->getStatusCode()); + } + }); + + // TokenMismatchException + $exceptionHandler->renderable(function (TokenMismatchException|HttpException $e, $request) { + if ($request->expectsJson() && stripos($e->getMessage(), 'token mismatch') !== false) { + return response()->json(['message' => $e->getMessage(), 'errors' => []], $e->getStatusCode()); + } + }); + + // JsonEncodingException [on json columns], InvalidArgumentException [on json content-type], QueryException [etc] + $exceptionHandler->renderable(function (JsonEncodingException|\InvalidArgumentException|QueryException $e, $request) { + if ( + $e instanceof JsonEncodingException + || ($e instanceof \InvalidArgumentException && stripos($e->getMessage(), 'Malformed UTF-8 characters') !== false) + || ($e instanceof QueryException && stripos($e->getMessage(), 'invalid byte sequence for encoding "UTF8"') !== false) + ) { + if ($request->expectsJson()) { + return response(['message' => 'Malformed UTF-8 characters, possibly incorrectly encoded.'], 400); + } else { + return response('Malformed UTF-8 characters, possibly incorrectly encoded.', 400); + } + } + }); + + // MassAssignmentException + $exceptionHandler->renderable(function (\Illuminate\Database\Eloquent\MassAssignmentException $e, $request) { + if ($request->expectsJson()) { + return response()->json(['message' => $e->getMessage(), 'errors' => []], $e->getStatusCode()); + } + }); + } }