From 68a128c0ea8d9c850d4c5b0e0c798104dbad3830 Mon Sep 17 00:00:00 2001 From: Shish Date: Tue, 16 Nov 2021 14:52:26 +0000 Subject: [PATCH] don't log user errors into the server error log --- core/exceptions.php | 27 +++++++++++++++++++++------ core/util.php | 7 +++++-- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/core/exceptions.php b/core/exceptions.php index 5ee3d165..9a325405 100644 --- a/core/exceptions.php +++ b/core/exceptions.php @@ -7,6 +7,7 @@ class SCoreException extends RuntimeException { public ?string $query; public string $error; + public int $http_code = 500; public function __construct(string $msg, ?string $query=null) { @@ -31,44 +32,58 @@ class InstallerException extends RuntimeException } } +class UserErrorException extends SCoreException +{ + public int $http_code = 400; +} + +class ServerErrorException extends SCoreException +{ + public int $http_code = 500; +} + /** * A fairly common, generic exception. */ -class PermissionDeniedException extends SCoreException +class PermissionDeniedException extends UserErrorException { + public int $http_code = 403; } /** * This exception is used when an Image cannot be found by ID. */ -class ImageDoesNotExist extends SCoreException +class ImageDoesNotExist extends UserErrorException { + public int $http_code = 404; } /** * This exception is used when a User cannot be found by some criteria. */ -class UserDoesNotExist extends SCoreException +class UserDoesNotExist extends UserErrorException { + public int $http_code = 404; } /* * For validate_input() */ -class InvalidInput extends SCoreException +class InvalidInput extends UserErrorException { + public int $http_code = 402; } /* * This is used by the image resizing code when there is not enough memory to perform a resize. */ -class InsufficientMemoryException extends SCoreException +class InsufficientMemoryException extends ServerErrorException { } /* * This is used by the image resizing code when there is an error while resizing */ -class ImageResizeException extends SCoreException +class ImageResizeException extends ServerErrorException { } diff --git a/core/util.php b/core/util.php index 0d2dd8fb..3925d257 100644 --- a/core/util.php +++ b/core/util.php @@ -592,6 +592,7 @@ function _fatal_error(Exception $e): void $message = $e->getMessage(); $phpver = phpversion(); $query = is_subclass_of($e, "SCoreException") ? $e->query : null; + $code = is_subclass_of($e, "SCoreException") ? $e->http_code : 500; //$hash = exec("git rev-parse HEAD"); //$h_hash = $hash ? "

Hash: $hash" : ""; @@ -616,8 +617,10 @@ function _fatal_error(Exception $e): void print("Version: $version (on $phpver)\n"); } else { $q = $query ? "" : "

Query: " . html_escape($query); - error_log("Shimmie Error: $message (Query: $query)\n{$e->getTraceAsString()}"); - header("HTTP/1.0 500 Internal Error"); + if ($code >= 500) { + error_log("Shimmie Error: $message (Query: $query)\n{$e->getTraceAsString()}"); + } + header("HTTP/1.0 $code Error"); echo '