반응형
가독성 높이는 습관 - 익셉션 핸들링 (2)
일반적인 서버의 요청 흐름
[ Client ] --요청→ ←응답-- [ Contoller --호출→ Function ]
{
"timestamp": "2021-08-17T18:06:52.061+00:00",
"stauts": 500,
"error": "Internal Server Error",
"path": "/foo"
}
@RequestMapping
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
HttpStatus status = getStatus(request);
if (status == HttpStatus.NO_CONTENT) {
return new ResponseEntity<>(status);
}
Map<String, Object> body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));
return new ResponseEntity<>(body, status);
}
익셉션 핸들링 실습
package com.example.zerobase;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfiugre.SpringBootApplication;
@SpringBootApplication
public class ZerobaseExceptionHandleApplication {
public static void main(String[] args) {
SpringApplication.run(ZerobaseExceptionHandleAppliation.class, args);
}
}
package com.example.zerobase;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ZerobaseController {
@GetMapping("/ping")
public String ping() {
return "pong";
}
@GetMapping("/exception")
public void error() {
throw new ZerobaseException(ExceptionCode.INTERNAL_ERROR, "변경된 에러 문구입니다.");
}
}
package com.example.zerobase;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = ZerobaseException.class)
public ResponseEntity<ZerobaseErrorResult> runtimeException(ZerobaseException e) {
ExceptionCode errorCode = e.getCode();
ZerobaseErrorResult result = ZerobaseErrorResult.builder()
.code(errorCode.name())
.message(e.getMessage())
.build();
return new ResponseEntity(result, errorCode.getHttpStatus());
}
}
package com.example.zerobase;
import lombok.Builder;
import lombok.Value;
@Value
@Builder
public class ZerobaseErrorResult {
private String code;
private String message;
}
package com.example.zerobase.exception;
import lombok.Getter;
@Getter
public class ZerobaseException extends RuntimeException {
private final ExceptionCode code;
public ZerobaseException(ExceptionCode code) {
this.code = code;
}
public ZerobaseException(String message, ExceptionCode code) {
super(message);
this.code = code;
}
public ZerobaseException(String message, Throwable cause, ExceptionCode code) {
super(message, cause);
this.code = code;
}
public ZerobaseException(Throwable cause, ExceptionCode code) {
super(cause);
this.code = code;
}
public ZerobaseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, ExceptionCode code) {
super(message, cause, enableSuppression, writableStackTrace);
this.code = code;
}
public ZerobaseException(String message, String message) {
super(message);
this.code = code;
}
public ZerobaseException(String message, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}
package com.example.zerobase.type;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
@RequiredArgsConstructor
public enum ExceptionCode {
INTERNAL_ERROR(HttpStauts.INTERNAL_SERVER_ERROR, "서버 에러입니다.");
private final HttpStauts httpStatus;
private final String message;
}
반응형
'cs > java-spring-boot' 카테고리의 다른 글
[Zero-base] 10-4. 테스트 코드 (2) (0) | 2022.03.18 |
---|---|
[Zero-base] 10-3. 테스트 코드 (1) (0) | 2022.03.18 |
[Zero-base] 10-1. 익셉션 핸들링 (1) (0) | 2022.03.18 |
[Zero-base] 9-15. 주석 (0) | 2022.03.18 |
[Zero-base] 9-14. 심미적 방법들 (0) | 2022.03.17 |