Skip to content

Commit

Permalink
feat: 공통 예외 구조 (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyubin989 committed Dec 17, 2023
1 parent 4932f0b commit bdcaa81
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.baro.common.exception;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

@RequiredArgsConstructor
public enum CommonRequestExceptionType implements RequestExceptionType {

MISSING_PARAMETER_EXCEPTION("Request parameter is empty", HttpStatus.BAD_REQUEST),
;

private final String errorMessage;
private final HttpStatus httpStatus;

@Override
public String errorMessage() {
return errorMessage;
}

@Override
public HttpStatus httpStatus() {
return httpStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.baro.common.exception;

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

@Slf4j
@RestControllerAdvice
class GlobalExceptionHandler {

@ExceptionHandler(value = {RequestException.class})
ResponseEntity<String> handleBadRequestException(RequestException e) {
log.warn("[handleRequestException throw RequestException : {}", e.getMessage());
RequestExceptionType exceptionType = e.exceptionType();
return ResponseEntity.status(exceptionType.httpStatus()).body(exceptionType.errorMessage());
}

@ExceptionHandler(Exception.class)
ResponseEntity<String> handleException(HttpServletRequest request, Exception e) {
log.error("[ERROR] Unexpected error occurred. FROM URI: {}, ", request.getRequestURI(), e);
return ResponseEntity.internalServerError().body("Unknown error occurred. Please try later");
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/baro/common/exception/RequestException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baro.common.exception;

abstract class RequestException extends RuntimeException{
abstract RequestExceptionType exceptionType();
}
10 changes: 10 additions & 0 deletions src/main/java/com/baro/common/exception/RequestExceptionType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.baro.common.exception;

import org.springframework.http.HttpStatus;

public interface RequestExceptionType {

String errorMessage();

HttpStatus httpStatus();
}

0 comments on commit bdcaa81

Please sign in to comment.