You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@dubbo.apache.org by 马金凯 <ma...@dingtalk.com.INVALID> on 2018/10/19 05:43:03 UTC

回复:[incubator-dubbo-ops]A structure with error code or status code design

Greet, the status codes are important parts of the restful service.


------------------------------------------------------------------
发件人:Chen ZhiGuo <ch...@live.com>
发送时间:2018年10月19日(星期五) 12:54
收件人:dev@dubbo.apache.org <de...@dubbo.apache.org>
主 题:[incubator-dubbo-ops]A structure with error code or status code design

Hi, all

Now the restful api deliver only raw data, a structure with error code or status code is needed. So, I normalize the request return status code by annotation “@ControllerAdvice(annotations = ResponseBody.class)”.

The class CustomExceptionHandler[1] i added, to intercept various custom exceptions. For example, ParamValidationException, PermissionDeniedException, ResourceNotFoundException, ServiceException...
under package exception[2]. All of these exceptions are annotated with ResponseStatus[3].

Such as:
ParamValidationException will return status code: HttpStatus.BAD_REQUEST(400)
PermissionDeniedException will return status code: HttpStatus.UNAUTHORIZED(401)
ResourceNotFoundException will return status code: HttpStatus.NOT_FOUND(404)
ServiceException will return status code: HttpStatus.SERVICE_UNAVAILABLE(503)

Code:
@ResponseBody
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(value = {ResourceNotFoundException.class})
public CommonResponse resourceNotFoundExceptionHandle(Exception e) {
        CommonResponse commonResponse = CommonResponse.createCommonResponse();
        logger.error("[ResourceNotFoundException]Exception:", e);
        return commonResponse.fail("Resource not found! Message:" + e.getMessage());
}

When our custom exception is thrown by the developer, the method we customize with the “ExceptionHandler[4]"
annotation will catch the corresponding exception, then we will handle it uniformly and return a correct error status code to front end. So, for the exception series, we need to customize our own exceptions and catch them.

In addition, the return code of the normal series, we directly add the corresponding ResponseStatus[3] annotation to the method within the Controller.

Code:
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED) // It’s code 201
public void createAccess(@RequestBody AccessDTO accessDTO, @PathVariable String env) {}

I have submit the pr[5].

[1] org.apache.dubbo.admin.handler.CustomExceptionHandler.java
[2] org.apache.dubbo.admin.common.exception
[3] org.springframework.web.bind.annotation.ResponseStatus
[4] org.springframework.web.bind.annotation.ExceptionHandler
[5] https://github.com/apache/incubator-dubbo-ops/pull/147



Thanks & Best Regards.


Re: [incubator-dubbo-ops]A structure with error code or status code design

Posted by Ian Luo <ia...@gmail.com>.
ZhiGuo, I noticed ur proposal has been merged. Thanks for your
contributions :)

-Ian.

On Fri, Oct 19, 2018 at 1:43 PM 马金凯 <ma...@dingtalk.com.invalid> wrote:

> Greet, the status codes are important parts of the restful service.
>
>
> ------------------------------------------------------------------
> 发件人:Chen ZhiGuo <ch...@live.com>
> 发送时间:2018年10月19日(星期五) 12:54
> 收件人:dev@dubbo.apache.org <de...@dubbo.apache.org>
> 主 题:[incubator-dubbo-ops]A structure with error code or status code design
>
> Hi, all
>
> Now the restful api deliver only raw data, a structure with error code or
> status code is needed. So, I normalize the request return status code by
> annotation “@ControllerAdvice(annotations = ResponseBody.class)”.
>
> The class CustomExceptionHandler[1] i added, to intercept various custom
> exceptions. For example, ParamValidationException,
> PermissionDeniedException, ResourceNotFoundException, ServiceException...
> under package exception[2]. All of these exceptions are annotated with
> ResponseStatus[3].
>
> Such as:
> ParamValidationException will return status code:
> HttpStatus.BAD_REQUEST(400)
> PermissionDeniedException will return status code:
> HttpStatus.UNAUTHORIZED(401)
> ResourceNotFoundException will return status code:
> HttpStatus.NOT_FOUND(404)
> ServiceException will return status code:
> HttpStatus.SERVICE_UNAVAILABLE(503)
>
> Code:
> @ResponseBody
> @ResponseStatus(HttpStatus.NOT_FOUND)
> @ExceptionHandler(value = {ResourceNotFoundException.class})
> public CommonResponse resourceNotFoundExceptionHandle(Exception e) {
>         CommonResponse commonResponse =
> CommonResponse.createCommonResponse();
>         logger.error("[ResourceNotFoundException]Exception:", e);
>         return commonResponse.fail("Resource not found! Message:" +
> e.getMessage());
> }
>
> When our custom exception is thrown by the developer, the method we
> customize with the “ExceptionHandler[4]"
> annotation will catch the corresponding exception, then we will handle it
> uniformly and return a correct error status code to front end. So, for the
> exception series, we need to customize our own exceptions and catch them.
>
> In addition, the return code of the normal series, we directly add the
> corresponding ResponseStatus[3] annotation to the method within the
> Controller.
>
> Code:
> @RequestMapping(method = RequestMethod.POST)
> @ResponseStatus(HttpStatus.CREATED) // It’s code 201
> public void createAccess(@RequestBody AccessDTO accessDTO, @PathVariable
> String env) {}
>
> I have submit the pr[5].
>
> [1] org.apache.dubbo.admin.handler.CustomExceptionHandler.java
> [2] org.apache.dubbo.admin.common.exception
> [3] org.springframework.web.bind.annotation.ResponseStatus
> [4] org.springframework.web.bind.annotation.ExceptionHandler
> [5] https://github.com/apache/incubator-dubbo-ops/pull/147
>
>
>
> Thanks & Best Regards.
>
>