Skip to content

Commit

Permalink
refactor: error model
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing committed Dec 14, 2022
1 parent 4991018 commit 8618036
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package run.halo.app.infra.exception.handlers;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.lang.reflect.Method;
import java.net.URI;
Expand Down Expand Up @@ -58,8 +57,6 @@ public class GlobalErrorWebExceptionHandler extends DefaultErrorWebExceptionHand

private final ThemeResolver themeResolver;

private final ObjectMapper objectMapper;

static {
Map<HttpStatus.Series, String> views = new EnumMap<>(HttpStatus.Series.class);
views.put(HttpStatus.Series.CLIENT_ERROR, "4xx");
Expand All @@ -83,7 +80,6 @@ public GlobalErrorWebExceptionHandler(ErrorAttributes errorAttributes,
super(errorAttributes, resources, errorProperties, applicationContext);
this.errorProperties = errorProperties;
this.themeResolver = applicationContext.getBean(ThemeResolver.class);
this.objectMapper = applicationContext.getBean(ObjectMapper.class);
}

@Override
Expand Down Expand Up @@ -121,7 +117,7 @@ protected Mono<ServerResponse> renderErrorView(ServerRequest request) {
ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(errorStatus),
(String) errorAttributes.get("message"));
problemDetail.setInstance(URI.create(request.path()));
Map<String, Object> error = problemDetailMap(problemDetail);
Map<String, Object> error = Map.of("error", problemDetail);

ServerResponse.BodyBuilder responseBody =
ServerResponse.status(errorStatus).contentType(TEXT_HTML_UTF8);
Expand All @@ -144,11 +140,6 @@ protected void logError(ServerRequest request, ServerResponse response, Throwabl
}
}

private Map<String, Object> problemDetailMap(ProblemDetail problemDetail) {
return objectMapper.convertValue(problemDetail, new TypeReference<>() {
});
}

private String formatRequest(ServerRequest request) {
String rawQuery = request.uri().getRawQuery();
String query = StringUtils.hasText(rawQuery) ? "?" + rawQuery : "";
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/templates/error/error.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title th:text="${status} + ' | ' + ${#strings.defaultString(title, 'Internal server error')}"></title>
<title th:text="${error?.status} + ' | ' + ${#strings.defaultString(error?.title, 'Internal server error')}"></title>
<style>
body {
padding: 30px 20px;
Expand Down Expand Up @@ -117,9 +117,9 @@
<body>

<div class="container">
<h2 th:text="${status}"></h2>
<h1 class="title" th:text="${#strings.defaultString(title, 'Internal server error')}"></h1>
<p th:text="${#strings.defaultString(detail, '未知错误!可能存在的原因:未正确设置主题或主题文件缺失。')}"></p>
<h2 th:text="${error.status}"></h2>
<h1 class="title" th:text="${#strings.defaultString(error.title, 'Internal server error')}"></h1>
<p th:text="${#strings.defaultString(error.detail, '未知错误!可能存在的原因:未正确设置主题或主题文件缺失。')}"></p>
<div class="back-home">
<button th:onclick="window.location.href='[(${site.url})]'"
th:text="首页">
Expand Down

0 comments on commit 8618036

Please sign in to comment.