๐Ÿ“ ์˜ค๋Š˜์˜ ๋‚ด์šฉ์ •๋ฆฌ

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์Šคํ„ฐ๋”” 6์ผ์ฐจ

ResponseEntity

ResponseEntity๋Š” ์Šคํ”„๋ง์—์„œ ์ œ๊ณตํ•˜๋Š” HttpEntity๋ฅผ ์ƒ์†๋ฐ›์•„ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค์ด๋‹ค.

์‘๋‹ต์„ ๋‚˜ํƒ€๋‚ด๋Š” ํด๋ž˜์Šค๋กœ, ์‘๋‹ต ๋ณธ๋ฌธ, ์ƒํƒœ์ฝ”๋“œ, ํ—ค๋”๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค.

ResponseEntity๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‘๋‹ต์„ ์ƒ์„ธํ•˜๊ฒŒ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์–ด์„œ ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„๊ฐ„์˜ ํ†ต์‹ ์„ ์œ ์—ฐํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

Custom Exception

ํ˜„์žฌ ์ฝ”๋“œ์—์„œ๋Š” ๊ฒŒ์‹œ๊ธ€์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ๋•Œ์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์„ ๋•Œ ์˜ˆ์™ธ์ƒํ™ฉ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ๋ชจ๋‘ 500์œผ๋กœ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌ๋œ๋‹ค. ์˜๋ฏธ์—†๋Š” ๋ฐ์ดํ„ฐ๋„ ๊ฐ™์ด ์ „๋‹ฌ๋˜๋Š” ์ƒํ™ฉ์ด๋‹ค.

์‚ฌ์ง„

๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ปค์Šคํ…€ ์˜ˆ์™ธ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์ž!

1. ErrorCode enum ์ƒ์„ฑ

@AllArgsConstructor
@Getter
public enum ErrorCode {
	INVALID_AUTH(HttpStatus.UNAUTHORIZED, "๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."),
	BOARD_NOT_FOUND(HttpStatus.NOT_FOUND, "๊ฒŒ์‹œ๊ธ€์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."),
	;

	private final HttpStatus httpStatus;
	private final String message;

}

2. Exception, ExceptionHandler ์ถ”๊ฐ€

@AllArgsConstructor
@Getter
public class CustomException extends RuntimeException{
	ErrorCode errorCode;
}
@ControllerAdvice
public class CustomExceptionHandler {

	@ExceptionHandler(CustomException.class)
	public ResponseEntity<ErrorResponseEntity> handleCustomException(CustomException e){
		return ErrorResponseEntity.toResponseEntity(e.getErrorCode());
	}
}

@ControllerAdvice

  • ์—ฌ๋Ÿฌ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์˜ˆ์™ธ๋ฅผ ์ผ๊ด„์ฒ˜๋ฆฌ ํ•œ๋‹ค.

@ExceptionHandler

  • @ControllerAdvice๊ฐ€ ์ ์šฉ๋œ ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ํŠน์ • ์˜ˆ์™ธ ํƒ€์ž…์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

3. ErrorResponseEntity ์ถ”๊ฐ€

@Data
@Builder
public class ErrorResponseEntity {
    private int status;
    private String name;
    private String message;

    public static ResponseEntity<ErrorResponseEntity> toResponseEntity(ErrorCode e){
        return ResponseEntity
                .status(e.getHttpStatus())
                .body(ErrorResponseEntity.builder()
	              		.status(e.getHttpStatus().value())
	                  .name(e.name())
	                  .message(e.getMessage())
	                  .build());
    }
}

4. ์‚ฌ์šฉ

private Board getBoardById(long id) {
	return boardRepository.findById(id).orElseThrow(() -> new CustomException(StatusEnum.BOARD_NOT_FOUND));
}

// ๋น„๋ฐ€ ๋ฒˆํ˜ธ ์ผ์น˜ ์—ฌ๋ถ€ ํ™•์ธ
private void passwordCheck(Board board, String inputPassword) {
	if (!board.getPassword().equals(inputPassword)) {
		throw new CustomException(StatusEnum.PASSWORD_NOT_MATCHED);
	}
}

5. ๊น”๋”!

image

๐Ÿ”š ์˜ค๋Š˜์˜ ๋งˆ๋ฌด๋ฆฌ

์Šคํ”„๋ง ๊ณผ์ œ๋ฅผ ๋งˆ๋ฌด๋ฆฌํ•˜๊ณ  ์ œ์ถœํ–ˆ๋‹ค. ์•„์ง๋„ Exception์„ ์ฒ˜๋ฆฌํ•˜๋Š”๊ฑด ๋‚ฏ์„ค๋‹ค. ๋‚ฏ์„ค์—ˆ๋˜ ์ฝ”๋“œ๋“ค์ด ์–ธ์  ๊ฐ€ ์ต์ˆ™ํ•ด์กŒ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ Exception ์ฒ˜๋ฆฌ๋„ ํ•˜๋‹ค๋ณด๋ฉด ์ต์ˆ™ํ•ด์ง€๋Š” ๋‚ ์ด ์˜ค์ง€ ์•Š์„๊นŒ? ๋ฌผ๋ก  ๊ทธ๋Ÿฐ ์ˆœ๊ฐ„์— ๋„๋‹ฌํ•˜๋ ค๋ฉด ๋”์šฑ๋” ์—ด์‹ฌํžˆ ํ•ด์•ผ๊ฒ ์ง€!