Get support for WyriHaximus/php-json-throwable
If you're new to LTH, please see our FAQ for more information on what it is we do.
Support Options
Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:
View Open IssuesTake a look to see if anyone else has experienced the same issue as you and if they managed to solve it.
Open an IssueMake sure to read any relevant guidelines for opening issues on this repo before posting a new issue.
Sponsor directlyCheck out the page and see if there are any options to sponsor this project or it's developers directly.
WyriHaximus/php-json-throwable
JSON encode and decode throwables and exceptions
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 fromthrowable_json_encode
into the originalException
orError
-
throwable_decode
- Decodes an array in the format fromthrowable_encode
into the originalException
orError
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
Throwable
s will also be encoded and decoded but always withthrowable_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.
Our Mission
We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.
From the Blog
Interesting Articles
-
Generating income from open source
Jun 23 • 8 min read
-
2023 State of OSS
Apr 23 • 45 min read ★
-
A funding experiment...
Aug 19 • 10 min read
-
But You Said I could
Aug 19 • 2 min read
Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated
p-e622a1a2