Skip to content

Commit

Permalink
feat: cutsom Exception handle - #106
Browse files Browse the repository at this point in the history
  • Loading branch information
sjk4618 committed Jan 21, 2025
1 parent 6cafe43 commit acd3fce
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.cakey.rescode.ErrorCode;
import com.cakey.exception.CakeBaseException;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public abstract class CakeApiBaseException extends CakeBaseException { ///abstract ์ด์œ  : ์ถ”์ƒ ํด๋ž˜์Šค๋กœ ์ƒ์†๋ฐ›๋Š” ์ž์‹ ํด๋ž˜์Šค๊ฐ€ ๊ตฌ์ฒด์ ์ธ ๊ตฌํ˜„์„ ์ œ๊ณตํ•˜๋„๋ก ๊ฐ•์ œ
private final ErrorCode errorCode;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cakey.common.response;

import com.cakey.rescode.ErrorBaseCode;
import com.cakey.rescode.ErrorCode;
import com.cakey.rescode.SuccessCode;
import org.springframework.http.ResponseEntity;

Expand All @@ -15,13 +16,13 @@ public static <T> ResponseEntity<BaseResponse<?>> success(final SuccessCode succ
.body(BaseResponse.of(successCode, data));
}

public static ResponseEntity<BaseResponse<?>> failure(final ErrorBaseCode errorBaseCode) {
public static ResponseEntity<BaseResponse<?>> failure(final ErrorCode errorBaseCode) {
return ResponseEntity.status(errorBaseCode.getHttpStatus())
.body(BaseResponse.of(errorBaseCode));
}

//๋”ฐ๋กœ error message ๋„ฃ์–ด์ค„ ๋•Œ, ์‚ฌ์šฉ
public static ResponseEntity<BaseResponse<?>> failure(final ErrorBaseCode errorBaseCode, final String message) {
public static ResponseEntity<BaseResponse<?>> failure(final ErrorCode errorBaseCode, final String message) {
return ResponseEntity
.status(errorBaseCode.getHttpStatus())
.body(BaseResponse.of(errorBaseCode.getCode(), message));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.cakey.exception.handler;

import com.cakey.cake.exception.CakeApiBaseException;
import com.cakey.rescode.ErrorBaseCode;
import com.cakey.common.response.ApiResponseUtil;
import com.cakey.common.response.BaseResponse;
import com.cakey.store.exception.StoreApiBaseException;
import com.cakey.user.exception.UserApiBaseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import jakarta.persistence.EntityNotFoundException;
import lombok.val;
Expand All @@ -28,19 +31,26 @@
@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(CakeApiBaseException.class)
public ResponseEntity<BaseResponse<?>> handleCakeApiBaseException(final CakeApiBaseException e) {
return ApiResponseUtil.failure(e.getErrorCode());
}

// /**
// * CakeyException
// */
// @ExceptionHandler(CakeyException.class)
// protected ResponseEntity<BaseResponse<?>> handleAgodaException(final AgodaException e) {
// return ApiResponseUtil.failure(e.getFailMessage());
// }
@ExceptionHandler(StoreApiBaseException.class)
public ResponseEntity<BaseResponse<?>> handleStoreApiBaseException(final StoreApiBaseException e) {
return ApiResponseUtil.failure(e.getErrorCode());
}

/**
* 400 - MethodArgumentNotValidException
* ๋ฐœ์ƒ ์ด์œ  : @Valid ๊ฒ€์ฆ ์‹คํŒจ (@Request Body)
*/
@ExceptionHandler(UserApiBaseException.class)
public ResponseEntity<BaseResponse<?>> handleUserApiBaseException(final UserApiBaseException e) {
return ApiResponseUtil.failure(e.getErrorCode());
}


/**
* 400 - MethodArgumentNotValidException
* ๋ฐœ์ƒ ์ด์œ  : @Valid ๊ฒ€์ฆ ์‹คํŒจ (@Request Body)
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<BaseResponse<?>> handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) {
// ๋ชจ๋“  ๊ฒ€์ฆ ์‹คํŒจ ๋ฉ”์‹œ์ง€๋ฅผ ์ถ”์ถœํ•˜๊ณ  ์ค„๋ฐ”๊ฟˆ์œผ๋กœ ์—ฐ๊ฒฐ
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.cakey.rescode.ErrorCode;
import com.cakey.exception.CakeBaseException;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public abstract class StoreApiBaseException extends CakeBaseException {
private final ErrorCode errorCode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.cakey.exception.CakeBaseException;
import com.cakey.rescode.ErrorCode;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public abstract class UserApiBaseException extends CakeBaseException {
private final ErrorCode errorCode;

Expand Down

0 comments on commit acd3fce

Please sign in to comment.