| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package WPEmerge |
| 4 |
* @author Atanas Angelov <hi@atanas.dev> |
| 5 |
* @copyright 2017-2019 Atanas Angelov |
| 6 |
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
| 7 |
* @link https://wpemerge.com/ |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace WPEmerge\Exceptions; |
| 11 |
|
| 12 |
use Exception as PhpException; |
| 13 |
use Psr\Http\Message\ResponseInterface; |
| 14 |
use Whoops\RunInterface; |
| 15 |
use WPEmerge\Csrf\InvalidCsrfTokenException; |
| 16 |
use WPEmerge\Requests\RequestInterface; |
| 17 |
use WPEmerge\Responses\ResponseService; |
| 18 |
use WPEmerge\Routing\NotFoundException; |
| 19 |
use WPEmerge\Support\Arr; |
| 20 |
|
| 21 |
class ErrorHandler implements ErrorHandlerInterface { |
| 22 |
/** |
| 23 |
* Response service. |
| 24 |
* |
| 25 |
* @var ResponseService |
| 26 |
*/ |
| 27 |
protected $response_service = null; |
| 28 |
|
| 29 |
/** |
| 30 |
* Pretty handler. |
| 31 |
* |
| 32 |
* @var RunInterface|null |
| 33 |
*/ |
| 34 |
protected $whoops = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* Whether debug mode is enabled. |
| 38 |
* |
| 39 |
* @var boolean |
| 40 |
*/ |
| 41 |
protected $debug = false; |
| 42 |
|
| 43 |
/** |
| 44 |
* Constructor. |
| 45 |
* |
| 46 |
* @codeCoverageIgnore |
| 47 |
* @param ResponseService $response_service |
| 48 |
* @param RunInterface|null $whoops |
| 49 |
* @param boolean $debug |
| 50 |
*/ |
| 51 |
public function __construct( $response_service, $whoops, $debug = false ) { |
| 52 |
$this->response_service = $response_service; |
| 53 |
$this->whoops = $whoops; |
| 54 |
$this->debug = $debug; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* {@inheritDoc} |
| 59 |
* @codeCoverageIgnore |
| 60 |
*/ |
| 61 |
public function register() { |
| 62 |
if ( $this->debug && $this->whoops !== null ) { |
| 63 |
$this->whoops->register(); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* {@inheritDoc} |
| 69 |
* @codeCoverageIgnore |
| 70 |
*/ |
| 71 |
public function unregister() { |
| 72 |
if ( $this->debug && $this->whoops !== null ) { |
| 73 |
$this->whoops->unregister(); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Convert an exception to a ResponseInterface instance if possible. |
| 79 |
* |
| 80 |
* @param PhpException $exception |
| 81 |
* @return ResponseInterface|false |
| 82 |
*/ |
| 83 |
protected function toResponse( $exception ) { |
| 84 |
// @codeCoverageIgnoreStart |
| 85 |
if ( $exception instanceof InvalidCsrfTokenException ) { |
| 86 |
wp_nonce_ays( '' ); |
| 87 |
} |
| 88 |
// @codeCoverageIgnoreEnd |
| 89 |
|
| 90 |
if ( $exception instanceof NotFoundException ) { |
| 91 |
return $this->response_service->error( 404 ); |
| 92 |
} |
| 93 |
|
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Convert an exception to a debug ResponseInterface instance if possible. |
| 99 |
* |
| 100 |
* @throws PhpException |
| 101 |
* @param RequestInterface $request |
| 102 |
* @param PhpException $exception |
| 103 |
* @return ResponseInterface |
| 104 |
*/ |
| 105 |
protected function toDebugResponse( RequestInterface $request, PhpException $exception ) { |
| 106 |
if ( $request->isAjax() ) { |
| 107 |
return $this->response_service->json( [ |
| 108 |
'message' => $exception->getMessage(), |
| 109 |
'exception' => get_class( $exception ), |
| 110 |
'file' => $exception->getFile(), |
| 111 |
'line' => $exception->getLine(), |
| 112 |
'trace' => array_map( function ( $trace ) { |
| 113 |
return Arr::except( $trace, ['args'] ); |
| 114 |
}, $exception->getTrace() ), |
| 115 |
] )->withStatus( 500 ); |
| 116 |
} |
| 117 |
|
| 118 |
if ( $this->whoops !== null ) { |
| 119 |
return $this->toPrettyErrorResponse( $exception ); |
| 120 |
} |
| 121 |
|
| 122 |
throw $exception; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Convert an exception to a pretty error response. |
| 127 |
* |
| 128 |
* @codeCoverageIgnore |
| 129 |
* @param PhpException $exception |
| 130 |
* @return ResponseInterface |
| 131 |
*/ |
| 132 |
protected function toPrettyErrorResponse( $exception ) { |
| 133 |
$method = RunInterface::EXCEPTION_HANDLER; |
| 134 |
ob_start(); |
| 135 |
$this->whoops->$method( $exception ); |
| 136 |
$response = ob_get_clean(); |
| 137 |
return $this->response_service->output( $response )->withStatus( 500 ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* {@inheritDoc} |
| 142 |
* @throws PhpException |
| 143 |
*/ |
| 144 |
public function getResponse( RequestInterface $request, PhpException $exception ) { |
| 145 |
$response = $this->toResponse( $exception ); |
| 146 |
|
| 147 |
if ( $response !== false ) { |
| 148 |
return $response; |
| 149 |
} |
| 150 |
|
| 151 |
// @codeCoverageIgnoreStart |
| 152 |
if ( ! defined( 'WPEMERGE_TEST_DIR' ) ) { |
| 153 |
// Only log errors if we are not running the WP Emerge test suite. |
| 154 |
error_log( $exception ); |
| 155 |
} |
| 156 |
// @codeCoverageIgnoreEnd |
| 157 |
|
| 158 |
if ( ! $this->debug ) { |
| 159 |
return $this->response_service->error( 500 ); |
| 160 |
} |
| 161 |
|
| 162 |
return $this->toDebugResponse( $request, $exception ); |
| 163 |
} |
| 164 |
} |
| 165 |
|