Skip to content

Commit

Permalink
REFACTOR(error) :: GlobalException Log 추가 및 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
Woongbin06 committed Nov 7, 2024
1 parent 2eeebb4 commit ddf7e7e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
public class ErrorResponse {

private int status;
private String code;
private String message;

@Override
public String toString() {
return "{\n" +
"\t\"status\": " + status +
",\n\t\"code\": \"" + code + '\"' +
",\n\t\"message\": \"" + message + '\"' +
"\n}";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,52 @@
package com.woongeya.zoing.global.error;

import com.woongeya.zoing.global.error.exception.ZoingException;
import static org.springframework.http.HttpStatus.*;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import java.util.HashMap;
import java.util.Map;

import static com.woongeya.zoing.global.error.exception.ErrorCode.INTERNAL_SERVER_ERROR;
import lombok.extern.slf4j.Slf4j;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

@ExceptionHandler(ZoingException.class)
public ResponseEntity<?> handleCustomException(ZoingException e) {
return new ResponseEntity<>(new ErrorResponse(e.getErrorCode().getStatus(), e.getErrorCode().getCode(), e.getErrorCode().getMessage()), HttpStatus.valueOf(e.getErrorCode().getStatus()));
@ExceptionHandler(JJoingException.class)
public ResponseEntity<ErrorResponse> handleDefineException(JJoingException e) {
log.warn(e.getMessage() + "\n \t {}", e);
return ResponseEntity.status(e.getStatus())
.body(new ErrorResponse(e.getStatus().value(), e.getMessage()));
}

@ExceptionHandler(BindException.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public ResponseEntity<?> bindException(BindException e) {
Map<String, String> errorMap = new HashMap<>();

for (FieldError error : e.getFieldErrors()) {
errorMap.put(error.getField(), error.getDefaultMessage());
}

return new ResponseEntity<>(errorMap, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler({MethodArgumentNotValidException.class})
@ResponseStatus(BAD_REQUEST)
public ErrorResponse handleDefineException(MethodArgumentNotValidException e) {
log.warn(e.getMessage() + "\n \t {}", e);

@ExceptionHandler({ConstraintViolationException.class})
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public ResponseEntity<?> handleConstraintViolation(ConstraintViolationException e) {
Map<String, String> errorMap = new HashMap<>();
String message;

for (ConstraintViolation<?> violation : e.getConstraintViolations()) {
errorMap.put(violation.getPropertyPath().toString(), violation.getMessage());
if (e.getFieldError() == null) {
message = "";
} else {
message = e.getFieldError().getDefaultMessage();
}

return new ResponseEntity<>(errorMap, HttpStatus.BAD_REQUEST);
return new ErrorResponse(400, message);
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleServerException(Exception ex) {
return new ResponseEntity<>(
new ErrorResponse(
INTERNAL_SERVER_ERROR.getStatus(),
INTERNAL_SERVER_ERROR.getCode(),
INTERNAL_SERVER_ERROR.getMessage()
),
HttpStatus.INTERNAL_SERVER_ERROR
);
@ResponseStatus(INTERNAL_SERVER_ERROR)
public ErrorResponse handleDefineException(Exception e) {
log.error(e.getMessage() + "\n \t {}", e);
return new ErrorResponse(500, "서버에서 알 수 없는 에러가 발생했습니다.");
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/woongeya/zoing/global/error/JJoingException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.woongeya.zoing.global.error;

import org.springframework.http.HttpStatus;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Getter
public class JJoingException extends RuntimeException {

private final HttpStatus status;
private final String message;
}

This file was deleted.

0 comments on commit ddf7e7e

Please sign in to comment.