提交 | 用户 | 时间
|
e7c126
|
1 |
/* |
H |
2 |
* Copyright 1999-2018 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.exception.runtime.NacosRuntimeException; |
|
20 |
import com.alibaba.nacos.plugin.auth.exception.AccessException; |
|
21 |
import com.alibaba.nacos.common.model.RestResultUtils; |
|
22 |
import com.alibaba.nacos.common.utils.ExceptionUtil; |
|
23 |
import com.alibaba.nacos.core.utils.Commons; |
|
24 |
import org.slf4j.Logger; |
|
25 |
import org.slf4j.LoggerFactory; |
|
26 |
import org.springframework.http.HttpStatus; |
|
27 |
import org.springframework.http.ResponseEntity; |
|
28 |
import org.springframework.web.bind.annotation.ControllerAdvice; |
|
29 |
import org.springframework.web.bind.annotation.ExceptionHandler; |
|
30 |
import org.springframework.web.util.HtmlUtils; |
|
31 |
|
|
32 |
import javax.servlet.http.HttpServletRequest; |
|
33 |
|
|
34 |
/** |
|
35 |
* Exception handler for console module. |
|
36 |
* |
|
37 |
* @author nkorange |
|
38 |
* @since 1.2.0 |
|
39 |
*/ |
|
40 |
@ControllerAdvice |
|
41 |
public class ConsoleExceptionHandler { |
|
42 |
|
|
43 |
private static final Logger LOGGER = LoggerFactory.getLogger(ConsoleExceptionHandler.class); |
|
44 |
|
|
45 |
@ExceptionHandler(AccessException.class) |
|
46 |
private ResponseEntity<String> handleAccessException(AccessException e) { |
|
47 |
LOGGER.error("got exception. {}", e.getErrMsg()); |
|
48 |
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getErrMsg()); |
|
49 |
} |
|
50 |
|
|
51 |
@ExceptionHandler(IllegalArgumentException.class) |
|
52 |
private ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException e) { |
|
53 |
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ExceptionUtil.getAllExceptionMsg(e)); |
|
54 |
} |
|
55 |
|
|
56 |
@ExceptionHandler(NacosRuntimeException.class) |
|
57 |
private ResponseEntity<String> handleNacosRuntimeException(NacosRuntimeException e) { |
|
58 |
LOGGER.error("got exception. {}", e.getMessage()); |
|
59 |
return ResponseEntity.status(e.getErrCode()).body(ExceptionUtil.getAllExceptionMsg(e)); |
|
60 |
} |
|
61 |
|
|
62 |
@ExceptionHandler(Exception.class) |
|
63 |
private ResponseEntity<Object> handleException(HttpServletRequest request, Exception e) { |
|
64 |
String uri = request.getRequestURI(); |
|
65 |
LOGGER.error("CONSOLE {}", uri, e); |
|
66 |
if (uri.contains(Commons.NACOS_SERVER_VERSION_V2)) { |
|
67 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) |
|
68 |
.body(RestResultUtils.failed(HtmlUtils.htmlEscape(ExceptionUtil.getAllExceptionMsg(e), "utf-8"))); |
|
69 |
} |
|
70 |
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) |
|
71 |
.body(HtmlUtils.htmlEscape(ExceptionUtil.getAllExceptionMsg(e), "utf-8")); |
|
72 |
} |
|
73 |
} |