JSON APIリクエスト時に例外が発生したら、常にJSONでレスポンスする
This commit is contained in:
		@@ -4,7 +4,10 @@ namespace App\Exceptions;
 | 
			
		||||
 | 
			
		||||
use Exception;
 | 
			
		||||
use Illuminate\Auth\AuthenticationException;
 | 
			
		||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
 | 
			
		||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
 | 
			
		||||
use Illuminate\Http\JsonResponse;
 | 
			
		||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 | 
			
		||||
 | 
			
		||||
class Handler extends ExceptionHandler
 | 
			
		||||
{
 | 
			
		||||
@@ -68,4 +71,28 @@ class Handler extends ExceptionHandler
 | 
			
		||||
 | 
			
		||||
        return redirect()->guest(route('login'));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function prepareException(Exception $e)
 | 
			
		||||
    {
 | 
			
		||||
        if (!config('app.debug') && $e instanceof ModelNotFoundException) {
 | 
			
		||||
            return new NotFoundHttpException('Resource not found.', $e);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return parent::prepareException($e);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected function prepareJsonResponse($request, Exception $e)
 | 
			
		||||
    {
 | 
			
		||||
        $status = $this->isHttpException($e) ? $e->getStatusCode() : 500;
 | 
			
		||||
 | 
			
		||||
        return new JsonResponse(
 | 
			
		||||
            [
 | 
			
		||||
                'status' => $status,
 | 
			
		||||
                'error' => $this->convertExceptionToArray($e),
 | 
			
		||||
            ],
 | 
			
		||||
            $status,
 | 
			
		||||
            $this->isHttpException($e) ? $e->getHeaders() : [],
 | 
			
		||||
            JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -40,6 +40,7 @@ class Kernel extends HttpKernel
 | 
			
		||||
 | 
			
		||||
        'api' => [
 | 
			
		||||
            'throttle:60,1',
 | 
			
		||||
            \App\Http\Middleware\EnforceJson::class,
 | 
			
		||||
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
 | 
			
		||||
        ],
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										25
									
								
								app/Http/Middleware/EnforceJson.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								app/Http/Middleware/EnforceJson.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Http\Middleware;
 | 
			
		||||
 | 
			
		||||
use Closure;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Request headerに Accept: application/json を上書きする。APIエンドポイント用。
 | 
			
		||||
 */
 | 
			
		||||
class EnforceJson
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * Handle an incoming request.
 | 
			
		||||
     *
 | 
			
		||||
     * @param  \Illuminate\Http\Request  $request
 | 
			
		||||
     * @param  \Closure  $next
 | 
			
		||||
     * @return mixed
 | 
			
		||||
     */
 | 
			
		||||
    public function handle($request, Closure $next)
 | 
			
		||||
    {
 | 
			
		||||
        $request->headers->set('Accept', 'application/json');
 | 
			
		||||
 | 
			
		||||
        return $next($request);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user