Skip to content

Instantly share code, notes, and snippets.

@osibg
osibg / Handler.php
Last active June 20, 2021 06:27
Handler.php
<?php
namespace App\Exceptions;
use App\Exceptions\Handlers;
use Throwable;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\QueryException;
use Illuminate\Http\JsonResponse;
@osibg
osibg / AuthenticationException.php
Last active June 20, 2021 06:27
Handlers for Exceptions
<?php
namespace App\Exceptions\Handlers;
use Illuminate\Http\JsonResponse;
class AuthenticationException
{
public function __invoke(\Exception $exception): ?JsonResponse
{
return response()->json(['error' => 'Unauthorized: Access is denied due to invalid credentials.'], 401);
@osibg
osibg / Handler.php
Created June 16, 2020 06:04
Laravel default Exception Handler
<?php
namespace App\Exceptions;
class Handler extends \Laravel\Lumen\Exceptions\Handler
{
public function render($request, Throwable $exception)
{
if ($exception instanceof CustomException) {
return response()->json(['error' => 'Oops!'], 400);
}
@osibg
osibg / RenderException.php
Created June 16, 2020 06:05
Custom exception with own rendering method
<?php
namespace App\Exceptions;
class RenderException extends \Exception
{
public function report()
{
// overwrite default report/log behavior
}