[geary/mjog/imap-connection-fixes: 31/34] Geary.Imap.Command: Add throw_on_error method



commit 15c9cc48faa0cb631b5fe8494afa3ffaba40e7e4
Author: Michael Gratton <mike vee net>
Date:   Thu Mar 26 22:20:45 2020 +1100

    Geary.Imap.Command: Add throw_on_error method
    
    Add method that checks the command's status response and throws an
    appropriate error if it indicates that something we care about is
    wrong.

 src/engine/imap/command/imap-command.vala | 102 ++++++++++++++++++++++++++++++
 test/meson.build                          |   1 +
 test/test-engine.vala                     |   1 +
 3 files changed, 104 insertions(+)
---
diff --git a/src/engine/imap/command/imap-command.vala b/src/engine/imap/command/imap-command.vala
index 58163671..322b18d2 100644
--- a/src/engine/imap/command/imap-command.vala
+++ b/src/engine/imap/command/imap-command.vala
@@ -271,6 +271,108 @@ public abstract class Geary.Imap.Command : BaseObject {
         }
     }
 
+    /**
+     * Throws an error if this command's status response is NO or BAD.
+     *
+     * If the response is NO, an ImapError.OPERATIONAL_ERROR is
+     * thrown. If the response is BAD, an ImapError.SERVER_ERROR is
+     * thrown. If a specific response code is set, another more
+     * appropriate exception may be thrown. The given command is used
+     * to provide additional context information in case an error is
+     * thrown.
+     */
+    public void throw_on_error() throws ImapError {
+        StatusResponse? response = this.status;
+        if (response != null && response.status in new Status[] { BAD, NO }) {
+            ResponseCode? code = response.response_code;
+            if (code != null) {
+                ResponseCodeType code_type = code.get_response_code_type();
+                switch (code_type.value) {
+                case ResponseCodeType.ALREADYEXISTS:
+                    throw new ImapError.SERVER_ERROR(
+                        "%s: Already exists: %s",
+                        to_brief_string(),
+                        response.to_string()
+                    );
+
+                case ResponseCodeType.AUTHENTICATIONFAILED:
+                    throw new ImapError.UNAUTHENTICATED(
+                        "%s: Bad credentials: %s",
+                        to_brief_string(),
+                        response.to_string()
+                    );
+
+                case ResponseCodeType.AUTHORIZATIONFAILED:
+                    throw new ImapError.SERVER_ERROR(
+                        "%s: Not authorised: %s",
+                        to_brief_string(),
+                        response.to_string()
+                    );
+
+                case ResponseCodeType.CANNOT:
+                    throw new ImapError.SERVER_ERROR(
+                        "%s: Cannot be performed: %s",
+                        to_brief_string(),
+                        response.to_string()
+                    );
+
+                case ResponseCodeType.LIMIT:
+                    throw new ImapError.SERVER_ERROR(
+                        "%s: Hit limit: %s",
+                        to_brief_string(),
+                        response.to_string()
+                    );
+
+                case ResponseCodeType.NOPERM:
+                    throw new ImapError.SERVER_ERROR(
+                        "%s: Not permitted by ACL: %s",
+                        to_brief_string(),
+                        response.to_string()
+                    );
+
+                case ResponseCodeType.NONEXISTENT:
+                    throw new ImapError.SERVER_ERROR(
+                        "%s: Does not exist: %s",
+                        to_brief_string(),
+                        response.to_string()
+                    );
+
+                case ResponseCodeType.OVERQUOTA:
+                    throw new ImapError.SERVER_ERROR(
+                        "%s: Over quota: %s",
+                        to_brief_string(),
+                        response.to_string()
+                    );
+
+                case ResponseCodeType.UNAVAILABLE:
+                    throw new ImapError.UNAVAILABLE(
+                        "%s: Server is unavailable: %s",
+                        to_brief_string(),
+                        response.to_string()
+                    );
+                }
+            }
+
+            // No interesting response code, so just throw a generic
+            // error
+            switch (response.status) {
+            case Status.NO:
+                throw new ImapError.OPERATIONAL_ERROR(
+                    "%s: Operational server error: %s",
+                    to_brief_string(),
+                    response.to_string()
+                );
+
+            case Status.BAD:
+                throw new ImapError.SERVER_ERROR(
+                    "%s: Fatal server error: %s",
+                    to_brief_string(),
+                    response.to_string()
+                );
+            }
+        }
+    }
+
     public virtual string to_string() {
         string args = this.args.to_string();
         return (Geary.String.is_empty(args))
diff --git a/test/meson.build b/test/meson.build
index 9cd4717f..86d7782d 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -36,6 +36,7 @@ geary_test_engine_sources = [
   'engine/common/common-contact-harvester-test.vala',
   'engine/db/db-database-test.vala',
   'engine/db/db-versioned-database-test.vala',
+  'engine/imap/command/imap-command-test.vala',
   'engine/imap/command/imap-create-command-test.vala',
   'engine/imap/command/imap-fetch-command-test.vala',
   'engine/imap/message/imap-data-format-test.vala',
diff --git a/test/test-engine.vala b/test/test-engine.vala
index a70cb11c..1826609f 100644
--- a/test/test-engine.vala
+++ b/test/test-engine.vala
@@ -50,6 +50,7 @@ int main(string[] args) {
     // Other IMAP tests rely on these working, so test them first
     engine.add_suite(new Geary.Imap.DataFormatTest().get_suite());
 
+    engine.add_suite(new Geary.Imap.CommandTest().get_suite());
     engine.add_suite(new Geary.Imap.CreateCommandTest().get_suite());
     engine.add_suite(new Geary.Imap.FetchCommandTest().get_suite());
     engine.add_suite(new Geary.Imap.ListParameterTest().get_suite());


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]