圆通-http-error
说明
创建可被捕获并转换为错误响应的 http 错误 .
所需资源
- 节点: 22
安装
穿过 国家预防机制 作为 @chubbyts/chubbyts-http-error (英语)。 。 。 .
npm i @chubbyts/chubbyts-http-error@^3.6.0
使用量
import { createNotFound } from '@chubbyts/chubbyts-http-error/http-error';
try {
throw createNotFound({ detail: 'Something went wrong', instance: 'server-1', otherKey: 'otherValue' });
} catch (e) {
console.log(e);
}
数据
detail, (中文(简体) ),instance: 问题细节 (RFC 9457 (韩语)(中文(简体) )headers: 错误中间软件应该设置在错误响应(如.www-authenticate, (中文(简体) ),allow, (中文(简体) ),retry-after),从身体的一部分- 其它任何密钥: 额外的问题细节,带有
undefined值被跳过,保留密钥(type, (中文(简体) ),status, (中文(简体) ),title,...) 被忽略
中间软件出错
import { isHttpError } from '@chubbyts/chubbyts-http-error/http-error';
const errorMiddleware: Middleware = async (request, handler) => {
try {
return await handler(request);
} catch (e) {
if (isHttpError(e)) {
const { headers, _httpError, ...body } = e;
return new Response(JSON.stringify(body), {
status: e.status,
headers: { 'content-type': 'application/problem+json', ...headers },
});
}
throw e;
}
};
版权
2026 道米尼克·佐格(英语: