提交 | 用户 | 时间
|
e7c126
|
1 |
/* |
H |
2 |
* Copyright 1999-2022 Alibaba Group Holding Ltd. |
|
3 |
* |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
*/ |
|
16 |
|
|
17 |
package com.alibaba.nacos.console.exception; |
|
18 |
|
|
19 |
import com.alibaba.nacos.api.annotation.NacosApi; |
|
20 |
import com.alibaba.nacos.api.exception.NacosException; |
|
21 |
import com.alibaba.nacos.api.exception.api.NacosApiException; |
|
22 |
import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException; |
|
23 |
import com.alibaba.nacos.api.model.v2.ErrorCode; |
|
24 |
import com.alibaba.nacos.api.model.v2.Result; |
|
25 |
import com.alibaba.nacos.common.utils.ExceptionUtil; |
|
26 |
import com.alibaba.nacos.plugin.auth.exception.AccessException; |
|
27 |
import org.slf4j.Logger; |
|
28 |
import org.slf4j.LoggerFactory; |
|
29 |
import org.springframework.core.annotation.Order; |
|
30 |
import org.springframework.dao.DataAccessException; |
|
31 |
import org.springframework.http.HttpStatus; |
|
32 |
import org.springframework.http.ResponseEntity; |
|
33 |
import org.springframework.http.converter.HttpMessageConversionException; |
|
34 |
import org.springframework.http.converter.HttpMessageNotReadableException; |
|
35 |
import org.springframework.web.HttpMediaTypeException; |
|
36 |
import org.springframework.web.bind.MissingServletRequestParameterException; |
|
37 |
import org.springframework.web.bind.annotation.ControllerAdvice; |
|
38 |
import org.springframework.web.bind.annotation.ExceptionHandler; |
|
39 |
import org.springframework.web.bind.annotation.ResponseBody; |
|
40 |
import org.springframework.web.bind.annotation.ResponseStatus; |
|
41 |
|
|
42 |
import javax.servlet.ServletException; |
|
43 |
import java.io.IOException; |
|
44 |
|
|
45 |
/** |
|
46 |
* Exception Handler for Nacos API. |
|
47 |
* @author dongyafei |
|
48 |
* @date 2022/7/22 |
|
49 |
*/ |
|
50 |
|
|
51 |
@Order(-1) |
|
52 |
@ControllerAdvice(annotations = {NacosApi.class}) |
|
53 |
@ResponseBody |
|
54 |
public class NacosApiExceptionHandler { |
|
55 |
|
|
56 |
private static final Logger LOGGER = LoggerFactory.getLogger(NacosApiExceptionHandler.class); |
|
57 |
|
|
58 |
@ExceptionHandler(NacosApiException.class) |
|
59 |
public ResponseEntity<Result<String>> handleNacosApiException(NacosApiException e) { |
|
60 |
LOGGER.error("got exception. {} {}", e.getErrAbstract(), e.getErrMsg()); |
|
61 |
return ResponseEntity.status(e.getErrCode()).body(new Result<>(e.getDetailErrCode(), e.getErrAbstract(), e.getErrMsg())); |
|
62 |
} |
|
63 |
|
|
64 |
@ExceptionHandler(NacosException.class) |
|
65 |
public ResponseEntity<Result<String>> handleNacosException(NacosException e) { |
|
66 |
LOGGER.error("got exception. {}", e.getErrMsg()); |
|
67 |
return ResponseEntity.status(e.getErrCode()).body(Result.failure(ErrorCode.SERVER_ERROR, e.getErrMsg())); |
|
68 |
} |
|
69 |
|
|
70 |
@ExceptionHandler(NacosRuntimeException.class) |
|
71 |
public ResponseEntity<Result<String>> handleNacosRuntimeException(NacosRuntimeException e) { |
|
72 |
LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); |
|
73 |
return ResponseEntity.status(e.getErrCode()).body(Result.failure(ErrorCode.SERVER_ERROR, e.getMessage())); |
|
74 |
} |
|
75 |
|
|
76 |
@ResponseStatus(HttpStatus.BAD_REQUEST) |
|
77 |
@ExceptionHandler(HttpMessageNotReadableException.class) |
|
78 |
public Result<String> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) { |
|
79 |
LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); |
|
80 |
return Result.failure(ErrorCode.PARAMETER_MISSING, e.getMessage()); |
|
81 |
} |
|
82 |
|
|
83 |
@ResponseStatus(HttpStatus.BAD_REQUEST) |
|
84 |
@ExceptionHandler(HttpMessageConversionException.class) |
|
85 |
public Result<String> handleHttpMessageConversionException(HttpMessageConversionException e) { |
|
86 |
LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); |
|
87 |
return Result.failure(ErrorCode.PARAMETER_VALIDATE_ERROR, e.getMessage()); |
|
88 |
} |
|
89 |
|
|
90 |
@ResponseStatus(HttpStatus.BAD_REQUEST) |
|
91 |
@ExceptionHandler(NumberFormatException.class) |
|
92 |
public Result<String> handleNumberFormatException(NumberFormatException e) { |
|
93 |
LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); |
|
94 |
return Result.failure(ErrorCode.PARAMETER_VALIDATE_ERROR, e.getMessage()); |
|
95 |
} |
|
96 |
|
|
97 |
@ResponseStatus(HttpStatus.BAD_REQUEST) |
|
98 |
@ExceptionHandler(IllegalArgumentException.class) |
|
99 |
public Result<String> handleIllegalArgumentException(IllegalArgumentException e) { |
|
100 |
LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); |
|
101 |
return Result.failure(ErrorCode.PARAMETER_VALIDATE_ERROR, e.getMessage()); |
|
102 |
} |
|
103 |
|
|
104 |
@ResponseStatus(HttpStatus.BAD_REQUEST) |
|
105 |
@ExceptionHandler(MissingServletRequestParameterException.class) |
|
106 |
public Result<String> handleMissingServletRequestParameterException(MissingServletRequestParameterException e) { |
|
107 |
LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); |
|
108 |
return Result.failure(ErrorCode.PARAMETER_MISSING, e.getMessage()); |
|
109 |
} |
|
110 |
|
|
111 |
@ResponseStatus(HttpStatus.BAD_REQUEST) |
|
112 |
@ExceptionHandler(HttpMediaTypeException.class) |
|
113 |
public Result<String> handleHttpMediaTypeException(HttpMediaTypeException e) { |
|
114 |
LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); |
|
115 |
return Result.failure(ErrorCode.MEDIA_TYPE_ERROR, e.getMessage()); |
|
116 |
} |
|
117 |
|
|
118 |
@ResponseStatus(HttpStatus.FORBIDDEN) |
|
119 |
@ExceptionHandler(AccessException.class) |
|
120 |
public Result<String> handleAccessException(AccessException e) { |
|
121 |
LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); |
|
122 |
return Result.failure(ErrorCode.ACCESS_DENIED, e.getErrMsg()); |
|
123 |
} |
|
124 |
|
|
125 |
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) |
|
126 |
@ExceptionHandler(value = {DataAccessException.class, ServletException.class, IOException.class}) |
|
127 |
public Result<String> handleDataAccessException(Exception e) { |
|
128 |
LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); |
|
129 |
return Result.failure(ErrorCode.DATA_ACCESS_ERROR, e.getMessage()); |
|
130 |
} |
|
131 |
|
|
132 |
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) |
|
133 |
@ExceptionHandler(Exception.class) |
|
134 |
public Result<String> handleOtherException(Exception e) { |
|
135 |
LOGGER.error("got exception. {} {}", e.getMessage(), ExceptionUtil.getAllExceptionMsg(e)); |
|
136 |
return Result.failure(e.getMessage()); |
|
137 |
} |
|
138 |
} |