glance-project/error-middleware
Composer 安装命令:
composer require glance-project/error-middleware
包简介
Error middleware
README 文档
README
A PSR-7 and PSR-15
compatible middleware for handling errors
Installation
Install with Composer
composer require glance-project/error-handler
Getting started
The error handler should be the first middleware executed by your application. In Slim, the last middlewared registed is executed first.
$app = \Slim\App::create();
$logger = /* ... */;
$errorMiddleware = ErrorMiddleware::create();
$errorMiddleware->setLogger($logger); // Optional. Accepts any PSR-3 logger
$errorMiddleware->debugMode(true); // Optional. Set to false on production
$errorMiddleware->useSentry(); // Optional. Sentry must be installed and configured
$app->add(/* other middleware */);
$app->add($errorMiddleware);
/* Register routes here */
$app->run();
If the error handler is not the first registered middleware, exceptions on previous middleware will not be properly handled.
Exceptions
After initialized, all the uncaught exceptions are handled by this library.
The ErrorMiddleware behaves differently according to the type of the exception.
BaseException
Sometimes while developing an application, it is desired to abort everything and
return an error to the user. This library provides a BaseException which
encapsulates one or multiple errors.
An Error follows parts of the JSON API standard
with the following optional fields:
- Title
- Detail
- Code
- HTTP status
- Source
$errors = [
new Error("Invalid email", "Email is bad formatted."),
new Error(
"Invalid password",
"Password should contain at least 6 characters.",
12345,
400,
new ErrorSource("user/password")
),
];
throw new BaseException(400, $errors);
The code above on your application would produce the following HTTP response:
STATUS: 400
{
"status": 400,
"errors": [
{
"title": "Invalid email",
"details": "Email is bad formatted."
}
{
"title": "Invalid password",
"details": "Password should contain at least 6 characters.",
"code": 12345,
"status": 400,
"source": { "pointer": "user/password" }
}
]
}
You can also create your custom exceptions by extending the BaseException.
Other exceptions
Any other type of uncaught of exception will result on the following HTTP Response.
STATUS: 500
{
"errors": [
{
"status": 500,
"title": "Internal Error",
"detail": "An unexpected server error occurred."
}
]
}
If debugMode is on, additional debug information will be appended the the
response body, such as file, line, message and trace.
Using Sentry
To send unknown errors to Sentry, install and initialize sentry/sdk.
More information about setting up Sentry on PHP can be found at
the official documentation.
composer require sentry/sdk
<?php
\Sentry\init([
"dsn" => getenv("SENTRY_DSN"),
"environment" => getenv("ENVIRONMENT"),
]);
$app = \Slim\App::create();
$logger = /* ... */;
$errorMiddleware = ErrorMiddleware::create();
$errorMiddleware->useSentry();
$app->add(/* other middleware */);
$app->add($errorMiddleware);
// Register routes here...
$app->run();
glance-project/error-middleware 适用场景与选型建议
glance-project/error-middleware 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.97k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 02 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 glance-project/error-middleware 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 glance-project/error-middleware 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5.97k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2022-02-18