Skip to content

Commit

Permalink
[fix] GlobalExceptionRestAdvice 수정
Browse files Browse the repository at this point in the history
GlobalExceptionRestAdvice 수정
  • Loading branch information
liyusang1 committed Jan 5, 2024
1 parent c30a849 commit 071816b
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.haejwo.tripcometrue.global.exception;

import com.haejwo.tripcometrue.global.util.ResponseDTO;
import java.util.Map;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand Down Expand Up @@ -66,14 +65,16 @@ public ResponseEntity<ResponseDTO<Void>> handleValidationExceptions(

BindingResult bindingResult = e.getBindingResult();

Map<String, String> fieldErrors = bindingResult.getFieldErrors()
List<String> fieldErrors = bindingResult.getFieldErrors()
.stream()
.collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
.map(error -> error.getField() + ": " + error.getDefaultMessage())
.collect(Collectors.toList());

log.error(e.getMessage(), e);
String errorMessage = String.join(", ", fieldErrors);

return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(ResponseDTO.errorWithMessage(HttpStatus.BAD_REQUEST,
fieldErrors.values().toString().substring(1,fieldErrors.values().toString().length()-1)));
.body(ResponseDTO.errorWithMessage(HttpStatus.BAD_REQUEST, errorMessage));
}
}

0 comments on commit 071816b

Please sign in to comment.