From 979c5251240d0de08b2b4a816801a021240ee2ee Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Tue, 2 May 2017 09:07:39 +0200 Subject: [PATCH] I like to throw exceptions instead of using if statements. --- classes/File.php | 10 +++++++--- classes/File_redirection.php | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/classes/File.php b/classes/File.php index b0da3f09f3..9cfdb94c56 100644 --- a/classes/File.php +++ b/classes/File.php @@ -194,10 +194,14 @@ class File extends Managed_DataObject } $redir = File_redirection::where($given_url); - $file = $redir->getFile(); - - if (!$file instanceof File || empty($file->id)) { + try { + $file = $redir->getFile(); + } catch (EmptyPkeyValueException $e) { + common_log(LOG_ERR, 'File_redirection::where gave object with empty file_id for given_url '._ve($given_url)); + throw new ServerException('URL processing failed without new File object'); + } catch (NoResultException $e) { // This should not happen + common_log(LOG_ERR, 'File_redirection after discovery could still not return a File object.'); throw new ServerException('URL processing failed without new File object'); } diff --git a/classes/File_redirection.php b/classes/File_redirection.php index d1b266c90b..742a6143cc 100644 --- a/classes/File_redirection.php +++ b/classes/File_redirection.php @@ -445,8 +445,8 @@ class File_redirection extends Managed_DataObject } public function getFile() { - if(empty($this->file) && $this->file_id) { - $this->file = File::getKV('id', $this->file_id); + if (!$this->file instanceof File) { + $this->file = File::getByID($this->file_id); } return $this->file; -- 2.39.5