Skip to content

Commit

Permalink
Added data parameter to TryCatch exception handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Nov 1, 2017
1 parent e7f28c8 commit ddc6a99
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ php:
- 5.6
- 7.0
- 7.1
- 7.2

env:
matrix:
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -733,11 +733,11 @@ Merge(Strategy|Mapping|array|mixed $first, Strategy|Mapping|array|mixed $second)
### Replace

Replaces all occurrences one or more substrings.
Replaces all occurrences of one or more substrings.

Any number of searches and replacements can be specified. Searches and replacements are parsed in pairs. If no replacements are specified, all matches are removed instead of replaced. If fewer replacements than searches are specified, the last replacement will be used for the remaining searches. If more replacements than searches are specified, the extra replacements are ignored.

Searches can be specified as either string literals or wrapped in an `Expression` and treated as a regular expression. `Expression` and string searches can be mixed as desired. Regular expression replacements can reference sub-matches, e.g. `$1`.
Searches can be specified as either string literals or wrapped in an `Expression` and treated as a regular expression. `Expression` and string searches can be mixed as desired. Regular expression replacements can reference sub-matches, e.g. `$1` specifies the first capturing group.

#### Signature

Expand Down Expand Up @@ -830,7 +830,7 @@ TryCatch(Strategy $strategy, callable $handler, Strategy|Mapping|array|mixed $ex
```

1. `$strategy` – Primary strategy.
2. `$handler` – Exception handler that receives the thrown exception as its first argument.
2. `$handler` – Exception handler that receives the thrown exception as its first argument and data as its second.
3. `$expression` – Fallback expression.

#### Examples
Expand All @@ -844,7 +844,7 @@ TryCatch(Strategy $strategy, callable $handler, Strategy|Mapping|array|mixed $ex
throw new \DomainException;
}
),
function (\Exception $exception) {
function (\Exception $exception, array $data) {
if (!$exception instanceof \DomainException) {
throw $exception;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Strategy/Replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use ScriptFUSION\Mapper\Mapping;

/**
* Replaces one or more substrings.
* Replaces all occurrences of one or more substrings.
*/
class Replace extends Delegate
{
Expand Down
2 changes: 1 addition & 1 deletion src/Strategy/TryCatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __invoke($data, $context = null)
try {
return parent::__invoke($data, $context);
} catch (\Exception $exception) {
call_user_func($this->handler, $exception);
call_user_func($this->handler, $exception, $data);

return $this->delegate($this->expression, $data, $context);
}
Expand Down
22 changes: 12 additions & 10 deletions test/Integration/Mapper/Strategy/TryCatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class TryCatchTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
$this->callback = new Callback(function ($data) {
$this->callback = new Callback(function (array $data) {
if ($data[0] instanceof \Exception) {
throw $data[0];
}
Expand All @@ -26,9 +26,11 @@ public function testTryCatch()
$tryCatch = (
new TryCatch(
$this->callback,
function (\Exception $e) {
if (!$e instanceof \DomainException) {
throw $e;
function (\Exception $exception, array $data) {
self::assertNotEmpty($data);

if (!$exception instanceof \DomainException) {
throw $exception;
}
},
$fallback = 'bar'
Expand All @@ -49,16 +51,16 @@ public function testNestedTryCatch()
new TryCatch(
new TryCatch(
$this->callback,
function (\Exception $e) {
if (!$e instanceof \DomainException) {
throw $e;
function (\Exception $exception) {
if (!$exception instanceof \DomainException) {
throw $exception;
}
},
$innerFallback = 'bar'
),
function (\Exception $e) {
if (!$e instanceof \LogicException) {
throw $e;
function (\Exception $exception) {
if (!$exception instanceof \LogicException) {
throw $exception;
}
},
$outerFallback = 'baz'
Expand Down

0 comments on commit ddc6a99

Please sign in to comment.