diff --git a/formwork/src/Controllers/ErrorsController.php b/formwork/src/Controllers/ErrorsController.php index fbdcc20f2..97cdeac67 100644 --- a/formwork/src/Controllers/ErrorsController.php +++ b/formwork/src/Controllers/ErrorsController.php @@ -11,31 +11,32 @@ final class ErrorsController extends AbstractController implements ErrorsControl { /** * ErrorsController@error action + * + * @param array $data */ - public function error(ResponseStatus $responseStatus = ResponseStatus::InternalServerError, ?Throwable $throwable = null): Response + public function error(ResponseStatus $responseStatus = ResponseStatus::InternalServerError, ?Throwable $throwable = null, array $data = []): Response { Response::cleanOutputBuffers(); - $response = $this->request->isXmlHttpRequest() - ? JsonResponse::error('Error', $responseStatus) - : new Response($this->view( + if ($this->config->get('system.debug.enabled') || $this->request->isLocalhost()) { + $data['throwable'] = $throwable; + } + + if ($this->request->isXmlHttpRequest()) { + $response = JsonResponse::error('Error', $responseStatus); + } else { + $response = new Response($this->view( 'errors.error', [ - 'status' => $responseStatus->code(), - 'message' => $responseStatus->message(), - 'throwable' => $throwable, + 'status' => $responseStatus->code(), + 'message' => $responseStatus->message(), + ...$data, ] ), $responseStatus); + } if ($throwable !== null) { - error_log(sprintf( - "Uncaught %s: %s in %s:%s\nStack trace:\n%s\n", - $throwable::class, - $throwable->getMessage(), - $throwable->getFile(), - $throwable->getLine(), - $throwable->getTraceAsString() - )); + $this->logThrowable($throwable); } return $response; @@ -64,4 +65,19 @@ public function forbidden(): Response { return $this->error(ResponseStatus::Forbidden); } + + /** + * Log a throwable to the error log + */ + private function logThrowable(Throwable $throwable): void + { + error_log(sprintf( + "Uncaught %s: %s in %s:%s\nStack trace:\n%s\n", + $throwable::class, + $throwable->getMessage(), + $throwable->getFile(), + $throwable->getLine(), + $throwable->getTraceAsString() + )); + } } diff --git a/formwork/src/Panel/Controllers/ErrorsController.php b/formwork/src/Panel/Controllers/ErrorsController.php index 8a5eab853..0e21bf661 100644 --- a/formwork/src/Panel/Controllers/ErrorsController.php +++ b/formwork/src/Panel/Controllers/ErrorsController.php @@ -18,7 +18,7 @@ public function error(ResponseStatus $responseStatus = ResponseStatus::InternalS return $this->makeErrorResponse($responseStatus, 'internalServerError', [ 'href' => $this->makeGitHubIssueUri($throwable), 'label' => $this->translate('panel.errors.action.reportToGithub'), - ], ['throwable' => $throwable]); + ], $throwable); } /** @@ -40,7 +40,7 @@ public function internalServerError(Throwable $throwable): Response return $this->makeErrorResponse(ResponseStatus::InternalServerError, 'internalServerError', [ 'href' => $this->makeGitHubIssueUri($throwable), 'label' => $this->translate('panel.errors.action.reportToGithub'), - ], ['throwable' => $throwable]); + ], $throwable); } /** @@ -60,10 +60,14 @@ public function forbidden(): Response * @param array $action * @param array $data */ - private function makeErrorResponse(ResponseStatus $responseStatus, string $name, array $action, array $data = []): Response + private function makeErrorResponse(ResponseStatus $responseStatus, string $name, array $action, ?Throwable $throwable = null, array $data = []): Response { Response::cleanOutputBuffers(); + if ($this->config->get('system.debug.enabled') || $this->request->isLocalhost()) { + $data['throwable'] = $throwable; + } + if ($this->request->isXmlHttpRequest()) { $response = JsonResponse::error('Error', $responseStatus); } else { @@ -78,8 +82,8 @@ private function makeErrorResponse(ResponseStatus $responseStatus, string $name, ]), $responseStatus); } - if ($data['throwable'] !== null) { - $this->logThrowable($data['throwable']); + if ($throwable !== null) { + $this->logThrowable($throwable); } return $response; diff --git a/formwork/views/errors/error.php b/formwork/views/errors/error.php index 9f0464e84..49fe788b6 100644 --- a/formwork/views/errors/error.php +++ b/formwork/views/errors/error.php @@ -2,7 +2,7 @@

Oops, something went wrong!

Formwork encountered an error while serving your request.
If you are the maintainer of this site, please check Formwork configuration or the server log for errors.

Report an issue to GitHub

-config()->get('system.debug.enabled', false) || $app->request()->isLocalhost())) : ?> + insert('errors.partials.debug') ?> -insert('errors.partials.footer') ?> \ No newline at end of file +insert('errors.partials.footer') ?> diff --git a/panel/views/errors/error.php b/panel/views/errors/error.php index b97608a83..97f037d8f 100644 --- a/panel/views/errors/error.php +++ b/panel/views/errors/error.php @@ -24,7 +24,7 @@ - config()->get('system.debug.enabled') || $app->request()->isLocalhost())) : ?> +

Uncaught : getMessage() ?>

@@ -46,4 +46,4 @@ - \ No newline at end of file +