Skip to content

Commit

Permalink
Show a more friendly error message to users, and use the website's lo…
Browse files Browse the repository at this point in the history
…go instead of kikcms logo
  • Loading branch information
krazzer committed Jan 28, 2021
1 parent 30274b5 commit a67126f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
18 changes: 2 additions & 16 deletions src/Controllers/FrontendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function databaseConnectionFailureAction(): ResponseInterface
$title = $this->translator->tl('error.database.title');
$description = $this->translator->tl('error.database.description');

return $this->message($title, $description);
return $this->frontendService->getMessageResponse($title, $description);
}

/**
Expand Down Expand Up @@ -83,7 +83,7 @@ public function pageAction(string $urlPath = null)
$title = $this->translator->tl('maintenance.title');
$description = $this->translator->tl('maintenance.description');

return $this->message($title, $description);
return $this->frontendService->getMessageResponse($title, $description);
}

if ( ! $pageLanguage = $this->frontendService->getPageLanguageToLoadByUrlPath($urlPath)) {
Expand Down Expand Up @@ -198,18 +198,4 @@ private function loadPage(PageLanguage $pageLanguage): ?Response

return null;
}

/**
* @param string $title
* @param string $description
* @return ResponseInterface
*/
private function message(string $title, string $description): ResponseInterface
{
return $this->response->setContent($this->view->getPartial('@kikcms/frontend/message', [
'title' => $title,
'description' => $description,
'customCss' => $this->websiteSettings->getCustomCss(),
]));
}
}
12 changes: 10 additions & 2 deletions src/Services/ErrorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,21 @@ public function getErrorView($error, bool $isProduction): ?string
*/
public function getResponse(string $errorType, array $parameters = []): ResponseInterface
{
$title = $this->translator->tl('error.' . $errorType . '.title');
$description = $this->translator->tl('error.' . $errorType . '.description', $parameters);

if ($this->request->isAjax() && $this->config->isProd()) {
return $this->response->setJsonContent([
'title' => $this->translator->tl('error.' . $errorType . '.title'),
'description' => $this->translator->tl('error.' . $errorType . '.description', $parameters),
]);
} else {
return $this->response->setContent($this->view->getPartial('@kikcms/errors/show' . $errorType, $parameters));
if($this->config->isProd()){
return $this->frontendService->getMessageResponse($title, $description);
} else {
$content = $this->view->getPartial('@kikcms/errors/show' . $errorType, $parameters);
return $this->response->setContent($content);
}
}
}

Expand All @@ -61,7 +69,7 @@ public function handleError($error)
{
$isProduction = $this->config->isProd();

if( ! $errorView = $this->getErrorView($error, $isProduction)){
if ( ! $errorView = $this->getErrorView($error, $isProduction)) {
return;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Services/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ protected function initErrorHandler(): ErrorHandler
$errorHandler = new ErrorHandler($this->get('logger'));

set_exception_handler(function ($error) {
$this->get('errorService')->handleError($error);
/** @var ErrorService $errorService */
$errorService = $this->get('errorService');
$errorService->handleError($error);
});

// handle warnings and notices as exceptions on development
Expand Down
18 changes: 18 additions & 0 deletions src/Services/Website/FrontendService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use KikCMS\Services\Pages\UrlService;
use KikCMS\Services\UserService;
use KikCMS\Classes\Phalcon\Injectable;
use Phalcon\Http\ResponseInterface;

/**
* @property PageLanguageService $pageLanguageService
Expand All @@ -20,6 +21,23 @@
*/
class FrontendService extends Injectable
{
/**
* Return a response with a message. This can be used for error or maintainance messages, where the website is
* not functional
*
* @param string $title
* @param string $description
* @return ResponseInterface
*/
public function getMessageResponse(string $title, string $description): ResponseInterface
{
return $this->response->setContent($this->view->getPartial('@kikcms/frontend/message', [
'title' => $title,
'description' => $description,
'customCss' => $this->websiteSettings->getCustomCss(),
]));
}

/**
* @param string|null $urlPath
* @return PageLanguage|null
Expand Down

0 comments on commit a67126f

Please sign in to comment.