Skip to content

Commit 20cd91d

Browse files
committed
add error message handler
1 parent e6e6972 commit 20cd91d

File tree

11 files changed

+104
-54
lines changed

11 files changed

+104
-54
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111
<groupId>com.codingapi.springboot</groupId>
1212
<artifactId>springboot-framework-parent</artifactId>
13-
<version>0.0.15</version>
13+
<version>0.0.16</version>
1414

1515
<url>https://github.com/codingapi/springboot-framewrok</url>
1616
<name>springboot-framework-parent</name>

springboot-data-permission/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-framework-parent</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>0.0.15</version>
8+
<version>0.0.16</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

@@ -32,7 +32,7 @@
3232
<dependency>
3333
<groupId>com.codingapi.springboot</groupId>
3434
<artifactId>springboot-framework</artifactId>
35-
<version>0.0.15</version>
35+
<version>0.0.16</version>
3636
</dependency>
3737

3838
<dependency>

springboot-example/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
<parent>
55
<groupId>com.codingapi.springboot</groupId>
66
<artifactId>springboot-framework-parent</artifactId>
7-
<version>0.0.15</version>
7+
<version>0.0.16</version>
88
</parent>
99
<artifactId>springboot-example</artifactId>
10-
<version>0.0.15</version>
10+
<version>0.0.16</version>
1111

1212
<name>springboot-example</name>
1313
<description>springboot-example project for Spring Boot</description>
@@ -29,7 +29,7 @@
2929
<dependency>
3030
<groupId>com.codingapi.springboot</groupId>
3131
<artifactId>springboot-framework</artifactId>
32-
<version>0.0.15</version>
32+
<version>0.0.16</version>
3333
</dependency>
3434

3535
<dependency>
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
package com.codingapi.springboot.example.controller;
22

3-
import com.codingapi.springboot.framework.exception.MessageException;
3+
import com.codingapi.springboot.framework.dto.response.Response;
4+
import com.codingapi.springboot.framework.dto.response.SingleResponse;
5+
import com.codingapi.springboot.framework.exception.LocaleMessageException;
46
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestMapping;
58
import org.springframework.web.bind.annotation.RestController;
69

710
@RestController
11+
@RequestMapping("/demo")
812
public class DemoController {
913

14+
@GetMapping("/error")
15+
public Response error(){
16+
throw new LocaleMessageException("hello.error");
17+
}
18+
1019
@GetMapping("/hello")
11-
public String hello(){
12-
throw new MessageException("hello.error");
20+
public SingleResponse<String> hello(){
21+
return SingleResponse.of("hello");
1322
}
23+
24+
25+
1426
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.codingapi.springboot.example.handler;
2+
3+
import com.codingapi.springboot.framework.exception.LocaleMessageException;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.stereotype.Component;
6+
import org.springframework.web.servlet.HandlerExceptionResolver;
7+
import org.springframework.web.servlet.ModelAndView;
8+
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
9+
10+
import javax.servlet.http.HttpServletRequest;
11+
import javax.servlet.http.HttpServletResponse;
12+
13+
@Slf4j
14+
@Component
15+
public class ServletExceptionHandler implements HandlerExceptionResolver {
16+
17+
private final static String DEFAULT_ERROR_CODE = "system.err";
18+
19+
@Override
20+
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
21+
log.error("exception:",ex);
22+
23+
MappingJackson2JsonView view = new MappingJackson2JsonView();
24+
ModelAndView mv = new ModelAndView(view);
25+
26+
mv.addObject("success",false);
27+
if(ex instanceof LocaleMessageException) {
28+
LocaleMessageException localMessageException = (LocaleMessageException)ex;
29+
mv.addObject("errCode",localMessageException.getErrCode());
30+
mv.addObject("errMessage",localMessageException.getMessage());
31+
return mv;
32+
}
33+
mv.addObject("errCode",DEFAULT_ERROR_CODE);
34+
mv.addObject("errMessage",ex.getMessage());
35+
36+
return mv;
37+
}
38+
}

springboot-framework/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
<parent>
55
<groupId>com.codingapi.springboot</groupId>
66
<artifactId>springboot-framework-parent</artifactId>
7-
<version>0.0.15</version>
7+
<version>0.0.16</version>
88
</parent>
99
<artifactId>springboot-framework</artifactId>
10-
<version>0.0.15</version>
10+
<version>0.0.16</version>
1111

1212
<name>springboot-framework</name>
1313
<description>springboot-framework project for Spring Boot</description>

springboot-framework/src/main/java/com/codingapi/springboot/framework/exception/ExceptionConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
public class ExceptionConfiguration {
99

1010
@Bean(initMethod = "init")
11-
public ExceptionLocaleMessage exceptionLocaleMessage(MessageSource messageSource){
12-
return new ExceptionLocaleMessage(messageSource);
11+
public LocaleMessage exceptionLocaleMessage(MessageSource messageSource){
12+
return new LocaleMessage(messageSource);
1313
}
1414

1515
}

springboot-framework/src/main/java/com/codingapi/springboot/framework/exception/ExceptionMessageContext.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

springboot-framework/src/main/java/com/codingapi/springboot/framework/exception/ExceptionLocaleMessage.java renamed to springboot-framework/src/main/java/com/codingapi/springboot/framework/exception/LocaleMessage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
import java.util.Locale;
77

8-
class ExceptionLocaleMessage {
8+
class LocaleMessage {
99

1010
private final MessageSource messageSource;
1111

12-
public ExceptionLocaleMessage(MessageSource messageSource) {
12+
public LocaleMessage(MessageSource messageSource) {
1313
this.messageSource = messageSource;
1414
}
1515

1616
public void init(){
17-
ExceptionMessageContext.getInstance().setExceptionLocaleMessage(this);
17+
MessageContext.getInstance().setExceptionLocaleMessage(this);
1818
}
1919

2020
public String getMessage(String code){

springboot-framework/src/main/java/com/codingapi/springboot/framework/exception/MessageException.java renamed to springboot-framework/src/main/java/com/codingapi/springboot/framework/exception/LocaleMessageException.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import lombok.Getter;
44

5-
public class MessageException extends RuntimeException{
5+
public class LocaleMessageException extends RuntimeException{
66

77
@Getter
88
private final String errCode;
@@ -11,26 +11,26 @@ public class MessageException extends RuntimeException{
1111
private final String errMessage;
1212

1313

14-
public MessageException(String errCode, String errMessage, Throwable cause) {
14+
public LocaleMessageException(String errCode, String errMessage, Throwable cause) {
1515
super(errMessage, cause);
1616
this.errCode = errCode;
1717
this.errMessage = errMessage;
1818
}
1919

20-
public MessageException(String errCode, Throwable cause) {
21-
super(ExceptionMessageContext.getInstance().getErrorMsg(errCode), cause);
20+
public LocaleMessageException(String errCode, Throwable cause) {
21+
super(MessageContext.getInstance().getErrorMsg(errCode), cause);
2222
this.errCode = errCode;
2323
this.errMessage = getMessage();
2424
}
2525

26-
public MessageException(String errCode, String errMessage) {
26+
public LocaleMessageException(String errCode, String errMessage) {
2727
super(errMessage);
2828
this.errCode = errCode;
2929
this.errMessage = errMessage;
3030
}
3131

32-
public MessageException(String errCode) {
33-
super(ExceptionMessageContext.getInstance().getErrorMsg(errCode));
32+
public LocaleMessageException(String errCode) {
33+
super(MessageContext.getInstance().getErrorMsg(errCode));
3434
this.errCode = errCode;
3535
this.errMessage = getMessage();
3636
}

0 commit comments

Comments
 (0)