Skip to content

Commit

Permalink
Fix: #3029 - trap SQL errors caused by server issues
Browse files Browse the repository at this point in the history
  • Loading branch information
fisharebest committed Mar 1, 2020
1 parent 50a05b2 commit b8d4625
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
12 changes: 10 additions & 2 deletions app/Http/Middleware/HandleExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,23 @@ private function httpExceptionResponse(ServerRequestInterface $request, HttpExce
$default = Site::getPreference('DEFAULT_GEDCOM');
$tree = $tree ?? $this->tree_service->all()[$default] ?? $this->tree_service->all()->first();

if ($request->getHeaderLine('X-Requested-With') !== '') {
$status_code = $exception->getCode();

// If this was a GET request, then we were probably fetching HTML to display, for
// example a chart or tab.
if (
$request->getHeaderLine('X-Requested-With') !== '' &&
$request->getMethod() === RequestMethodInterface::METHOD_GET
) {
$this->layout = 'layouts/ajax';
$status_code = StatusCodeInterface::STATUS_OK;
}

return $this->viewResponse('components/alert-danger', [
'alert' => $exception->getMessage(),
'title' => $exception->getMessage(),
'tree' => $tree,
], $exception->getCode());
], $status_code);
}

/**
Expand Down
23 changes: 22 additions & 1 deletion app/Http/Middleware/UseDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

namespace Fisharebest\Webtrees\Http\Middleware;

use Fisharebest\Webtrees\Exceptions\HttpServerErrorException;
use Fisharebest\Webtrees\Webtrees;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Query\Builder;
use LogicException;
use PDO;
use PDOException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
Expand All @@ -34,6 +36,14 @@
*/
class UseDatabase implements MiddlewareInterface
{
// The following errors are likely to be caused by server issues, not by webtrees.
private const SERVER_ERRORS = [
'mysql' => [1203],
'pgsql' => [],
'sqlite' => [],
'sqlsvr' => [],
];

/**
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
Expand Down Expand Up @@ -92,6 +102,17 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
return $this->where($column, 'LIKE', '%' . $search . '%', $boolean);
});

return $handler->handle($request);
try {
return $handler->handle($request);
} catch (PDOException $exception) {
if (in_array($exception->errorInfo[1], self::SERVER_ERRORS[$driver], true)) {
$message = 'A database error occurred. This is most likely caused by an issue with your server.' . PHP_EOL . PHP_EOL;
$message .= $exception->getMessage() . PHP_EOL . PHP_EOL;
$message .= $exception->getFile() . ':' . $exception->getLine();
throw new HttpServerErrorException($message);
}

throw $exception;
}
}
}

0 comments on commit b8d4625

Please sign in to comment.