From 8a8930c8fde7847d160d9d7cb7ccc3db106df2b4 Mon Sep 17 00:00:00 2001 From: Daniel Lowrey Date: Thu, 11 Jun 2015 15:22:14 -0400 Subject: [PATCH] Fix exception breakage across 5.x/7 environments --- lib/Failure.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/Failure.php b/lib/Failure.php index 6143413..60ef95f 100644 --- a/lib/Failure.php +++ b/lib/Failure.php @@ -8,7 +8,18 @@ namespace Amp; class Failure implements Promise { private $error; - public function __construct(\Exception $error) { + /** + * The error parameter used to fail a promisor must always be an exception + * instance. However, we cannot typehint this parameter in environments + * where PHP5.x compatibility is required because PHP7 BaseException + * instances will break the typehint. + */ + public function __construct($error) { + if (!($error instanceof \Exception || $error instanceof \BaseException)) { + throw new \InvalidArgumentException( + "Only exceptions may be used to fail a promise" + ); + } $this->error = $error; }