treblle/error-codes

一个帮助您提供一致错误代码的包。

0.0.1 2023-08-25 10:16 UTC

This package is auto-updated.

Last update: 2024-09-13 15:21:48 UTC


README

API错误代码

Latest Version PHP Version tests Total Downloads

集成   •   网站   •   文档   •   博客   •   Twitter   •   Discord

一个帮助您提供一致错误代码的包。

安装

composer require treblle/error-codes

使用方法

此包易于使用,您可以确定所需的错误代码。然后访问其相关属性。

use Treblle\ErrorCodes\Enums\ErrorCode;

$badRequest = ErrorCode::BAD_REQUEST;

$title = $badRequest->getDescription()->title; // Bad Request
$code = $badRequest->getDescription()->code; // HTTP_400
$link = $badRequest->getDescription()->link; // https://mdn.org.cn/en-US/docs/Web/HTTP/Status/400
$status = $badRequest->getDescription()->status; // 400

组合

此包与 treblle/api-responses 包配合使用效果很好。

final class Handler extends ExceptionHandler
{
    public function register(): void
    {
        $this->renderable(function (ModelNotFoundException $exception, Request $request) {
            $errorCode = \Treblle\ErrorCodes\Enums\ErrorCode::NOT_FOUND;
            
            return new ErrorResponse(
                data: new ApiError(
                    title: $errorCode->getDescription()->title,
                    detail: $exception->getMessage(),
                    instance: $request->path(),
                    code: $errorCode->getDescription()->code,
                    link: $errorCode->getDescription()->link,
                ),
                status: Status::NOT_FOUND,
            );
        });
    }
}

社区 💙

首先和最重要的是:请 星标并关注此存储库 以保持最新。

此外,请关注我们的 博客Twitter

您可以在 Discord 上与团队和其他成员聊天,并在 YouTube 上查看我们的教程和其他视频材料。

Treblle Discord

Treblle YouTube

Treblle on Twitter

如何贡献

以下是一些使 Treblle 更好的贡献方式

  • 尝试 Treblle,并让我们知道如何使 Treblle 对您更有帮助。在 Discord 上告诉我们。
  • 加入我们的 Discord 并与其他成员连接,以共享和学习。
  • 向我们的任何 开源仓库 发送拉取请求。请查阅您想贡献的仓库的贡献指南以获取更多有关如何贡献的详细信息。我们期待您的贡献!

测试

运行测试套件

composer run test

鸣谢

A table of avatars from the project's contributors

许可

MIT 许可(MIT)。有关更多信息,请参阅许可文件