more error handling

This commit is contained in:
Shish 2021-11-10 19:33:51 +00:00
parent 4c4b26f098
commit 58db685b29
7 changed files with 27 additions and 16 deletions

View File

@ -1,8 +1,6 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
/** /**
* Class SCoreException
*
* A base exception to be caught by the upper levels. * A base exception to be caught by the upper levels.
*/ */
class SCoreException extends RuntimeException class SCoreException extends RuntimeException
@ -34,8 +32,6 @@ class InstallerException extends RuntimeException
} }
/** /**
* Class PermissionDeniedException
*
* A fairly common, generic exception. * A fairly common, generic exception.
*/ */
class PermissionDeniedException extends SCoreException class PermissionDeniedException extends SCoreException
@ -43,16 +39,19 @@ class PermissionDeniedException extends SCoreException
} }
/** /**
* Class ImageDoesNotExist
*
* This exception is used when an Image cannot be found by ID. * This exception is used when an Image cannot be found by ID.
*
* Example: Image::by_id(-1) returns null
*/ */
class ImageDoesNotExist extends SCoreException class ImageDoesNotExist extends SCoreException
{ {
} }
/**
* This exception is used when a User cannot be found by some criteria.
*/
class UserDoesNotExist extends SCoreException
{
}
/* /*
* For validate_input() * For validate_input()
*/ */

View File

@ -119,7 +119,7 @@ class Image
if (SPEED_HAX) { if (SPEED_HAX) {
if (!$user->can(Permissions::BIG_SEARCH) and count($tags) > 3) { if (!$user->can(Permissions::BIG_SEARCH) and count($tags) > 3) {
throw new SCoreException("Anonymous users may only search for up to 3 tags at a time"); throw new PermissionDeniedException("Anonymous users may only search for up to 3 tags at a time");
} }
} }

View File

@ -97,7 +97,7 @@ class User
{ {
$u = User::by_name($name); $u = User::by_name($name);
if (is_null($u)) { if (is_null($u)) {
throw new ScoreException("Can't find any user named $name"); throw new UserDoesNotExist("Can't find any user named $name");
} else { } else {
return $u->id; return $u->id;
} }

View File

@ -616,7 +616,7 @@ function _fatal_error(Exception $e): void
print("Version: $version (on $phpver)\n"); print("Version: $version (on $phpver)\n");
} else { } else {
$q = $query ? "" : "<p><b>Query:</b> " . html_escape($query); $q = $query ? "" : "<p><b>Query:</b> " . html_escape($query);
error_log("Shimmie Error: $message // $query // {$e->getTraceAsString()}"); error_log("Shimmie Error: $message (Query: $query)\n{$e->getTraceAsString()}");
header("HTTP/1.0 500 Internal Error"); header("HTTP/1.0 500 Internal Error");
echo ' echo '
<!doctype html> <!doctype html>

View File

@ -91,8 +91,12 @@ class Index extends Extension
if (!$images) { if (!$images) {
$images = Image::find_images(($page_number-1)*$page_size, $page_size, $search_terms); $images = Image::find_images(($page_number-1)*$page_size, $page_size, $search_terms);
} }
} catch (PermissionDeniedException $pde) {
$this->theme->display_error(403, "Permission denied", $pde->error);
$total_pages = 0;
$images = [];
} catch (SearchTermParseException $stpe) { } catch (SearchTermParseException $stpe) {
// FIXME: display the error somewhere $this->theme->display_error(400, "Malformed search query", $stpe->error);
$total_pages = 0; $total_pages = 0;
$images = []; $images = [];
} }

View File

@ -27,8 +27,12 @@ class RSSImages extends Extension
if (SPEED_HAX && $page_number > 9) { if (SPEED_HAX && $page_number > 9) {
return; return;
} }
$images = Image::find_images(($page_number-1)*$page_size, $page_size, $search_terms); try {
$this->do_rss($images, $search_terms, $page_number); $images = Image::find_images(($page_number-1)*$page_size, $page_size, $search_terms);
$this->do_rss($images, $search_terms, $page_number);
} catch (PermissionDeniedException $pde) {
$this->theme->display_error(403, "Permission denied", $pde->error);
}
} }
} }

View File

@ -385,8 +385,12 @@ class UserPage extends Extension
$matches = []; $matches = [];
if (preg_match(self::USER_SEARCH_REGEX, $event->term, $matches)) { if (preg_match(self::USER_SEARCH_REGEX, $event->term, $matches)) {
$user_id = User::name_to_id($matches[2]); try {
$event->add_querylet(new Querylet("images.owner_id ${matches[1]}= $user_id")); $user_id = User::name_to_id($matches[2]);
$event->add_querylet(new Querylet("images.owner_id ${matches[1]}= $user_id"));
} catch (UserDoesNotExist $e) {
$event->add_querylet(new Querylet("1=0"));
}
} elseif (preg_match(self::USER_ID_SEARCH_REGEX, $event->term, $matches)) { } elseif (preg_match(self::USER_ID_SEARCH_REGEX, $event->term, $matches)) {
$user_id = int_escape($matches[2]); $user_id = int_escape($matches[2]);
$event->add_querylet(new Querylet("images.owner_id ${matches[1]}= $user_id")); $event->add_querylet(new Querylet("images.owner_id ${matches[1]}= $user_id"));