Skip to content

Commit

Permalink
feat: add custom exception. (#9)
Browse files Browse the repository at this point in the history
Custom exception 설정을 하기 위한, ErrorCode enum, CustomException, CustomExceptionHandler, ErrorDTO를 생성. CustomException 핸들링을 동작시키기 위한 Security 설정을 추가함.
CustomException 핸들링 동작을 확인할 수 있는 test api 추가 (api/develop/v1/unauthorized, api/develop/v1/bad-request)
  • Loading branch information
chea-young committed Dec 11, 2023
1 parent 83244ae commit 4dd3087
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/main/java/com/compono/ibackend/common/dto/ErrorDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.compono.ibackend.common.dto;

import com.compono.ibackend.common.enumType.ErrorCode;
import com.compono.ibackend.common.exception.CustomException;
import lombok.Builder;
import lombok.Data;
import org.springframework.http.ResponseEntity;

@Data
@Builder
public class ErrorDTO {

private Integer code;
private String msg;
private String detail;

public static ResponseEntity<ErrorDTO> toResponseEntity(CustomException ex) {
ErrorCode errorType = ex.getErrorCode();
String detail = ex.getDetail();

return ResponseEntity
.status(ex.getStatus())
.body(ErrorDTO.builder()
.code(errorType.getCode())
.msg(errorType.getMsg())
.detail(detail)
.build());
}
}

18 changes: 18 additions & 0 deletions src/main/java/com/compono/ibackend/common/enumType/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.compono.ibackend.common.enumType;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum ErrorCode {
// 400
UNKNOWN(4000, "알 수 없는 에러가 발생했습니다."),
INVALID_PASSWORD(4001, "유효하지 않은 비밀번호 입니다."),

// 401
EXPIRED_TOKEN(4010, "만료된 토큰입니다.");

private final int code;
private final String msg;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.compono.ibackend.common.exception;

import com.compono.ibackend.common.enumType.ErrorCode;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
public class CustomException extends RuntimeException {
private final HttpStatus status;
private final ErrorCode errorCode;
private final String detail;

public CustomException(HttpStatus status, ErrorCode errorCode) {
this.status = status;
this.errorCode = errorCode;
this.detail = "";
}

public CustomException(HttpStatus status, ErrorCode errorCode, String detail) {
this.status = status;
this.errorCode = errorCode;
this.detail = detail;
}

public CustomException(HttpStatus status, ErrorCode errorCode, Throwable cause) {
this.status = status;
this.errorCode = errorCode;
this.detail = cause.getMessage();
}

public CustomException(HttpStatus status, CustomException customException) {
this.status = status;
this.errorCode = customException.getErrorCode();
this.detail = customException.getDetail();
}

public CustomException(HttpStatus status, Throwable cause) {
this.status = status;
this.errorCode = ErrorCode.UNKNOWN;
this.detail = cause.getMessage();
}

public CustomException(Exception exception) {
if (exception.getClass() == CustomException.class) {
CustomException customException = (CustomException) exception;
this.status = customException.getStatus();
this.errorCode = customException.getErrorCode();
this.detail = customException.getMessage();
} else {
this.status = HttpStatus.BAD_REQUEST;
this.errorCode = ErrorCode.UNKNOWN;
this.detail = exception.getMessage();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.compono.ibackend.common.handler;

import com.compono.ibackend.common.dto.ErrorDTO;
import com.compono.ibackend.common.exception.CustomException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class CustomExceptionHandler {

@ExceptionHandler(CustomException.class)
protected ResponseEntity<ErrorDTO> handleCustom400Exception(CustomException ex) {
return ErrorDTO.toResponseEntity(ex);
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/compono/ibackend/config/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.compono.ibackend.config;

import jakarta.servlet.DispatcherType;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@RequiredArgsConstructor
@EnableWebSecurity
@Configuration
public class SecurityConfig {

private static final String[] DEFAULT_WHITELIST = {
"/status", "/images/**", "/error/**"
};

private static final String[] DEVELOP_TEST_PATH = {
"api/develop/**",
};

@Bean
protected SecurityFilterChain config(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(request -> request
.dispatcherTypeMatchers(DispatcherType.FORWARD).permitAll()
.requestMatchers(DEFAULT_WHITELIST).permitAll()
.requestMatchers(DEVELOP_TEST_PATH).permitAll()
.anyRequest().authenticated()
);
return http.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.compono.ibackend.develop.controller;

import com.compono.ibackend.common.enumType.ErrorCode;
import com.compono.ibackend.common.exception.CustomException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("api/develop/")
public class DevelopController {

@GetMapping("v1/bad-request")
public ResponseEntity<Object> test400Error() {
try {
throw new CustomException(HttpStatus.BAD_REQUEST, ErrorCode.INVALID_PASSWORD);
} catch (Exception ex) {
throw new CustomException(ex);
}
}

@GetMapping("v1/unauthorized")
public ResponseEntity<Object> test401Error() {
try {
throw new CustomException(HttpStatus.UNAUTHORIZED, ErrorCode.EXPIRED_TOKEN);
} catch (Exception ex) {
throw new CustomException(ex);
}
}
}

0 comments on commit 4dd3087

Please sign in to comment.