Skip to content

Commit

Permalink
refactor: error response 통일
Browse files Browse the repository at this point in the history
  • Loading branch information
ASak1104 committed Jan 8, 2024
1 parent 9e23c85 commit 5bb4948
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 73 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import com.backendoori.ootw.exception.ExceptionResponse.FieldErrorDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.dao.DuplicateKeyException;
Expand Down Expand Up @@ -41,7 +40,20 @@ public ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException(Metho

ErrorResponse errorResponse = new ErrorResponse(message);

return ResponseEntity.badRequest()
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(errorResponse);
}

@ExceptionHandler(HandlerMethodValidationException.class)
public ResponseEntity<ErrorResponse> handlerMethodValidationException(HandlerMethodValidationException e) {
String errorMessage = e.getAllValidationResults()
.get(0)
.getResolvableErrors()
.get(0)
.getDefaultMessage();
ErrorResponse errorResponse = new ErrorResponse(errorMessage);

return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(errorResponse);
}

Expand Down Expand Up @@ -69,29 +81,12 @@ public ResponseEntity<ErrorResponse> handleDuplicateKeyException(DuplicateKeyExc
.body(errorResponse);
}

@ExceptionHandler(HandlerMethodValidationException.class)
public ResponseEntity<ErrorResponse> handlerMethodValidationException(HandlerMethodValidationException e) {
String errorMessage = e.getAllValidationResults()
.get(0)
.getResolvableErrors()
.get(0)
.getDefaultMessage();
ErrorResponse errorResponse = new ErrorResponse(errorMessage);

return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(errorResponse);
}

@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<ExceptionResponse<String>> handleIllegalArgumentException(IllegalArgumentException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ExceptionResponse.from(e));
}

@ExceptionHandler(ImageUploadException.class)
public ResponseEntity<ExceptionResponse<String>> handleImageUploadException(ImageUploadException e) {
public ResponseEntity<ErrorResponse> handleImageUploadException(ImageUploadException e) {
ErrorResponse errorResponse = new ErrorResponse(e.getMessage());

return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY)
.body(ExceptionResponse.from(e));
.body(errorResponse);
}

@ExceptionHandler(Exception.class)
Expand All @@ -104,5 +99,4 @@ public ResponseEntity<ErrorResponse> handleException(Exception e) {
.body(errorResponse);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import static com.backendoori.ootw.security.jwt.JwtAuthenticationFilter.TOKEN_HEADER;
import static com.backendoori.ootw.security.jwt.JwtAuthenticationFilter.TOKEN_PREFIX;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.nio.charset.StandardCharsets;
import java.util.List;
import com.backendoori.ootw.exception.ExceptionResponse;
import com.backendoori.ootw.exception.ExceptionResponse.FieldErrorDetail;
import com.backendoori.ootw.post.dto.PostReadResponse;
import com.backendoori.ootw.post.dto.PostSaveRequest;
import com.backendoori.ootw.post.dto.PostSaveResponse;
Expand Down Expand Up @@ -173,16 +173,10 @@ void saveFailByMethodArgumentNotValidException() throws Exception {
.accept(MediaType.APPLICATION_JSON);

// then
String response = mockMvc.perform(requestBuilder)
mockMvc.perform(requestBuilder)
.andExpect(status().isBadRequest())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn()
.getResponse()
.getContentAsString(StandardCharsets.UTF_8);

ExceptionResponse<List<FieldErrorDetail>> exceptionResponse =
objectMapper.readValue(response, ExceptionResponse.class);
assertThat(exceptionResponse.error()).hasSize(1);
.andExpect(jsonPath("$.message", instanceOf(String.class)))
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}

@Test
Expand Down

0 comments on commit 5bb4948

Please sign in to comment.