. */ namespace App\Http\Middleware; use Closure; use Symfony\Component\HttpKernel\Exception\HttpException; /** * Class JsonExceptions. */ class JsonExceptions { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { try { $response = $next($request); } catch (HttpException $e) { return \Response::json([ 'message' => $e->getMessage(), ], $e->getStatusCode()); } return $response; } }