2017 © Pedro Peláez
 

library json-throwable

JSON encode and decode throwables and exceptions

image

wyrihaximus/json-throwable

JSON encode and decode throwables and exceptions

  • Thursday, March 15, 2018
  • by WyriHaximus
  • Repository
  • 1 Watchers
  • 1 Stars
  • 6,688 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 72 % Grown

The README.md

JSON encode and decode throwables and exceptions

Continuous Integration Latest Stable Version Total Downloads Code Coverage Type Coverage License, (*1)

Installation

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

composer require wyrihaximus/json-throwable

Usage

This package comes with four functions:, (*3)

  • 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., (*4)

  • 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:, (*5)

<?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:, (*6)

<?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:, (*7)

<?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:, (*8)

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., (*9)

Contributing

Please see CONTRIBUTING for details., (*10)

License

Copyright 2020 Cees-Jan Kiewiet, (*11)

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:, (*12)

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

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., (*14)

The Versions

15/03 2018

dev-master

9999999-dev

JSON encode and decode throwables and exceptions

  Sources   Download

MIT

The Requires

 

The Development Requires

15/03 2018

2.0.0

2.0.0.0

JSON encode and decode throwables and exceptions

  Sources   Download

MIT

The Requires

 

The Development Requires

26/02 2018

1.0.3

1.0.3.0

JSON encode and decode throwables and exceptions

  Sources   Download

MIT

The Requires

  • php ^7.0 || ^5.4

 

The Development Requires

25/02 2018

1.0.2

1.0.2.0

JSON encode and decode throwables and exceptions

  Sources   Download

MIT

The Requires

  • php ^7.0 || ^5.4

 

The Development Requires

25/02 2018

1.0.1

1.0.1.0

JSON encode and decode throwables and exceptions

  Sources   Download

MIT

The Requires

  • php ^7.0 || ^5.4

 

The Development Requires

19/02 2018

1.0.0

1.0.0.0

JSON encode and decode throwables and exceptions

  Sources   Download

MIT

The Requires

  • php ^7.0 || ^5.4

 

The Development Requires