Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wrr-cat
apollo
提交
5219b5c6
apollo
项目概览
wrr-cat
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
apollo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5219b5c6
编写于
8月 15, 2016
作者:
J
Jason Song
提交者:
GitHub
8月 15, 2016
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #380 from nobodyiam/reformat
reformat
上级
07bd7c06
8fa2cc07
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
73 addition
and
72 deletion
+73
-72
apollo-common/src/main/java/com/ctrip/framework/apollo/common/controller/GlobalDefaultExceptionHandler.java
...ollo/common/controller/GlobalDefaultExceptionHandler.java
+73
-72
未找到文件。
apollo-common/src/main/java/com/ctrip/framework/apollo/common/controller/GlobalDefaultExceptionHandler.java
浏览文件 @
5219b5c6
...
...
@@ -3,6 +3,9 @@ package com.ctrip.framework.apollo.common.controller;
import
com.google.gson.Gson
;
import
com.google.gson.reflect.TypeToken
;
import
com.ctrip.framework.apollo.common.exception.AbstractApolloHttpException
;
import
com.dianping.cat.Cat
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.http.HttpHeaders
;
...
...
@@ -15,9 +18,6 @@ import org.springframework.web.bind.annotation.ControllerAdvice;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.client.HttpStatusCodeException
;
import
com.ctrip.framework.apollo.common.exception.AbstractApolloHttpException
;
import
com.dianping.cat.Cat
;
import
java.lang.reflect.Type
;
import
java.time.LocalDateTime
;
import
java.time.format.DateTimeFormatter
;
...
...
@@ -34,74 +34,75 @@ import static org.springframework.http.MediaType.APPLICATION_JSON;
@ControllerAdvice
public
class
GlobalDefaultExceptionHandler
{
private
Gson
gson
=
new
Gson
();
private
static
Type
mapType
=
new
TypeToken
<
Map
<
String
,
Object
>>()
{}.
getType
();
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
GlobalDefaultExceptionHandler
.
class
);
//处理系统内置的Exception
@ExceptionHandler
(
Throwable
.
class
)
public
ResponseEntity
<
Map
<
String
,
Object
>>
exception
(
HttpServletRequest
request
,
Throwable
ex
)
{
return
handleError
(
request
,
INTERNAL_SERVER_ERROR
,
ex
);
}
@ExceptionHandler
({
HttpRequestMethodNotSupportedException
.
class
,
HttpMediaTypeException
.
class
})
public
ResponseEntity
<
Map
<
String
,
Object
>>
badRequest
(
HttpServletRequest
request
,
ServletException
ex
)
{
return
handleError
(
request
,
BAD_REQUEST
,
ex
);
}
@ExceptionHandler
(
HttpStatusCodeException
.
class
)
public
ResponseEntity
<
Map
<
String
,
Object
>>
restTemplateException
(
HttpServletRequest
request
,
HttpStatusCodeException
ex
)
{
return
handleError
(
request
,
ex
.
getStatusCode
(),
ex
);
}
@ExceptionHandler
(
AccessDeniedException
.
class
)
public
ResponseEntity
<
Map
<
String
,
Object
>>
accessDeny
(
HttpServletRequest
request
,
AccessDeniedException
ex
)
{
return
handleError
(
request
,
FORBIDDEN
,
ex
);
}
//处理自定义Exception
@ExceptionHandler
({
AbstractApolloHttpException
.
class
})
public
ResponseEntity
<
Map
<
String
,
Object
>>
badRequest
(
HttpServletRequest
request
,
AbstractApolloHttpException
ex
)
{
return
handleError
(
request
,
ex
);
}
private
ResponseEntity
<
Map
<
String
,
Object
>>
handleError
(
HttpServletRequest
request
,
AbstractApolloHttpException
ex
)
{
return
handleError
(
request
,
ex
.
getHttpStatus
(),
ex
);
}
private
ResponseEntity
<
Map
<
String
,
Object
>>
handleError
(
HttpServletRequest
request
,
HttpStatus
status
,
Throwable
ex
)
{
String
message
=
ex
.
getMessage
();
logger
.
error
(
message
,
ex
);
Cat
.
logError
(
ex
);
Map
<
String
,
Object
>
errorAttributes
=
new
HashMap
<>();
//如果是admin server引起的异常,则显示内部的异常信息
if
(
ex
instanceof
HttpStatusCodeException
){
errorAttributes
=
gson
.
fromJson
(((
HttpStatusCodeException
)
ex
).
getResponseBodyAsString
(),
mapType
);
status
=
((
HttpStatusCodeException
)
ex
).
getStatusCode
();
}
else
{
errorAttributes
.
put
(
"status"
,
status
.
value
());
errorAttributes
.
put
(
"message"
,
message
);
errorAttributes
.
put
(
"timestamp"
,
LocalDateTime
.
now
().
format
(
DateTimeFormatter
.
ISO_LOCAL_DATE_TIME
));
errorAttributes
.
put
(
"exception"
,
ex
.
getClass
().
getName
());
}
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
APPLICATION_JSON
);
return
new
ResponseEntity
<>(
errorAttributes
,
headers
,
status
);
}
private
Gson
gson
=
new
Gson
();
private
static
Type
mapType
=
new
TypeToken
<
Map
<
String
,
Object
>>()
{
}.
getType
();
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
GlobalDefaultExceptionHandler
.
class
);
//处理系统内置的Exception
@ExceptionHandler
(
Throwable
.
class
)
public
ResponseEntity
<
Map
<
String
,
Object
>>
exception
(
HttpServletRequest
request
,
Throwable
ex
)
{
return
handleError
(
request
,
INTERNAL_SERVER_ERROR
,
ex
);
}
@ExceptionHandler
({
HttpRequestMethodNotSupportedException
.
class
,
HttpMediaTypeException
.
class
})
public
ResponseEntity
<
Map
<
String
,
Object
>>
badRequest
(
HttpServletRequest
request
,
ServletException
ex
)
{
return
handleError
(
request
,
BAD_REQUEST
,
ex
);
}
@ExceptionHandler
(
HttpStatusCodeException
.
class
)
public
ResponseEntity
<
Map
<
String
,
Object
>>
restTemplateException
(
HttpServletRequest
request
,
HttpStatusCodeException
ex
)
{
return
handleError
(
request
,
ex
.
getStatusCode
(),
ex
);
}
@ExceptionHandler
(
AccessDeniedException
.
class
)
public
ResponseEntity
<
Map
<
String
,
Object
>>
accessDeny
(
HttpServletRequest
request
,
AccessDeniedException
ex
)
{
return
handleError
(
request
,
FORBIDDEN
,
ex
);
}
//处理自定义Exception
@ExceptionHandler
({
AbstractApolloHttpException
.
class
})
public
ResponseEntity
<
Map
<
String
,
Object
>>
badRequest
(
HttpServletRequest
request
,
AbstractApolloHttpException
ex
)
{
return
handleError
(
request
,
ex
);
}
private
ResponseEntity
<
Map
<
String
,
Object
>>
handleError
(
HttpServletRequest
request
,
AbstractApolloHttpException
ex
)
{
return
handleError
(
request
,
ex
.
getHttpStatus
(),
ex
);
}
private
ResponseEntity
<
Map
<
String
,
Object
>>
handleError
(
HttpServletRequest
request
,
HttpStatus
status
,
Throwable
ex
)
{
String
message
=
ex
.
getMessage
();
logger
.
error
(
message
,
ex
);
Cat
.
logError
(
ex
);
Map
<
String
,
Object
>
errorAttributes
=
new
HashMap
<>();
//如果是admin server引起的异常,则显示内部的异常信息
if
(
ex
instanceof
HttpStatusCodeException
)
{
errorAttributes
=
gson
.
fromJson
(((
HttpStatusCodeException
)
ex
).
getResponseBodyAsString
(),
mapType
);
status
=
((
HttpStatusCodeException
)
ex
).
getStatusCode
();
}
else
{
errorAttributes
.
put
(
"status"
,
status
.
value
());
errorAttributes
.
put
(
"message"
,
message
);
errorAttributes
.
put
(
"timestamp"
,
LocalDateTime
.
now
().
format
(
DateTimeFormatter
.
ISO_LOCAL_DATE_TIME
));
errorAttributes
.
put
(
"exception"
,
ex
.
getClass
().
getName
());
}
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
setContentType
(
APPLICATION_JSON
);
return
new
ResponseEntity
<>(
errorAttributes
,
headers
,
status
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录