-
Notifications
You must be signed in to change notification settings - Fork 2k
fix: add error logging to JsonException catch blocks #10253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
34bd2a9
c642948
784ba45
5c7fca7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -112,18 +112,33 @@ public function save(string $key, mixed $value, int $ttl = 60): bool | |
|
|
||
| public function delete(string $key): bool | ||
| { | ||
| $key = static::validateKey($key, $this->prefix); | ||
| $key = static::validateKey($key, $this->prefix); | ||
| $file = $this->path . $key; | ||
|
|
||
| if (! is_file($file)) { | ||
| return false; | ||
| } | ||
|
|
||
| $result = @unlink($file); | ||
|
|
||
| return is_file($this->path . $key) && unlink($this->path . $key); | ||
| if ($result === false) { | ||
| log_message('error', 'Failed to delete cache file: ' . $file); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| public function deleteMatching(string $pattern): int | ||
| { | ||
| $deleted = 0; | ||
|
|
||
| foreach (glob($this->path . $pattern, GLOB_NOSORT) as $filename) { | ||
| if (is_file($filename) && @unlink($filename)) { | ||
| $deleted++; | ||
| if (is_file($filename)) { | ||
| if (@unlink($filename)) { | ||
| $deleted++; | ||
| } else { | ||
| log_message('error', 'Failed to delete cache file: ' . $filename); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -200,6 +215,8 @@ protected function getItem(string $filename): array|false | |
| $content = @file_get_contents($this->path . $filename); | ||
|
|
||
| if ($content === false) { | ||
| log_message('error', 'Failed to read cache file: ' . $this->path . $filename); | ||
|
|
||
| return false; | ||
| } | ||
|
Comment on lines
217
to
221
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, same as above, since we're already returning false, that would already mean the read operation failed, so we can just return false instead of logging. |
||
|
|
||
|
|
@@ -222,7 +239,11 @@ protected function getItem(string $filename): array|false | |
| } | ||
|
|
||
| if ($data['ttl'] > 0 && Time::now()->getTimestamp() > $data['time'] + $data['ttl']) { | ||
| @unlink($this->path . $filename); | ||
| $file = $this->path . $filename; | ||
|
|
||
| if (is_file($file) && ! @unlink($file)) { | ||
| log_message('error', 'Failed to delete expired cache file: ' . $file); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
@@ -242,6 +263,8 @@ protected function getItem(string $filename): array|false | |
| protected function writeFile($path, $data, $mode = 'wb'): bool | ||
| { | ||
| if (($fp = @fopen($path, $mode)) === false) { | ||
| log_message('error', 'Failed to open cache file for writing: ' . $path); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
|
|
@@ -280,6 +303,8 @@ protected function deleteFiles(string $path, bool $delDir = false, bool $htdocs | |
| $path = rtrim($path, '/\\'); | ||
|
|
||
| if (! $currentDir = @opendir($path)) { | ||
| log_message('error', 'Failed to open cache directory: ' . $path); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
|
|
@@ -288,14 +313,27 @@ protected function deleteFiles(string $path, bool $delDir = false, bool $htdocs | |
| if (is_dir($path . DIRECTORY_SEPARATOR . $filename) && $filename[0] !== '.') { | ||
| $this->deleteFiles($path . DIRECTORY_SEPARATOR . $filename, $delDir, $htdocs, $_level + 1); | ||
| } elseif (! $htdocs || preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename) !== 1) { | ||
| @unlink($path . DIRECTORY_SEPARATOR . $filename); | ||
| $file = $path . DIRECTORY_SEPARATOR . $filename; | ||
| if (! @unlink($file)) { | ||
| log_message('error', 'Failed to delete cache file: ' . $file); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| closedir($currentDir); | ||
|
|
||
| return ($delDir && $_level > 0) ? @rmdir($path) : true; | ||
| if ($delDir && $_level > 0) { | ||
| $result = @rmdir($path); | ||
|
|
||
| if (! $result) { | ||
| log_message('error', 'Failed to remove cache directory: ' . $path); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -295,6 +295,7 @@ private function removeTokenInRequest(IncomingRequest $request): void | |
| try { | ||
| $json = json_decode($body, flags: JSON_THROW_ON_ERROR); | ||
| } catch (JsonException) { | ||
| log_message('error', 'Invalid JSON in request body during CSRF token removal'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An |
||
| $json = null; | ||
| } | ||
|
|
||
|
|
@@ -346,6 +347,7 @@ private function getPostedToken(IncomingRequest $request): ?string | |
| try { | ||
| $json = json_decode($body, flags: JSON_THROW_ON_ERROR); | ||
| } catch (JsonException) { | ||
| log_message('error', 'Invalid JSON in request body during CSRF token retrieval'); | ||
| $json = null; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if we actually need this logging or not. Since the
delete()method already returnsbool, whether to log or not would be user's preference rather than framework responsibility. If they decide to not log and just pass on, thislog_message()call would be redundant in that case