Skip to content

Commit 7d1527d

Browse files
committed
inner exception message를 데이터로 이동
1 parent 1fa8237 commit 7d1527d

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

src/main/java/com/zkdlu/apiresponsespringbootstarter/core/advice/ExceptionAdvice.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ public ExceptionAdvice(ResponseProperties responseProperties) {
1919

2020
@ExceptionHandler(RuntimeException.class)
2121
public Object handle(Exception e) {
22-
return getResult(getExceptionProperties(e, ExceptionProperties.UNHANDLED));
22+
return getResult(e, getExceptionProperties(e, ExceptionProperties.UNHANDLED));
2323
}
2424

2525

2626
@ExceptionHandler(NoHandlerFoundException.class)
2727
public Object handleNoHandlerFoundException(Exception e) {
28-
return getResult(getExceptionProperties(e, ExceptionProperties.NOT_FOUND));
28+
return getResult(e, getExceptionProperties(e, ExceptionProperties.NOT_FOUND));
2929
}
3030

3131
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
3232
public Object handleNotSupportedMethodException(Exception e) {
33-
return getResult(getExceptionProperties(e, ExceptionProperties.METHOD_NOT_ALLOWED));
33+
return getResult(e, getExceptionProperties(e, ExceptionProperties.METHOD_NOT_ALLOWED));
3434
}
3535

3636
@ExceptionHandler(MissingServletRequestParameterException.class)
3737
public Object handleMissingRequestParameterException(Exception e) {
38-
return getResult(getExceptionProperties(e, ExceptionProperties.BAD_REQUEST));
38+
return getResult(e, getExceptionProperties(e, ExceptionProperties.BAD_REQUEST));
3939
}
4040

4141
private ExceptionProperties getExceptionProperties(Exception e, ExceptionProperties unhandled) {
@@ -45,15 +45,16 @@ private ExceptionProperties getExceptionProperties(Exception e, ExceptionPropert
4545
.findFirst()
4646
.orElse(unhandled);
4747

48-
exceptionModel.setMsg(exceptionModel.getMsg() + ": " + e.getMessage());
48+
exceptionModel.setMsg(exceptionModel.getMsg());
4949
return exceptionModel;
5050
}
5151

52-
private SingleResult<Object> getResult(ExceptionProperties exceptionModel) {
52+
private SingleResult<Object> getResult(Exception e, ExceptionProperties exceptionModel) {
5353
SingleResult<Object> result = new SingleResult<>();
5454
result.setSuccess(false);
5555
result.setCode(exceptionModel.getCode());
5656
result.setMsg(exceptionModel.getMsg());
57+
result.setData(e.getMessage());
5758
return result;
5859
}
5960
}

src/test/java/com/zkdlu/apiresponsespringbootstarter/HandlerTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ void setUp() {
6767
.andExpect(status().isBadRequest())
6868
.andExpect(jsonPath("$.success", equalTo(false)))
6969
.andExpect(jsonPath("$.code", equalTo(400)))
70-
.andExpect(jsonPath("$.msg", equalTo("Custom Message: 메시지")))
71-
.andExpect(jsonPath("$.data", equalTo(null)))
70+
.andExpect(jsonPath("$.msg", equalTo("Custom Message")))
71+
.andExpect(jsonPath("$.data", equalTo("메시지")))
7272
;
7373
}
7474

@@ -79,8 +79,8 @@ void setUp() {
7979
.andExpect(status().isNotFound())
8080
.andExpect(jsonPath("$.success", equalTo(false)))
8181
.andExpect(jsonPath("$.code", equalTo(404)))
82-
.andExpect(jsonPath("$.msg", equalTo("Not Found: No handler found for GET /asdf")))
83-
.andExpect(jsonPath("$.data", equalTo(null)))
82+
.andExpect(jsonPath("$.msg", equalTo("Not Found")))
83+
.andExpect(jsonPath("$.data", equalTo("No handler found for GET /asdf")))
8484
;
8585
}
8686

@@ -91,8 +91,8 @@ void setUp() {
9191
.andExpect(status().isBadRequest())
9292
.andExpect(jsonPath("$.success", equalTo(false)))
9393
.andExpect(jsonPath("$.code", equalTo(400)))
94-
.andExpect(jsonPath("$.msg", equalTo("Bad Request: Required String parameter 'param' is not present")))
95-
.andExpect(jsonPath("$.data", equalTo(null)))
94+
.andExpect(jsonPath("$.msg", equalTo("Bad Request")))
95+
.andExpect(jsonPath("$.data", equalTo("Required String parameter 'param' is not present")))
9696
;
9797
}
9898

@@ -103,8 +103,8 @@ void setUp() {
103103
.andExpect(status().isMethodNotAllowed())
104104
.andExpect(jsonPath("$.success", equalTo(false)))
105105
.andExpect(jsonPath("$.code", equalTo(405)))
106-
.andExpect(jsonPath("$.msg", equalTo("Method Not Allowed: Request method 'POST' not supported")))
107-
.andExpect(jsonPath("$.data", equalTo(null)))
106+
.andExpect(jsonPath("$.msg", equalTo("Method Not Allowed")))
107+
.andExpect(jsonPath("$.data", equalTo("Request method 'POST' not supported")))
108108
;
109109
}
110110

@@ -124,8 +124,8 @@ void setUp() {
124124
.andExpect(status().isInternalServerError())
125125
.andExpect(jsonPath("$.success", equalTo(false)))
126126
.andExpect(jsonPath("$.code", equalTo(4000)))
127-
.andExpect(jsonPath("$.msg", equalTo("지원하지 않는 상태코드: 메시지")))
128-
.andExpect(jsonPath("$.data", equalTo(null)))
127+
.andExpect(jsonPath("$.msg", equalTo("지원하지 않는 상태코드")))
128+
.andExpect(jsonPath("$.data", equalTo("메시지")))
129129
;
130130
}
131131

@@ -145,8 +145,8 @@ void setUp() {
145145
.andExpect(status().isNotImplemented())
146146
.andExpect(jsonPath("$.success", equalTo(false)))
147147
.andExpect(jsonPath("$.code", equalTo(501)))
148-
.andExpect(jsonPath("$.msg", equalTo("Unhandled Exception: " + "메시지")))
149-
.andExpect(jsonPath("$.data", equalTo(null)))
148+
.andExpect(jsonPath("$.msg", equalTo("Unhandled Exception")))
149+
.andExpect(jsonPath("$.data", equalTo("메시지")))
150150
;
151151
}
152152

0 commit comments

Comments
 (0)