fix : 오류 발생시 json으로 값 전달 및 Swagger 문서 수정
This commit is contained in:
@@ -2,7 +2,14 @@
|
|||||||
|
|
||||||
namespace App\Exceptions;
|
namespace App\Exceptions;
|
||||||
|
|
||||||
|
use Illuminate\Auth\AuthenticationException;
|
||||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
use Illuminate\Support\Facades\Http;
|
use Illuminate\Support\Facades\Http;
|
||||||
|
|
||||||
@@ -37,4 +44,79 @@ protected function sendSlackException(Throwable $e): void
|
|||||||
logger()->error('슬랙 전송 실패', ['message' => $ex->getMessage()]);
|
logger()->error('슬랙 전송 실패', ['message' => $ex->getMessage()]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function render($request, Throwable $exception)
|
||||||
|
{
|
||||||
|
|
||||||
|
if ($request->expectsJson()) {
|
||||||
|
// 400 Bad Request (예: ValidationException)
|
||||||
|
if (
|
||||||
|
$exception instanceof ValidationException ||
|
||||||
|
$exception instanceof BadRequestHttpException
|
||||||
|
) {
|
||||||
|
return response()->json([
|
||||||
|
'success' => false,
|
||||||
|
'message' => '필수 파라미터 누락',
|
||||||
|
'data' => null,
|
||||||
|
], 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 401 Unauthorized
|
||||||
|
if ($exception instanceof AuthenticationException) {
|
||||||
|
return response()->json([
|
||||||
|
'success' => false,
|
||||||
|
'message' => '인증 실패',
|
||||||
|
'data' => null,
|
||||||
|
], 401);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 403 Forbidden
|
||||||
|
if ($exception instanceof AccessDeniedHttpException) {
|
||||||
|
return response()->json([
|
||||||
|
'success' => false,
|
||||||
|
'message' => '권한 없음',
|
||||||
|
'data' => null,
|
||||||
|
], 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 404 Not Found
|
||||||
|
if ($exception instanceof NotFoundHttpException) {
|
||||||
|
return response()->json([
|
||||||
|
'success' => false,
|
||||||
|
'message' => '존재하지 않는 URI 또는 데이터',
|
||||||
|
'data' => null,
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 405 Method Not Allowed
|
||||||
|
if ($exception instanceof MethodNotAllowedHttpException) {
|
||||||
|
return response()->json([
|
||||||
|
'success' => false,
|
||||||
|
'message' => '허용되지 않는 메서드',
|
||||||
|
'data' => null,
|
||||||
|
], 405);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 500 Internal Server Error (기타 모든 에러)
|
||||||
|
if (
|
||||||
|
$exception instanceof HttpException &&
|
||||||
|
$exception->getStatusCode() === 500
|
||||||
|
) {
|
||||||
|
return response()->json([
|
||||||
|
'success' => false,
|
||||||
|
'message' => '서버 에러',
|
||||||
|
'data' => null,
|
||||||
|
], 500);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 그 외 모든 예외
|
||||||
|
return response()->json([
|
||||||
|
'success' => false,
|
||||||
|
'message' => '서버 에러',
|
||||||
|
'data' => null,
|
||||||
|
], 500);
|
||||||
|
}
|
||||||
|
|
||||||
|
return parent::render($request, $exception);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,13 +85,6 @@ class MemberController extends Controller
|
|||||||
* )
|
* )
|
||||||
* )
|
* )
|
||||||
* ),
|
* ),
|
||||||
*
|
|
||||||
* @OA\Response(response=400, description="필수 파라미터 누락"),
|
|
||||||
* @OA\Response(response=401, description="인증 실패"),
|
|
||||||
* @OA\Response(response=403, description="권한 없음")
|
|
||||||
* @OA\Response(response=404, description="존재하지 않는 URI 또는 데이터")
|
|
||||||
* @OA\Response(response=405, description="허용되지 않는 메서드")
|
|
||||||
* @OA\Response(response=500, description="서버 에러")
|
|
||||||
* )
|
* )
|
||||||
*/
|
*/
|
||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
* @OA\Info(
|
* @OA\Info(
|
||||||
* version="1.0.0",
|
* version="1.0.0",
|
||||||
* title="SAM API Documentation",
|
* title="SAM API Documentation",
|
||||||
* description="SAM(Semi-Automatics Management) API 입니다.",
|
* description="===============================<br><strong>[공통 에러 응답 포맷]</strong><br>400: 필수 파라미터 누락<br>401: 인증 실패<br>403: 권한 없음<br>404: 존재하지 않는 URI 또는 데이터<br>405: 허용되지 않는 메서드<br>500: 서버 에러<br><br><strong>모든 에러 응답 예시:</strong><br>{<br> "success": false,<br> "message": "에러 메시지",<br> "data": null<br>}<br>===============================",
|
||||||
* @OA\Contact(
|
* @OA\Contact(
|
||||||
* email="shine1324@gmail.com"
|
* email="shine1324@gmail.com"
|
||||||
* )
|
* )
|
||||||
|
|||||||
Reference in New Issue
Block a user