Skip to content

Commit

Permalink
Merge pull request #5 from soulive-A/feature/#4-exception
Browse files Browse the repository at this point in the history
[feat] 예외처리 환경 구축
  • Loading branch information
lsm-del authored Mar 2, 2024
2 parents 1d44565 + 70bb951 commit 24afc2c
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.backend.soullive_a.controller;

import com.backend.soullive_a.exception.base.BaseResponse;
import com.backend.soullive_a.exception.custom.NotFoundUserException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ExceptionController {
@GetMapping("/success")
public BaseResponse<Object> getUser() {
return BaseResponse.builder()
.isSuccess(true)
.code(200)
.message("사용자를 찾았습니다")
.data("somin")
.build();
}

@GetMapping("/fail")
public BaseResponse<Object> notFoundUser() {
throw new NotFoundUserException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class MainController {
public String apiTest() {
System.out.println("health-check!");
return "health check! v2.";

}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.backend.soullive_a.exception.base;

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

@Getter
public class BaseException extends RuntimeException {

private final int errorCode;
private final String message;
private final HttpStatus httpStatus;

public BaseException(ErrorCode errorCode) {
super();
this.errorCode = errorCode.getCode();
this.message = errorCode.getMessage();
this.httpStatus = errorCode.getHttpStatus();
}

public BaseException(ErrorCode errorCode, String message) {
super();
this.errorCode = errorCode.getCode();
this.message = message;
this.httpStatus = errorCode.getHttpStatus();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.backend.soullive_a.exception.base;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.ExceptionHandler;

@Getter
@NoArgsConstructor
public class BaseResponse<T> {

private boolean isSuccess;
private int code;
private String message;
private T data;

@Builder
public BaseResponse(boolean isSuccess, int code, String message, T data) {
this.isSuccess = isSuccess;
this.code = code;
this.message = message;
this.data = data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.backend.soullive_a.exception.base;

import jakarta.validation.ValidationException;
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 CustomExceptionHandler extends ResponseEntityExceptionHandler {
/**
* Custom Exception 핸들링
*/
@ExceptionHandler(BaseException.class)
public ResponseEntity<Object> onBaseException(BaseException exception) {
return new ResponseEntity<>(new BaseResponse<>(
false,
exception.getErrorCode(),
exception.getMessage(),
null),
null,
exception.getHttpStatus());
}

/**
* 클라이언트로부터 넘어오는 값 Validation Exception 핸들링
*/
@ExceptionHandler(ValidationException.class)
public ResponseEntity<Object> onValidationException(Exception exception) {
return new ResponseEntity<>(new BaseResponse<>(
false,
400,
exception.getMessage(),
null),
null,
HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(Exception.class)
public ResponseEntity<Object> onException(Exception exception) {
return new ResponseEntity<>(new BaseResponse<>(
false,
500,
exception.getMessage(),
null),
null,
HttpStatus.INTERNAL_SERVER_ERROR);
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/backend/soullive_a/exception/base/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.backend.soullive_a.exception.base;

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

@Getter
public enum ErrorCode {
// 성공
SUCCESS(true, 1000, "요청에 성공했습니다.", HttpStatus.OK),

//실패 - 필요시 만들어 쓰세요
/**
* 2000 : User 오류
*/
NOT_FOUND_USER(false, 2001, "사용자를 찾을 수 없습니다", HttpStatus.NOT_FOUND);

private final boolean isSuccess;
private final int code;
private final String message;
private final HttpStatus httpStatus;

ErrorCode(boolean isSuccess, int code, String message, HttpStatus httpStatus) {
this.isSuccess = isSuccess;
this.code = code;
this.message = message;
this.httpStatus = httpStatus;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.backend.soullive_a.exception.custom;

import com.backend.soullive_a.exception.base.BaseException;
import com.backend.soullive_a.exception.base.ErrorCode;
import lombok.Getter;

@Getter
public class NotFoundUserException extends BaseException {

public NotFoundUserException() {
super(ErrorCode.NOT_FOUND_USER);
}

public NotFoundUserException(String message) {
super(ErrorCode.NOT_FOUND_USER, message);
}
}

0 comments on commit 24afc2c

Please sign in to comment.