Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: supports exception page template for theme-side #2925

Merged
merged 9 commits into from
Dec 15, 2022

Conversation

guqing
Copy link
Member

@guqing guqing commented Dec 12, 2022

What type of PR is this?

/kind feature
/area core

What this PR does / why we need it:

主题端支持异常模板页面

异常模板必须放在主题目录的 templates/error 目录下:

  • 支持按照 response status 名称模板页面,例如 404.html ,当发生 404 错误时会使用 404.html
  • 支持 4xx.html、5xx.html,例如当发生 403 错误时,如果存在 403.html 则使用此页面,否则使用 4xx.html
    error 模板中具有 model 示例:
{
    "error": {
        "type": "about:blank",
        "title": "Not Found",
        "status": 404,
        "detail": "Extension run.halo.app.core.extension.Plugin with name amet ut magn not found",
        "instance": "/apis/plugin.halo.run/v1alpha1/plugins/amet%20ut%20magn"
    }
}

Which issue(s) this PR fixes:

Fixes #2690

Special notes for your reviewer:

/cc @halo-dev/sig-halo

Does this PR introduce a user-facing change?

主题端支持异常模板页面

@f2c-ci-robot f2c-ci-robot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. area/core Issues or PRs related to the Halo Core labels Dec 12, 2022
@ruibaby
Copy link
Member

ruibaby commented Dec 13, 2022

我将尝试在 https://github.com/halo-dev/theme-earth 实现这个模板。

Copy link
Member

@JohnNiang JohnNiang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

能否支持缺省的错误模板,而不是默认的 Whitelabel Error Page,尤其是主题没有适配这些模板的情况很有意义。

@guqing
Copy link
Member Author

guqing commented Dec 13, 2022

能否支持缺省的错误模板,而不是默认的 Whitelabel Error Page,尤其是主题没有适配这些模板的情况很有意义。

缺省的错误页面显示什么呢,是否需要样式设计或者只单纯显示一个 message 和 status code

@JohnNiang
Copy link
Member

缺省的错误页面显示什么呢,是否需要样式设计或者只单纯显示一个 message 和 status code

显示什么不重要,先能够支持在 Core 中定义缺省错误模板即可。

@ruibaby
Copy link
Member

ruibaby commented Dec 13, 2022

@guqing 可以使用 1.x 的默认模板。

@guqing
Copy link
Member Author

guqing commented Dec 13, 2022

缺省的错误页面显示什么呢,是否需要样式设计或者只单纯显示一个 message 和 status code

显示什么不重要,先能够支持在 Core 中定义缺省错误模板即可。

ok

@guqing
Copy link
Member Author

guqing commented Dec 13, 2022

@guqing 可以使用 1.x 的默认模板。

可以

@ruibaby
Copy link
Member

ruibaby commented Dec 13, 2022

@guqing 可以在 PR 描述里面补充一下 error 模板都有哪些数据,不然我等萌新不太容易知道,也方便后续编写主题开发文档。

@guqing
Copy link
Member Author

guqing commented Dec 13, 2022

@guqing 可以在 PR 描述里面补充一下 error 模板都有哪些数据,不然我等萌新不太容易知道,也方便后续编写主题开发文档。

好 写了

@guqing guqing requested a review from JohnNiang December 13, 2022 04:31
@ruibaby
Copy link
Member

ruibaby commented Dec 13, 2022

trace 一般什么情况下会有值呢,我这边估计写错语法啥的似乎 trace 没有内容。

@guqing
Copy link
Member Author

guqing commented Dec 13, 2022

trace 一般什么情况下会有值呢,我这边估计写错语法啥的似乎 trace 没有内容。

trace 是异常堆栈 一般不需要显示到页面,如果 trace 没值可以开启 debug 试一下

Copy link
Member

@ruibaby ruibaby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

2.1.x 时,需要更新主题开发文档:halo-dev/docs#144

@f2c-ci-robot f2c-ci-robot bot added the lgtm Indicates that a PR is ready to be merged. label Dec 13, 2022
@f2c-ci-robot f2c-ci-robot bot removed the lgtm Indicates that a PR is ready to be merged. label Dec 14, 2022
@JohnNiang
Copy link
Member

如果错误模板能够拿到 ProblemDetail 会不会更好呢?

@guqing
Copy link
Member Author

guqing commented Dec 14, 2022

如果错误模板能够拿到 ProblemDetail 会不会更好呢?

可以的,非常有道理

@ruibaby
Copy link
Member

ruibaby commented Dec 14, 2022

能否直接将 ProblemDetail 直接当做一个对象给到模板呢,目前会有这样的问题:

我的模板:

<!DOCTYPE html>
<html
  xmlns:th="https://www.thymeleaf.org"
  th:replace="~{modules/layout :: html(title = ${status} + ' | ' + ${#strings.defaultString(title, 'Internal server error')} + ' - ' +${site.title}, header = null, content = ~{::content}, head = null, footer = null, sidebar = null, contentClass = null)}"
>
  <th:block th:fragment="content">
    <section th:fragment="exception" class="flex items-center dark:bg-gray-900 dark:text-gray-100">
      <div class="container mx-auto my-8 flex flex-col items-center justify-center px-5">
        <div class="max-w-md text-center">
          <h2 class="mb-8 text-9xl font-extrabold dark:text-gray-600" th:text="${status}"></h2>
          <p class="mb-8 text-2xl font-semibold md:text-3xl" th:text="${#strings.defaultString(title, 'Internal server error')}"></p>
          <a
            th:href="@{${site.url}}"
            class="whitespace-no-wrap group inline-flex items-center justify-center gap-1 rounded-md border border-gray-200 bg-white px-4 py-1 text-sm font-medium leading-6 text-gray-600 shadow-sm hover:bg-gray-50 focus:shadow-none focus:outline-none dark:border-slate-600 dark:bg-slate-700 dark:text-slate-100 dark:hover:bg-slate-600 dark:hover:text-white"
          >
            <span class="i-tabler-arrow-left text-lg transition-all group-hover:-translate-x-1"></span>
            <span>返回首页</span>
          </a>
        </div>
      </div>
    </section>
  </th:block>
</html>

由于在引入 modules/layout 的时候传入了 title 字段给 layout.html 模板当做页面标题,然后错误页面模板的 title 参数被覆盖:

image

目前我不能确定这是 Thymeleaf 的 bug 还是我的写法问题。

@guqing
Copy link
Member Author

guqing commented Dec 14, 2022

能否直接将 ProblemDetail 直接当做一个对象给到模板呢,目前会有这样的问题:

这恐怕不得行,这是 springboot 约束的

	/**
	 * Render the given error data as a view, using a template view if available or a
	 * static HTML file if available otherwise. This will return an empty
	 * {@code Publisher} if none of the above are available.
	 * @param viewName the view name
	 * @param responseBody the error response being built
	 * @param error the error data as a map
	 * @return a Publisher of the {@link ServerResponse}
	 */
	protected Mono<ServerResponse> renderErrorView(String viewName, ServerResponse.BodyBuilder responseBody,
			Map<String, Object> error) {
		if (isTemplateAvailable(viewName)) {
			return responseBody.render(viewName, error);
		}
		Resource resource = resolveResource(viewName);
		if (resource != null) {
			return responseBody.body(BodyInserters.fromResource(resource));
		}
		return Mono.empty();
	}

除非包裹一个 kv 给这个 error map 为 (String, ProblemDetail)

@JohnNiang
Copy link
Member

可以包装成这样 {"error": problem-detail},为了减少字段的冲突。

@guqing
Copy link
Member Author

guqing commented Dec 14, 2022

可以包装成这样 {"error": problem-detail},为了减少字段的冲突。

好的

Copy link
Member

@ruibaby ruibaby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve

@f2c-ci-robot f2c-ci-robot bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 14, 2022
f2c-ci-robot bot pushed a commit to halo-dev/theme-earth that referenced this pull request Dec 14, 2022
Copy link
Member

@JohnNiang JohnNiang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@f2c-ci-robot f2c-ci-robot bot added the lgtm Indicates that a PR is ready to be merged. label Dec 15, 2022
@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Dec 15, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: JohnNiang, ruibaby

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@f2c-ci-robot f2c-ci-robot bot merged commit 686aece into halo-dev:main Dec 15, 2022
@guqing guqing deleted the feature/2690 branch December 15, 2022 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/core Issues or PRs related to the Halo Core kind/feature Categorizes issue or PR as related to a new feature. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

主题端支持异常页面模板
3 participants