#14 simple blog : custom exception

This commit is contained in:
haerong22
2022-07-29 02:39:11 +09:00
parent 0390d16e11
commit d81cf991b5
8 changed files with 140 additions and 5 deletions

View File

@@ -1,8 +1,10 @@
package com.example.simpleblog.controller;
import com.example.simpleblog.exception.BizException;
import com.example.simpleblog.response.ErrorResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
@@ -13,7 +15,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
public class ExceptionController {
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(Exception.class)
@ExceptionHandler(MethodArgumentNotValidException.class)
public ErrorResponse invalidRequestHandler(MethodArgumentNotValidException e) {
// if (e.hasErrors()) {
@@ -28,4 +30,18 @@ public class ExceptionController {
return response;
// }
}
@ExceptionHandler(BizException.class)
public ResponseEntity<ErrorResponse> postNotFound(BizException e) {
int statusCode = e.getStatusCode();
ErrorResponse body = ErrorResponse.builder()
.code(String.valueOf(statusCode))
.message(e.getMessage())
.validation(e.getValidation())
.build();
return ResponseEntity.status(statusCode)
.body(body);
}
}

View File

@@ -21,6 +21,7 @@ public class PostController {
@PostMapping("/posts")
public void post(@RequestBody @Valid PostCreate request) {
request.validate();
postService.write(request);
}

View File

@@ -0,0 +1,26 @@
package com.example.simpleblog.exception;
import lombok.Getter;
import java.util.HashMap;
import java.util.Map;
@Getter
public abstract class BizException extends RuntimeException {
private final Map<String, String> validation = new HashMap<>();
public BizException(String message) {
super(message);
}
public BizException(String message, Throwable cause) {
super(message, cause);
}
public abstract int getStatusCode();
public void addValidation(String fieldName, String message) {
validation.put(fieldName, message);
}
}

View File

@@ -0,0 +1,28 @@
package com.example.simpleblog.exception;
import lombok.Getter;
@Getter
public class InvalidRequest extends BizException {
private static final String MESSAGE = "잘못된 요청입니다.";
private String fieldName;
private String message;
public InvalidRequest() {
super(MESSAGE);
addValidation(fieldName, message);
}
public InvalidRequest(String fieldName, String message) {
super(MESSAGE);
this.fieldName = fieldName;
this.message = message;
}
@Override
public int getStatusCode() {
return 400;
}
}

View File

@@ -1,6 +1,6 @@
package com.example.simpleblog.exception;
public class PostNotFound extends RuntimeException {
public class PostNotFound extends BizException {
private static final String MESSAGE = "존재하지 않는 글입니다.";
@@ -8,4 +8,8 @@ public class PostNotFound extends RuntimeException {
super(MESSAGE);
}
@Override
public int getStatusCode() {
return 404;
}
}

View File

@@ -1,5 +1,6 @@
package com.example.simpleblog.request;
import com.example.simpleblog.exception.InvalidRequest;
import lombok.Builder;
import lombok.Data;
@@ -25,4 +26,10 @@ public class PostCreate {
this.title = title;
this.content = content;
}
public void validate() {
if (title.contains("바보")) {
throw new InvalidRequest("title", "제목에 바보를 포함할 수 없습니다.");
}
}
}

View File

@@ -3,7 +3,6 @@ package com.example.simpleblog.response;
import lombok.Builder;
import lombok.Getter;
import java.util.HashMap;
import java.util.Map;
/**
@@ -16,17 +15,19 @@ import java.util.Map;
* }
*/
@Getter
//@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
public class ErrorResponse {
private final String code;
private final String message;
private final Map<String, String> validation = new HashMap<>();
private final Map<String, String> validation;
@Builder
public ErrorResponse(String code, String message) {
public ErrorResponse(String code, String message, Map<String, String> validation) {
this.code = code;
this.message = message;
this.validation = validation;
}
public void addValidation(String field, String errorMessage) {

View File

@@ -239,4 +239,56 @@ class PostControllerTest {
;
}
@Test
@DisplayName("존재하지 않는 게시글 조회")
void get_post_not_found_test() throws Exception {
// expected
mockMvc.perform(get("/posts/{postId}", 1L)
.contentType(APPLICATION_JSON)
)
.andExpect(status().isNotFound())
.andDo(print())
;
}
@Test
@DisplayName("존재하지 않는 게시글 수정")
void update_post_not_found_test() throws Exception {
// given
PostEdit postEdit = PostEdit.builder()
.title("제목 수정")
.content("내용")
.build();
// expected
mockMvc.perform(patch("/posts/{postId}", 1L)
.contentType(APPLICATION_JSON)
.content(objectMapper.writeValueAsString(postEdit))
)
.andExpect(status().isNotFound())
.andDo(print())
;
}
@Test
@DisplayName("게시글 작성시 제목에 '바보'는 포함될 수 없다.")
void save_post_fail() throws Exception {
// given
PostCreate request = PostCreate.builder()
.title("야이 바보야!")
.content("글 내용입니다.")
.build();
String jsonString = objectMapper.writeValueAsString(request);
// when
mockMvc.perform(post("/posts")
.contentType(APPLICATION_JSON)
.content(jsonString)
)
.andExpect(status().isBadRequest())
.andDo(print())
;
}
}