#32 multi module: handle exception
This commit is contained in:
@@ -20,4 +20,9 @@ public class DemoController {
|
|||||||
public String find() {
|
public String find() {
|
||||||
return demoService.find();
|
return demoService.find();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/exception")
|
||||||
|
public String exception() {
|
||||||
|
return demoService.exception();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.example.moduleapi.exception;
|
||||||
|
|
||||||
|
import com.example.modulecommon.enums.CodeEnum;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class CustomException extends RuntimeException {
|
||||||
|
private final String returnCode;
|
||||||
|
private final String returnMessage;
|
||||||
|
|
||||||
|
public CustomException(CodeEnum codeEnum) {
|
||||||
|
super(codeEnum.getMessage());
|
||||||
|
this.returnCode = codeEnum.getCode();
|
||||||
|
this.returnMessage = codeEnum.getMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.example.moduleapi.exception;
|
||||||
|
|
||||||
|
import com.example.moduleapi.response.CommonResponse;
|
||||||
|
import com.example.modulecommon.enums.CodeEnum;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
@RestControllerAdvice
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
@ExceptionHandler(CustomException.class)
|
||||||
|
public CommonResponse<?> handleCustomException(CustomException e) {
|
||||||
|
|
||||||
|
return CommonResponse.builder()
|
||||||
|
.returnCode(e.getReturnCode())
|
||||||
|
.returnMessage(e.getReturnMessage())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
public CommonResponse<?> handleException(Exception e) {
|
||||||
|
return CommonResponse.builder()
|
||||||
|
.returnCode(CodeEnum.UNKNOWN_ERROR.getCode())
|
||||||
|
.returnMessage(CodeEnum.UNKNOWN_ERROR.getMessage())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.example.moduleapi.response;
|
||||||
|
|
||||||
|
import com.example.modulecommon.enums.CodeEnum;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Builder
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
|
public class CommonResponse<T> {
|
||||||
|
|
||||||
|
private String returnCode;
|
||||||
|
private String returnMessage;
|
||||||
|
private T info;
|
||||||
|
|
||||||
|
public CommonResponse(CodeEnum codeEnum) {
|
||||||
|
this.returnCode = codeEnum.getCode();
|
||||||
|
this.returnMessage = codeEnum.getMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommonResponse(T info) {
|
||||||
|
this.returnCode = CodeEnum.SUCCESS.getCode();
|
||||||
|
this.returnMessage = CodeEnum.SUCCESS.getMessage();
|
||||||
|
this.info = info;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommonResponse(CodeEnum codeEnum, T info) {
|
||||||
|
this.returnCode = codeEnum.getCode();
|
||||||
|
this.returnMessage = codeEnum.getMessage();
|
||||||
|
this.info = info;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.example.moduleapi.service;
|
package com.example.moduleapi.service;
|
||||||
|
|
||||||
|
import com.example.moduleapi.exception.CustomException;
|
||||||
import com.example.modulecommon.enums.CodeEnum;
|
import com.example.modulecommon.enums.CodeEnum;
|
||||||
import com.example.modulecommon.sevice.CommonDemoService;
|
import com.example.modulecommon.sevice.CommonDemoService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@@ -20,4 +21,8 @@ public class DemoService {
|
|||||||
public String find() {
|
public String find() {
|
||||||
return "find";
|
return "find";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String exception() {
|
||||||
|
throw new CustomException(CodeEnum.UNKNOWN_ERROR);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user