Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
AnourValar committed Mar 17, 2024
1 parent 7efedc8 commit 84b763d
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 141 deletions.
141 changes: 0 additions & 141 deletions src/Exceptions/HandlerRenderTrait.php

This file was deleted.

108 changes: 108 additions & 0 deletions src/Providers/LaravelAtomServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
}

/**
Expand All @@ -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());
}
});
}
}

0 comments on commit 84b763d

Please sign in to comment.