Skip to content

Commit

Permalink
Feature: custom error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Youthhing committed Jan 17, 2025
1 parent 9500edb commit 9c1a436
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.cotato.team2.team2.exception;

public record CustomErrorResponse(
int code,
String message,
String method,
String requestURI
) {
public static CustomErrorResponse of(int code, String message, String method, String requestURI) {
return new CustomErrorResponse(code, message, method, requestURI);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.cotato.team2.team2.exception;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

@ExceptionHandler(BusinessException.class)
public ResponseEntity<CustomErrorResponse> handleBusinessException(HttpServletRequest request, BusinessException e) {
log.error("BusinessException 발생: {} <{}>", e.getCode(), e.getMessage());
CustomErrorResponse errorResponse = CustomErrorResponse.of(e.getCode(), e.getMessage(), request.getMethod(), request.getRequestURI());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse);
}
}

0 comments on commit 9c1a436

Please sign in to comment.