diff --git a/LICENSE.md b/LICENSE.md index 15d883c..261fae9 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,7 @@ Portions of the "BitFrame Whoops Middleware" incorporates the work by (as provided under the MIT lincense): * Copyright (c) 2015-2017 Franz Liedke +* Copyright (c) 2013-2018 Filipe Dobreira (http://github.com/filp) All other copyright for the "BitFrame Whoops Middleware" are held by: diff --git a/composer.json b/composer.json index ddf7b29..6519170 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "designcise/bitframe-whoops", - "version": "1.0.0", + "version": "1.0.1", "type": "library", "description": "Whoops error handler middleware for BitFrame microframework", "license": "MIT", diff --git a/src/FormatNegotiator.php b/src/FormatNegotiator.php index 3f0c807..3de4042 100644 --- a/src/FormatNegotiator.php +++ b/src/FormatNegotiator.php @@ -9,7 +9,7 @@ * @author Franz Liedke * @copyright Copyright (c) 2015-2017 Franz Liedke * - * @license https://github.com/designcise/bitframe/blob/master/LICENSE.md MIT License + * @license https://github.com/designcise/bitframe-whoops/blob/master/LICENSE.md MIT License */ namespace BitFrame\ErrorHandler; diff --git a/src/Handler/JsonpResponseHandler.php b/src/Handler/JsonpResponseHandler.php new file mode 100644 index 0000000..dcb567c --- /dev/null +++ b/src/Handler/JsonpResponseHandler.php @@ -0,0 +1,114 @@ +callback = $callback; + } + + /** + * Returns errors[[]] instead of error[] to be in + * compliance with the json:api spec + * + * @param bool $jsonApi Default is false + * + * @return $this + */ + public function setJsonApi(bool $jsonApi = false): self + { + $this->jsonApi = (bool) $jsonApi; + return $this; + } + + /** + * @param bool|null $returnFrames + * + * @return bool|$this + */ + public function addTraceToOutput(?bool $returnFrames = null) + { + if (func_num_args() == 0) { + return $this->returnFrames; + } + + $this->returnFrames = (bool) $returnFrames; + return $this; + } + + /** + * Handle errors. + * + * @return int + */ + public function handle(): int + { + if ($this->jsonApi === true) { + $response = [ + 'errors' => [ + Formatter::formatExceptionAsDataArray( + $this->getInspector(), + $this->addTraceToOutput() + ), + ] + ]; + } else { + $response = [ + 'error' => Formatter::formatExceptionAsDataArray( + $this->getInspector(), + $this->addTraceToOutput() + ), + ]; + } + + $json = json_encode($response, defined('JSON_PARTIAL_OUTPUT_ON_ERROR') ? JSON_PARTIAL_OUTPUT_ON_ERROR : 0); + echo "{$this->callback}($json)"; + + return Handler::QUIT; + } + + /** + * Get content type. + * + * @return string + */ + public function contentType(): string + { + return 'application/json'; + } +} diff --git a/src/WhoopsErrorHandler.php b/src/WhoopsErrorHandler.php index ed1286c..b34a231 100644 --- a/src/WhoopsErrorHandler.php +++ b/src/WhoopsErrorHandler.php @@ -9,7 +9,7 @@ * @author Franz Liedke * @copyright Copyright (c) 2015-2017 Franz Liedke * - * @license https://github.com/designcise/bitframe/blob/master/LICENSE.md MIT License + * @license https://github.com/designcise/bitframe-whoops/blob/master/LICENSE.md MIT License */ namespace BitFrame\ErrorHandler; @@ -18,9 +18,11 @@ use \Psr\Http\Server\{RequestHandlerInterface, MiddlewareInterface}; use \Whoops\Run; +use \Whoops\Util\Misc; use \Whoops\Handler\{PlainTextHandler, JsonResponseHandler, XmlResponseHandler, PrettyPageHandler}; use BitFrame\Delegate\CallableMiddlewareTrait; +use BitFrame\ErrorHandler\Handler\JsonpResponseHandler; /** * Whoops error handler middleware to handle application @@ -139,13 +141,23 @@ public static function getWhoopsInstance( // select appropriate handler as per the requested format $format = ((PHP_SAPI === 'cli')) ? 'text' : ( ($format === 'auto') ? ( - (\Whoops\Util\Misc::isAjaxRequest()) ? 'json' : FormatNegotiator::getPreferredFormat($request) + (Misc::isAjaxRequest()) ? 'json' : FormatNegotiator::getPreferredFormat($request) ) : $format ); switch ($format) { case 'json': $handler = new JsonResponseHandler; + + // is a jsonp request? + if ( + $request->getMethod() === 'GET' && + ! empty($callback = $request->getQueryParam('callback', '')) + ) { + // use the custom jsonp response handler + $handler = new JsonpResponseHandler($callback); + } + $handler->addTraceToOutput($showTrace); $handler->setJsonApi(true); break; diff --git a/test/FormatNegotiatorTest.php b/test/FormatNegotiatorTest.php index 5efce29..c980c83 100644 --- a/test/FormatNegotiatorTest.php +++ b/test/FormatNegotiatorTest.php @@ -9,7 +9,7 @@ * @author Franz Liedke * @copyright Copyright (c) 2015-2017 Franz Liedke * - * @license https://github.com/designcise/bitframe/blob/master/LICENSE.md MIT License + * @license https://github.com/designcise/bitframe-whoops/blob/master/LICENSE.md MIT License */ namespace BitFrame\Test; diff --git a/test/WhoopsErrorHandlerTest.php b/test/WhoopsErrorHandlerTest.php index c5b53b2..895c04f 100644 --- a/test/WhoopsErrorHandlerTest.php +++ b/test/WhoopsErrorHandlerTest.php @@ -6,7 +6,7 @@ * @author Daniyal Hamid * @copyright Copyright (c) 2017-2018 Daniyal Hamid (https://designcise.com) * - * @license https://github.com/designcise/bitframe/blob/master/LICENSE.md MIT License + * @license https://github.com/designcise/bitframe-whoops/blob/master/LICENSE.md MIT License */ namespace BitFrame\Test;