. */ namespace Poniverse\Ponyfm\Http\Middleware; use Closure; use Symfony\Component\HttpKernel\Exception\HttpException; /** * Class JsonExceptions * @package Poniverse\Ponyfm\Http\Middleware * * This middleware turns any HTTP exceptions thrown during the request * into a JSON response. To be used when implementing the API! */ 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; } }