-
Notifications
You must be signed in to change notification settings - Fork 57
Allow opt-in to raising exceptions instead of error middleware #78
Changes from 4 commits
0d8d50a
dd82b23
2ed65c0
c258193
d8f38eb
01b9fb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,14 @@ | |
*/ | ||
class Dispatch | ||
{ | ||
/** | ||
* Flag indicating whether or not to raise throwables during dispatch; when | ||
* false, a try/catch block is used instead (default behavior). | ||
* | ||
* @var bool | ||
*/ | ||
private $raiseThrowables = false; | ||
|
||
/** | ||
* @var ResponseInterface | ||
*/ | ||
|
@@ -103,6 +111,17 @@ public function process(Route $route, RequestInterface $request, callable $next) | |
return $this->dispatchInteropMiddleware($route->handler, $next, $request); | ||
} | ||
|
||
/** | ||
* Enables the "raise throwables", causing this instance to raise | ||
* throwables instead of catch them. | ||
* | ||
* @return void | ||
*/ | ||
public function raiseThrowables() | ||
{ | ||
$this->raiseThrowables = true; | ||
} | ||
|
||
/** | ||
* Set a response prototype to use when invoking callable middleware following http-interop middleware. | ||
* | ||
|
@@ -193,6 +212,18 @@ private function dispatchCallableMiddleware( | |
break; | ||
} | ||
|
||
if ($this->raiseThrowables) { | ||
if ($hasError && $arity === 4) { | ||
return $middleware($err, $request, $response, $next); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not tested. Not sure if it's needed though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably not, as it's the same workflow as what's in the try/catch block... and we're recommending against using error middleware starting in this version anyways. |
||
} | ||
|
||
if (! $hasError && $arity < 4) { | ||
return $middleware($request, $response, $next); | ||
} | ||
|
||
return $next($request, $response, $err); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests are missing for this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests have now been added for both this path, and the one noted above. |
||
} | ||
|
||
try { | ||
if ($hasError && $arity === 4) { | ||
return $middleware($err, $request, $response, $next); | ||
|
@@ -231,6 +262,10 @@ private function dispatchInteropMiddleware($middleware, callable $next, RequestI | |
$middleware->setResponsePrototype($this->responsePrototype); | ||
} | ||
|
||
if ($this->raiseThrowables) { | ||
return $middleware->process($request, $next); | ||
} | ||
|
||
try { | ||
return $middleware->process($request, $next); | ||
} catch (Throwable $throwable) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/** | ||
* @link http://github.com/zendframework/zend-stratigility for the canonical source repository | ||
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Stratigility\Exception; | ||
|
||
use RuntimeException; | ||
|
||
/** | ||
* Exception raised when a string $err is provided and raise throwables is enabled. | ||
* | ||
* @todo Remove for 2.0.0. | ||
*/ | ||
class MiddlewareException extends RuntimeException | ||
{ | ||
/** | ||
* Create an instance based on an error value. | ||
* | ||
* @param mixed $err | ||
* @return self | ||
*/ | ||
public static function fromErrorValue($err) | ||
{ | ||
if (is_object($err)) { | ||
return self::fromType(get_class($err)); | ||
} | ||
|
||
if (is_array($err)) { | ||
return self::fromType(gettype($err)); | ||
} | ||
|
||
if (is_string($err)) { | ||
throw new self($err); | ||
} | ||
|
||
return self::fromType(var_export($err, true)); | ||
} | ||
|
||
/** | ||
* Create an instance using a templated error string. | ||
* | ||
* @param string $value | ||
* @return self | ||
*/ | ||
private static function fromType($value) | ||
{ | ||
return new self(sprintf( | ||
'Middleware raised an error condition: %s', | ||
$value | ||
)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import
Interop\Http\Middleware\DelegateInterface
is not used. However it is mentioned as a comment in the docblock right before the process method, but I don't think that's a reason to import it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, removed now.