From d07cece0d4f9d997bc8295ed966cfecc9bca5a44 Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 6 May 2024 15:52:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ExceptionFilter=E5=92=8CDefau?= =?UTF-8?q?ltExceptionFilter=E6=94=AF=E6=8C=81=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 +- .../server/filter/DefaultExceptionFilter.java | 37 ++++++++++++++++ .../http/server/filter/ExceptionFilter.java | 43 +++++++++++++++++++ .../http/server/ExceptionServerTest.java | 17 ++++++++ .../cn/hutool/http/server/Issue3568Test.java | 15 +++++++ 5 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 hutool-http/src/main/java/cn/hutool/http/server/filter/DefaultExceptionFilter.java create mode 100644 hutool-http/src/main/java/cn/hutool/http/server/filter/ExceptionFilter.java create mode 100644 hutool-http/src/test/java/cn/hutool/http/server/ExceptionServerTest.java create mode 100644 hutool-http/src/test/java/cn/hutool/http/server/Issue3568Test.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 27af7174e..b6e999539 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ # 🚀Changelog ------------------------------------------------------------------------------------------------------------- -# 5.8.28(2024-04-29) +# 5.8.28(2024-05-06) ### 🐣新特性 * 【core 】 修正XmlUtil的omitXmlDeclaration描述注释(issue#I9CPC7@Gitee) @@ -14,6 +14,7 @@ * 【core 】 NumberChineseFormatter提供阿拉伯转中文支持多位小数的方法(pr#3552@Github) * 【captcha】 Captcha.setBackground为null时背景透明(issue#3558@Github) * 【captcha】 HttpDownloader.downloadBytes增加超时参数重载(issue#3556@Github) +* 【http 】 增加ExceptionFilter和DefaultExceptionFilter支持异常处理(issue#3568@Github) ### 🐞Bug修复 * 【http 】 修复HttpUtil.urlWithFormUrlEncoded方法重复编码问题(issue#3536@Github) diff --git a/hutool-http/src/main/java/cn/hutool/http/server/filter/DefaultExceptionFilter.java b/hutool-http/src/main/java/cn/hutool/http/server/filter/DefaultExceptionFilter.java new file mode 100644 index 000000000..a5086189b --- /dev/null +++ b/hutool-http/src/main/java/cn/hutool/http/server/filter/DefaultExceptionFilter.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023. looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * https://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package cn.hutool.http.server.filter; + +import cn.hutool.core.exceptions.ExceptionUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.http.server.HttpServerRequest; +import cn.hutool.http.server.HttpServerResponse; + +/** + * 默认异常处理拦截器 + * + * @author looly + */ +public class DefaultExceptionFilter extends ExceptionFilter{ + + private final static String TEMPLATE_ERROR = "Hutool - Error report

HTTP Status {} - {}


{}


Hutool

"; + + @Override + public void afterException(final HttpServerRequest req, final HttpServerResponse res, final Throwable e) { + String content = ExceptionUtil.stacktraceToString(e); + content = content.replace("\n", "
\n"); + content = StrUtil.format(TEMPLATE_ERROR, 500, req.getURI(), content); + + res.sendError(500, content); + } +} diff --git a/hutool-http/src/main/java/cn/hutool/http/server/filter/ExceptionFilter.java b/hutool-http/src/main/java/cn/hutool/http/server/filter/ExceptionFilter.java new file mode 100644 index 000000000..1577a5537 --- /dev/null +++ b/hutool-http/src/main/java/cn/hutool/http/server/filter/ExceptionFilter.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023. looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * https://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package cn.hutool.http.server.filter; + +import cn.hutool.http.server.HttpServerRequest; +import cn.hutool.http.server.HttpServerResponse; +import com.sun.net.httpserver.Filter; + +/** + * 异常处理过滤器 + * + * @author looly + */ +public abstract class ExceptionFilter implements HttpFilter { + + @Override + public void doFilter(final HttpServerRequest req, final HttpServerResponse res, final Filter.Chain chain) { + try { + chain.doFilter(req.getHttpExchange()); + } catch (final Throwable e) { + afterException(req, res, e); + } + } + + /** + * 异常之后的处理逻辑 + * + * @param req {@link HttpServerRequest} + * @param res {@link HttpServerResponse} + * @param e 异常 + */ + public abstract void afterException(final HttpServerRequest req, final HttpServerResponse res, final Throwable e); +} diff --git a/hutool-http/src/test/java/cn/hutool/http/server/ExceptionServerTest.java b/hutool-http/src/test/java/cn/hutool/http/server/ExceptionServerTest.java new file mode 100644 index 000000000..96c3afe78 --- /dev/null +++ b/hutool-http/src/test/java/cn/hutool/http/server/ExceptionServerTest.java @@ -0,0 +1,17 @@ +package cn.hutool.http.server; + +import cn.hutool.http.HttpUtil; +import cn.hutool.http.server.filter.DefaultExceptionFilter; + +import java.io.IOException; + +public class ExceptionServerTest { + public static void main(final String[] args) { + HttpUtil.createServer(8888) + .addFilter(new DefaultExceptionFilter()) + .addAction("/", (req, res) -> { + throw new RuntimeException("Test Exception"); + }) + .start(); + } +} diff --git a/hutool-http/src/test/java/cn/hutool/http/server/Issue3568Test.java b/hutool-http/src/test/java/cn/hutool/http/server/Issue3568Test.java new file mode 100644 index 000000000..a836a70fc --- /dev/null +++ b/hutool-http/src/test/java/cn/hutool/http/server/Issue3568Test.java @@ -0,0 +1,15 @@ +package cn.hutool.http.server; + +import cn.hutool.http.HttpUtil; + +import java.io.IOException; + +public class Issue3568Test { + public static void main(String[] args) { + HttpUtil.createServer(8888) + .addHandler("/", httpExchange -> { + throw new IOException("111"); + }) + .start(); + } +}