Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions app/Http/Middleware/HandleExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

namespace Fisharebest\Webtrees\Http\Middleware;

use Error;
use Fig\Http\Message\RequestMethodInterface;
use Fig\Http\Message\StatusCodeInterface;
use Fisharebest\Webtrees\Http\Exceptions\HttpException;
Expand Down Expand Up @@ -134,10 +135,14 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

private function httpExceptionResponse(ServerRequestInterface $request, HttpException $exception): ResponseInterface
{
$tree = Validator::attributes($request)->treeOptional();
$default = Site::getPreference('DEFAULT_GEDCOM');
$tree ??= $this->tree_service->all()[$default] ?? $this->tree_service->all()->first();

$tree = Validator::attributes($request)->treeOptional();
try {
$default = Site::getPreference('DEFAULT_GEDCOM');
$tree ??= $this->tree_service->all()[$default] ?? $this->tree_service->all()->first();
} catch (Error) {
// httpException can be thrown before database connection is made: return a simple stack trace dump
return response(nl2br((string) $exception), $exception->getCode());
}
$status_code = $exception->getCode();

// If this was a GET request, then we were probably fetching HTML to display, for
Expand Down
2 changes: 1 addition & 1 deletion app/Http/RequestHandlers/AdminMediaFileDownload.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ public function handle(ServerRequestInterface $request): ResponseInterface
}
}

throw new HttpBadRequestException(I18N::translate('The parameter “path” is invalid.'));
throw new HttpBadRequestException('The parameter “path” is invalid.');
}
}
2 changes: 1 addition & 1 deletion app/Http/RequestHandlers/AdminMediaFileThumbnail.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ public function handle(ServerRequestInterface $request): ResponseInterface
}
}

throw new HttpBadRequestException(I18N::translate('The parameter “path” is invalid.'));
throw new HttpBadRequestException('The parameter “path” is invalid.');
}
}
40 changes: 24 additions & 16 deletions app/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public function boolean(string $parameter, bool|null $default = null): bool
}

if ($default === null) {
throw new HttpBadRequestException(sprintf('The parameter “%s” is missing.', $parameter));
throw $this->parameterMissingException($parameter);
}

return $default;
Expand All @@ -226,7 +226,7 @@ public function list(string $parameter): array
$values = $this->array($parameter);

if (!array_is_list($values)) {
throw new HttpBadRequestException(sprintf('The parameter “%s” is not a list.', $parameter));
throw $this->badParameterException($parameter, 'not a list');
}

return $values;
Expand All @@ -241,7 +241,7 @@ public function array(string $parameter): array

foreach ($values as $value) {
if (!is_string($value)) {
throw new HttpBadRequestException(sprintf('The parameter “%s” is missing.', $parameter));
throw $this->parameterMissingException($parameter);
}
}

Expand All @@ -257,15 +257,12 @@ public function arrayArray(string $parameter): array

foreach ($values as $array_value) {
if (!is_array($array_value)) {
$message = sprintf('The parameter “%s” is not an array of arrays.', $parameter);
throw new HttpBadRequestException($message);
throw $this->badParameterException($parameter, 'not an array of arrays');
}

foreach ($array_value as $value) {
if (!is_string($value)) {
$message = sprintf('The parameter “%s” is not an array of array of strings.', $parameter);

throw new HttpBadRequestException($message);
throw $this->badParameterException($parameter, 'not an array of array of strings');
}
}
}
Expand All @@ -281,7 +278,7 @@ private function arrayData(string $parameter): array
$value = $this->parameters[$parameter] ?? [];

if (!is_array($value)) {
throw new HttpBadRequestException(sprintf('The parameter “%s” is not an array.', $parameter));
throw $this->badParameterException($parameter, 'not an array');
}

$callback = static fn (array|null $value, Closure $rule): array|null => $rule($value);
Expand All @@ -304,7 +301,7 @@ public function float(string $parameter, float|null $default = null): float
$value = array_reduce($this->rules, $callback, $value) ?? $default;

if ($value === null) {
throw new HttpBadRequestException(sprintf('The parameter “%s” is missing.', $parameter));
throw $this->parameterMissingException($parameter);
}

return $value;
Expand All @@ -331,7 +328,7 @@ public function integer(string $parameter, int|null $default = null): int
$value = array_reduce($this->rules, $callback, $value) ?? $default;

if ($value === null) {
throw new HttpBadRequestException(sprintf('The parameter “%s” is missing.', $parameter));
throw $this->parameterMissingException($parameter);
}

return $value;
Expand All @@ -345,7 +342,7 @@ public function route(string $parameter = 'route'): Route
return $value;
}

throw new HttpBadRequestException(sprintf('The parameter “%s” is missing.', $parameter));
throw $this->parameterMissingException($parameter);
}

public function string(string $parameter, string|null $default = null): string
Expand All @@ -361,7 +358,7 @@ public function string(string $parameter, string|null $default = null): string
$value = array_reduce($this->rules, $callback, $value) ?? $default;

if ($value === null) {
throw new HttpBadRequestException(sprintf('The parameter “%s” is missing.', $parameter));
throw $this->parameterMissingException($parameter);
}

return $value;
Expand All @@ -375,7 +372,7 @@ public function tree(string $parameter = 'tree'): Tree
return $value;
}

throw new HttpBadRequestException(sprintf('The parameter “%s” is missing.', $parameter));
throw $this->parameterMissingException($parameter);
}

public function treeOptional(string $parameter = 'tree'): Tree|null
Expand All @@ -386,7 +383,7 @@ public function treeOptional(string $parameter = 'tree'): Tree|null
return $value;
}

throw new HttpBadRequestException(sprintf('The parameter “%s” is missing.', $parameter));
throw $this->parameterMissingException($parameter);
}

public function user(string $parameter = 'user'): UserInterface
Expand All @@ -397,6 +394,17 @@ public function user(string $parameter = 'user'): UserInterface
return $value;
}

throw new HttpBadRequestException(sprintf('The parameter “%s” is missing.', $parameter));
throw $this->parameterMissingException($parameter);
}

private function parameterMissingException(string $parameter): HttpBadRequestException
{
return $this->badParameterException($parameter, 'missing');
}

private function badParameterException(string $parameter, string $complaint): HttpBadRequestException
{
return new HttpBadRequestException(sprintf('The parameter “%s” is %s.', $parameter, $complaint));
}

}
Loading