주요 변경사항: - MenusStep.php: 존재하지 않는 컬럼(code, route_name, depth, description) 제거 - MenusStep.php: 실제 DB 스키마 컬럼(hidden, is_external, external_url) 추가 - RecipeRegistry.php: MenusStep 비활성화 (하이브리드 메뉴 생성 방식 도입) - Handler.php: ValidationException 처리 개선 (실제 에러 메시지 표시, 422 상태 코드) 기술 세부사항: - 하이브리드 접근: TenantBootstrapper(데이터) + MenuBootstrapService(메뉴) - HTTP 상태 코드 표준화: 422 Unprocessable Entity (validation 실패) - 실제 검증 에러 메시지 반환: errors 객체에 필드별 에러 정보 포함
140 lines
4.6 KiB
PHP
140 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Exceptions;
|
|
|
|
use Illuminate\Auth\AuthenticationException;
|
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
|
use Illuminate\Support\Facades\Http;
|
|
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;
|
|
|
|
class Handler extends ExceptionHandler
|
|
{
|
|
public function report(Throwable $e): void
|
|
{
|
|
try {
|
|
if (
|
|
app()->environment('local') || app()->environment('production')
|
|
|| app()->environment('LOCAL') || app()->environment('DEV')
|
|
) {
|
|
$this->sendSlackException($e);
|
|
}
|
|
} catch (\Throwable $ex) {
|
|
// app()이 아직 사용 불가하거나 env 문제가 있을 때는 무시
|
|
}
|
|
|
|
parent::report($e); // 로그는 그대로
|
|
}
|
|
|
|
protected function sendSlackException(Throwable $e): void
|
|
{
|
|
try {
|
|
$url = env('LOG_SLACK_WEBHOOK_URL');
|
|
|
|
if (! $url) {
|
|
return;
|
|
}
|
|
|
|
$ip = request()?->ip() ?? 'N/A'; // 요청이 없는 경우 대비
|
|
|
|
Http::post($url, [
|
|
'text' => "*[Laravel] 예외 발생!*\n".
|
|
"• 메시지: `{$e->getMessage()}`\n".
|
|
"• 위치: `{$e->getFile()}:{$e->getLine()}`\n".
|
|
'• 시간: '.now()->toDateTimeString()."\n".
|
|
"• IP: `{$ip}`",
|
|
]);
|
|
} catch (Throwable $ex) {
|
|
logger()->error('슬랙 전송 실패', ['message' => $ex->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function render($request, Throwable $exception)
|
|
{
|
|
|
|
if ($request->expectsJson()) {
|
|
// 422 Unprocessable Entity - Validation 실패
|
|
if ($exception instanceof ValidationException) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => '입력값 검증 실패',
|
|
'data' => [
|
|
'errors' => $exception->errors(),
|
|
],
|
|
], 422);
|
|
}
|
|
|
|
// 400 Bad Request
|
|
if ($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);
|
|
}
|
|
}
|