[250320] TIL

였늘 ν•œ 일

μŠ€ν”„λ§ λΆ€νŠΈ μ½”λ“œ μž‘μ—…

  • post κ΄€λ ¨ CRUD API μž‘μ„±
  • post μ—μ„œ λŒ“κΈ€ μžˆλŠ” κ²Œμ‹œκΈ€μ€ 계속 였λ₯˜κ°€ λ‚¬μ—ˆλŠ”λ°, μœ μ € id 값이 잘λͺ» λ“€μ–΄κ°”μ—ˆλ‹€.

AOP 곡뢀

  • μ˜ˆμ™Έ 처리 곡톡 λ‘œμ§μ„ λ”°λ‘œ λΉΌμ„œ μ²˜λ¦¬ν•˜λŠ” 방법
    @RestControllerAdvice
    @Slf4j
    public class GlobalExceptionHandler {
    
      @ExceptionHandler(CustomException.class)
      public ResponseEntity<ApiResponse> handleCustomException(CustomException e) {
          return ResponseEntity
                  .status(e.getErrorCode().getStatus())
                  .body(new ApiResponse("invalid_request", false, null));
      }
    
      @ExceptionHandler(Exception.class)
      public ResponseEntity<ApiResponse> handleException(Exception e) {
          String errorLog = getStackTraceAsString(e);
    
          log.error("[ERROR] : {}\n{}", e.getMessage(), errorLog);
          return ResponseEntity
                  .status(HttpStatus.INTERNAL_SERVER_ERROR)
                  .body(new ApiResponse("internal_server_error", false,null));
      }
    
      private String getStackTraceAsString(Throwable e) {
          StringWriter stringWriter = new StringWriter();
          PrintWriter printWriter = new PrintWriter(stringWriter);
          e.printStackTrace(printWriter);
          return stringWriter.toString();
      }
    }
    

Categories:

Updated:

Leave a comment