Skip to content

Commit 07923a3

Browse files
Gounlafcrazycodr
authored andcommitted
fix: add previous Exception for #19 and #20 (#22)
Signed-off-by: Florian Levis <[email protected]>
1 parent 1e6f26b commit 07923a3

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

Helpers/HttpExceptionFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,19 @@ public static function build(int $responseCode, string $message = '', Throwable
7171
* @param int $responseCode
7272
* @param mixed $context mixed data you can attach to the exception
7373
* @param string|null $message
74+
* @param Throwable|null $ex [optional] The previous throwable used for the exception chaining.
7475
* @return HttpExceptionInterface
7576
*
7677
* @throws ValidationException When {$responseCode} can't be mapped to an HttpException
7778
*/
78-
public static function buildWithContext(int $responseCode, $context, string $message = ''): HttpExceptionInterface
79+
public static function buildWithContext(int $responseCode, $context, string $message = '', Throwable $ex = null): HttpExceptionInterface
7980
{
8081
$mapping = static::getMapping();
8182
if (!array_key_exists($responseCode, $mapping)) {
8283
throw new ValidationException('Unknown mapping for response code ' . $responseCode);
8384
}
8485

85-
return $mapping[$responseCode]::withContext($context, $message, $responseCode);
86+
return $mapping[$responseCode]::withContext($context, $message, $responseCode, $ex);
8687
}
8788

8889
/**

Helpers/WithContext.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Exceptions\Helpers;
34

45
/**
@@ -21,15 +22,16 @@ trait WithContext
2122
* If you do not provide a message and/or code, it will use the getMessage and getCode interface methods to
2223
* retrieve the defaults for that exception.
2324
*
24-
* @param mixed context to save to the exception
25+
* @param $context
2526
* @param string $message to use, if null, will get the default exception message
2627
* @param int $code to use, if null, will get the default exception code
28+
* @param \Throwable|null $previous [optional] The previous throwable used for the exception chaining.
2729
*
2830
* @return static
2931
*/
30-
public static function withContext($context, string $message = null, int $code = null)
32+
public static function withContext($context, string $message = null, int $code = null, \Throwable $previous = null)
3133
{
32-
$ex = new static($message ?? static::getDefaultMessage(), $code ?? static::getDefaultCode());
34+
$ex = new static($message ?? static::getDefaultMessage(), $code ?? static::getDefaultCode(), $previous);
3335
$ex->context = $context;
3436
return $ex;
3537
}

Tests/HttpExceptionFactoryTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ public function testBuildFailed()
3838
public function testBuildWithContext(int $errorCode, string $expectedClass)
3939
{
4040
$ctx = new stdClass();
41-
$exception = HttpExceptionFactory::buildWithContext($errorCode, $ctx);
41+
$message = 'test';
42+
$previousException = new \Exception();
43+
$exception = HttpExceptionFactory::buildWithContext($errorCode, $ctx, $message, $previousException);
4244
$this->assertInstanceOf($expectedClass, $exception);
4345
$this->assertSame($ctx, $exception->getContext());
46+
$this->assertSame('test', $exception->getMessage());
47+
$this->assertSame($previousException, $exception->getPrevious());
4448
}
4549

4650
public function testBuildWithContextFailed()

composer.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@
2929
},
3030
"config": {
3131
"sort-packages": true,
32-
"optimize-autoloader": true,
33-
"platform": {
34-
"php": "7.3"
35-
}
32+
"optimize-autoloader": true
3633
},
3734
"scripts": {
3835
"test": "vendor/bin/phpunit",

0 commit comments

Comments
 (0)