为了方便排查错误,系统默认开启了错误信息显示。
前台错误页面的模板
前台访问发生错误时,错误页面的模板位置:
src/main/webapp/templates/error
(源码中错误模板的位置)templates/error
(war部署时错误模板的位置)static/templates/error
(jar部署时错误模板的位置)
后台错误信息的处理代码
后台发生错误时,错误信息的处理代码为前端项目 ujcms-cp
的 src/utils/request.ts
文件,相关代码为:
export const handleError = ({ timestamp, message, path, error, exception, trace, status }: ErrorInfo): void => {
if (exception === 'com.ujcms.cms.core.web.support.SiteForbiddenException') {
//没有当前站点权限,清空站点信息,刷新页面以获取默认站点
useCurrentSiteStore().setCurrentSiteId(null);
window.location.reload();
} else if (exception === 'com.ujcms.commons.web.exception.LogicException') {
ElMessageBox.alert(message, { type: 'warning' });
} else if (status === 401) {
removeAccessToken();
removeRefreshToken();
showMessageBox();
} else if (status === 403) {
ElMessageBox({
title: String(status),
message: h('div', null, [h('p', { class: 'text-lg' }, t('error.forbidden')), h('p', { class: 'mt-2' }, path), h('p', { class: 'mt-2' }, message)]),
});
} else {
ElMessageBox({
title: t('error.title'),
message: h('div', null, [
h('h', null, [h('span', { class: 'text-4xl' }, status), h('span', { class: ['ml-2', 'text-xl'] }, error)]),
h('p', { class: 'mt-2' }, dayjs(timestamp).format('YYYY-MM-DD HH:mm:ss')),
h('p', { class: 'mt-2' }, path),
h('p', { class: 'mt-2' }, message),
h('p', { class: 'mt-2' }, exception),
h('pre', { class: 'mt-2' }, [h('code', { class: ['whitespace-pre-wrap'] }, trace)]),
]),
customStyle: { maxWidth: '100%' },
});
}
};
错误信息配置
spring boot 是错误信息产生的源头,可以通过设置 spring boot 相关配置,以控制错误信息的显示。修改 application.yaml
以下内容:
# 错误处理类 org.springframework.boot.autoconfigure.web.servlet.error
# 错误处理是否包含 "exception" 属性。Include the "exception" attribute
# 默认为 false
server.error.include-exception: true
# 错误处理是否包含 "message" 属性。Include the "message" attribute
# 开发环境默认为 always,生产环境默认为 never。如不需要包含 message,可注释该行,或设置为 never
server.error.include-message: always
# 错误处理是否包含 "stacktrace" (trace?) 属性。When to include a "stacktrace" attribute
# 开发环境默认为 always,生产环境默认为 never。如不需要包含 stacktrace,可注释该行,或设置为 never
server.error.include-stacktrace: always
如有安全相关需求,通常关闭错误堆栈信息 server.error.include-stacktrace
即可。