承接 wyrihaximus/json-throwable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wyrihaximus/json-throwable

Composer 安装命令:

composer require wyrihaximus/json-throwable

包简介

📠 JSON encode and decode throwables and exceptions

README 文档

README

Continuous Integration Latest Stable Version Total Downloads Code Coverage Type Coverage License

Installation

To install via Composer, use the command below, it will automatically detect the latest version and bind it with ~.

composer require wyrihaximus/json-throwable

Usage

This package comes with four functions:

  • throwable_json_encode - Encodes any Throwable to a JSON string
  • throwable_encode - Encodes any Throwable to an array
  • throwable_json_decode - Decodes a JSON string in the format from throwable_json_encode into the original Exception or Error
  • throwable_decode - Decodes an array in the format from throwable_encode into the original Exception or Error

Heads up

There are a few gotchas when using this package.

  • Both the encoding functions drop the arguments from the trace.
  • Because we can't overwrite the trace, a new one will be placed in the originalTrace property when available.
  • Any previous Throwables will also be encoded and decoded but always with throwable_json_*.

Example of gaining access to the original trace, it isn't optimal, but it works:

<?php

declare(strict_types=1);

final class ExposeTraceException extends Exception
{
    /** @var array<array<string, mixed>> */
    private array $originalTrace = [];

    /**
     * @return array<array<string, mixed>>
     */
    public function getOriginalTrace(): array
    {
        return $this->originalTrace;
    }
}

Alternatively you can use the trait supplied by this package:

<?php

declare(strict_types=1);

use WyriHaximus\ExposeTraceTrait;

final class ExposeTraceException extends Exception
{
    use ExposeTraceTrait;
}

Including additional properties from throwables

If your throwables include any properties you'd want to take along when it gets encoded, implement the AdditionalPropertiesInterface returning a list of all properties you'd want to haved included in the encoding:

<?php

declare(strict_types=1);

use WyriHaximus\AdditionalPropertiesInterface;
use WyriHaximus\ExposeTraceTrait;

final class AdditionalPropertiesException extends Exception implements AdditionalPropertiesInterface
{
    use ExposeTraceTrait;

    private int $time;

    public function __construct(int $time)
    {
        parent::__construct('Additional properties exception raised');
        $this->time = $time;
    }

    public function time(): int
    {
        return $this->time;
    }

    /**
     * @return array<string>
     */
    public function additionalProperties(): array
    {
        return ['time'];
    }
}

Encode/Decode array type hint

When you're calling throwable_encode or throwable_decode the array (return) type hint has the following signature:

array{class: class-string<Throwable>, message: string, code: mixed, file: string, line: int, previous: string|null, originalTrace: array<int, mixed>, additionalProperties: array<string, string>}

This signature isn't enforce by PHP but tools like PHPStan or Psalm will use it to assert type safety from a static analysis pont of view.

Contributing

Please see CONTRIBUTING for details.

License

Copyright 2020 Cees-Jan Kiewiet

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

wyrihaximus/json-throwable 适用场景与选型建议

wyrihaximus/json-throwable 是一款 基于 Makefile 开发的 Composer 扩展包,目前已累计 277.26k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2017 年 10 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 wyrihaximus/json-throwable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 wyrihaximus/json-throwable 我们能提供哪些服务?
定制开发 / 二次开发

基于 wyrihaximus/json-throwable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 277.26k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 16
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 12
  • Watchers: 1
  • Forks: 1
  • 开发语言: Makefile

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-10-05