Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.UMC.domain.test.controller;

import com.example.UMC.domain.user.exception.code.UserErrorCode;
import com.example.UMC.global.apiPayload.ApiResponse;
import com.example.UMC.global.apiPayload.code.GeneralSucessCode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class TestController {

//성공
@GetMapping("sucess")
public ApiResponse<String> sucessTest() {
return ApiResponse.onSucess(GeneralSucessCode.OK, "요청 정상 처리");
}

//실패
@GetMapping("fail")
public ApiResponse<Void> failTest() {
return ApiResponse.onFailure(UserErrorCode.USER_NOT_FOUND, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.UMC.domain.user.exception;

import com.example.UMC.global.apiPayload.code.BaseErrorCode;
import com.example.UMC.global.apiPayload.exception.GeneralException;

public class UserException extends GeneralException {
public UserException(BaseErrorCode code) {
super(code);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.UMC.domain.user.exception.code;

import com.example.UMC.global.apiPayload.code.BaseErrorCode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
@AllArgsConstructor
public enum UserErrorCode implements BaseErrorCode {
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "USER4001", "해당 사용자를 찾을 수 없습니다."),
DUPLICATE_EMAIL(HttpStatus.BAD_REQUEST, "USER4002","이미 존재하는 이메일입니다.");

private final HttpStatus status;
private final String code;
private final String message;
}
25 changes: 25 additions & 0 deletions src/main/java/com/example/UMC/global/apiPayload/ApiResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.UMC.global.apiPayload;

import com.example.UMC.global.apiPayload.code.BaseErrorCode;
import com.example.UMC.global.apiPayload.code.BaseSucessCode;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class ApiResponse<T> {
private final Boolean isSucess;
private final String code;
private final String message;
private final T result;

// 성공 응답
public static <T> ApiResponse<T> onSucess(BaseSucessCode code, T result){
return new ApiResponse<>(true, code.getCode(), code.getMessage(), result);
}

//실패 응답
public static <T> ApiResponse<T> onFailure(BaseErrorCode code, T result) {
return new ApiResponse<>(false, code.getCode(), code.getMessage(), result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.UMC.global.apiPayload.code;

import org.springframework.http.HttpStatus;

public interface BaseErrorCode {
HttpStatus getStatus();
String getCode();
String getMessage();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.UMC.global.apiPayload.code;

public interface BaseSucessCode {
String getCode();
String getMessage();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.UMC.global.apiPayload.code;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
@AllArgsConstructor
public enum GeneralErrorCode implements BaseErrorCode {
BAD_REQUEST(HttpStatus.BAD_REQUEST, "COMMON400", "잘못된 요청입니다."),
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "COMMON401", "인증이 필요합니다."),
FORBIDDEN(HttpStatus.FORBIDDEN, "COMMON403", "접근이 거부되었습니다."),
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "COMMON500", "서버 오류가 발생했습니다.");

private final HttpStatus status;
private final String code;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.UMC.global.apiPayload.code;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum GeneralSucessCode implements BaseSucessCode{
OK("COMMON200", "요청이 성공적으로 처리되었습니다."),
CREATED("COMMON201", "리소스가 성공적으로 생성되었습니다.");

private final String code;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.UMC.global.apiPayload.exception;

import com.example.UMC.global.apiPayload.code.BaseErrorCode;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class GeneralException extends RuntimeException{

private final BaseErrorCode code;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.UMC.global.apiPayload.handler;

import com.example.UMC.global.apiPayload.ApiResponse;
import com.example.UMC.global.apiPayload.code.BaseErrorCode;
import com.example.UMC.global.apiPayload.code.GeneralErrorCode;
import com.example.UMC.global.apiPayload.exception.GeneralException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GeneralExceptionAdvice {

// 애플리케이션에서 발생하는 커스텀 예외를 처리(내가 만든 예외)
@ExceptionHandler(GeneralException.class)
public ResponseEntity<ApiResponse<Void>> handleException(GeneralException ex) {
return ResponseEntity.status(ex.getCode().getStatus())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

우리 열심히 만들었던 공통응답을 활용해보아요 😢

.body(ApiResponse.onFailure(ex.getCode(), null));
}
// 그 외의 정의되지 않은 모든 예외 처리(스프링 내부에서 처리)
@ExceptionHandler(Exception.class)
public ResponseEntity<ApiResponse<String>> handleException(Exception ex) {
BaseErrorCode code = GeneralErrorCode.INTERNAL_SERVER_ERROR;
return ResponseEntity.status(code.getStatus())
.body(ApiResponse.onFailure(code, ex.getMessage()));
}
}