From f91c88cad35b5c7d0f9054f9fe69626729f170c2 Mon Sep 17 00:00:00 2001 From: Synox Date: Fri, 23 Feb 2018 22:27:20 +0100 Subject: [PATCH] move error --- src/imap_client.php | 4 +++- src/index.php | 15 --------------- src/pages.php | 12 +++++++++++- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/imap_client.php b/src/imap_client.php index 843ee2d..1b6a8f4 100644 --- a/src/imap_client.php +++ b/src/imap_client.php @@ -39,7 +39,9 @@ class ImapClient { $this->mailbox->deleteMail($mailid); $this->mailbox->expungeDeletedMails(); } else { - error(404, 'delete error: invalid username/mailid combination'); + // TODO: use return/throw instead + @http_response_code(404); + die('delete error: invalid username/mailid combination'); } } diff --git a/src/index.php b/src/index.php index b28ca39..11d23ef 100644 --- a/src/index.php +++ b/src/index.php @@ -26,20 +26,5 @@ $page->invoke($imapClient); // delete after each request $imapClient->delete_old_messages($config['delete_messages_older_than']); -/** - * print error and stop program. - * @param $status integer http status - * @param $text string error text - */ -function error($status, $text) { - @http_response_code($status); - @print("{\"error\": \"$text\"}"); - die(); -} - - - - - ?> \ No newline at end of file diff --git a/src/pages.php b/src/pages.php index c46cf62..5da1ddf 100644 --- a/src/pages.php +++ b/src/pages.php @@ -25,6 +25,16 @@ abstract class Page { header("location: ?$name@$domain"); } + /** + * print error and stop program. + * @param $status integer http status + * @param $text string error text + */ + function error($status, $text) { + @http_response_code($status); + die("{\"error\": \"$text\"}"); + } + } class RedirectToAddressPage extends Page { @@ -123,6 +133,6 @@ class DisplayEmailsPage extends Page { class InvalidRequestPage extends Page { function invoke(ImapClient $imapClient) { - error(400, "Bad Request"); + $this->error(400, "Bad Request"); } }