core,server: Use proper error codes
authorJens Georg <jensg@openismus.com>
Mon, 5 Aug 2013 13:03:35 +0000 (15:03 +0200)
committerJens Georg <jensg@openismus.com>
Tue, 6 Aug 2013 13:05:15 +0000 (15:05 +0200)
There are several places where the wrong error code was used.

https://bugzilla.gnome.org/show_bug.cgi?id=702452

src/librygel-core/rygel-connection-manager.vala
src/librygel-server/rygel-content-directory.vala
src/librygel-server/rygel-import-resource.vala
src/librygel-server/rygel-media-query-action.vala
src/librygel-server/rygel-object-creator.vala

index 75f1aed..b9a4108 100644 (file)
@@ -117,14 +117,15 @@ public class Rygel.ConnectionManager : Service {
         string connection_id;
 
         action.get ("ConnectionID", typeof (string), out connection_id);
-        if (connection_id == null || connection_id != "0") {
-            action.return_error (706, _("Invalid connection reference"));
+        if (connection_id == null || action.get_argument_count () != 1 ||
+            (connection_id != "0" && int.parse (connection_id) == 0)) {
+            action.return_error (402, _("Invalid argument"));
 
             return;
         }
 
-        if (action.get_argument_count () != 1) {
-            action.return_error (402, _("Invalid argument"));
+        if (connection_id != "0") {
+            action.return_error (706, _("Invalid connection reference"));
 
             return;
         }
index 9b9a147..adf2f24 100644 (file)
@@ -41,6 +41,7 @@ internal errordomain Rygel.ContentDirectoryError {
     RESTRICTED_OBJECT = 711,
     BAD_METADATA = 712,
     RESTRICTED_PARENT = 713,
+    NO_SUCH_FILE_TRANSFER = 717,
     NO_SUCH_DESTINATION_RESOURCE = 718,
     CANT_PROCESS = 720,
     OUTDATED_OBJECT_METADATA = 728,
@@ -267,8 +268,9 @@ internal class Rygel.ContentDirectory: Service {
             return;
         }
 
-        var import = find_import_for_action (action);
-        if (import != null) {
+        try {
+            var import = this.find_import_for_action (action);
+
             action.set ("TransferStatus",
                             typeof (string),
                             import.status_as_string,
@@ -280,8 +282,8 @@ internal class Rygel.ContentDirectory: Service {
                             import.bytes_total);
 
             action.return ();
-        } else {
-            action.return_error (717, _("No such file transfer"));
+        } catch (Error error) {
+            action.return_error (error.code, error.message);
         }
     }
 
@@ -294,13 +296,13 @@ internal class Rygel.ContentDirectory: Service {
             return;
         }
 
-        var import = find_import_for_action (action);
-        if (import != null) {
+        try {
+            var import = find_import_for_action (action);
             import.cancellable.cancel ();
 
             action.return ();
-        } else {
-            action.return_error (717, _("No such file transfer"));
+        } catch (Error error) {
+            action.return_error (error.code, error.message);
         }
     }
 
@@ -596,13 +598,24 @@ internal class Rygel.ContentDirectory: Service {
         });
     }
 
-    private ImportResource? find_import_for_action (ServiceAction action) {
+    private ImportResource? find_import_for_action (ServiceAction action)
+                                            throws ContentDirectoryError {
         ImportResource ret = null;
         uint32 transfer_id;
+        string transfer_id_string;
 
+        // TODO: Remove string hack once bgo#705516 is fixed
         action.get ("TransferID",
                         typeof (uint32),
-                        out transfer_id);
+                        out transfer_id,
+                    "TransferID",
+                        typeof (string),
+                        out transfer_id_string);
+        if (transfer_id == 0 &&
+            (transfer_id_string == null || transfer_id_string != "0")) {
+            throw new ContentDirectoryError.INVALID_ARGS
+                                        (_("Invalid argument"));
+        }
 
         foreach (var import in this.active_imports) {
             if (import.transfer_id == transfer_id) {
@@ -620,6 +633,11 @@ internal class Rygel.ContentDirectory: Service {
             }
         }
 
+        if (ret == null) {
+            throw new ContentDirectoryError.NO_SUCH_FILE_TRANSFER
+                                        (_("No such file transfer"));
+        }
+
         return ret;
     }
 
index 06a01f4..69c49f3 100644 (file)
@@ -125,14 +125,7 @@ internal class Rygel.ImportResource : GLib.Object, Rygel.StateMachine {
                      this.destination_uri,
                      error.message);
 
-            int code;
-            if (error is ContentDirectoryError) {
-                code = error.code;
-            } else {
-                code = 719;
-            }
-
-            this.action.return_error (code, error.message);
+            this.action.return_error (error.code, error.message);
             this.status = TransferStatus.ERROR;
             this.completed ();
 
@@ -174,8 +167,14 @@ internal class Rygel.ImportResource : GLib.Object, Rygel.StateMachine {
     }
 
     private async MediaItem fetch_item () throws Error {
-        var uri = new HTTPItemURI.from_string (this.destination_uri,
+        HTTPItemURI uri;
+        try {
+            uri = new HTTPItemURI.from_string (this.destination_uri,
                                                this.http_server);
+        } catch (Error error) {
+            throw new ContentDirectoryError.NO_SUCH_DESTINATION_RESOURCE
+                                            (error.message);
+        }
         var media_object = yield this.root_container.find_object (uri.item_id,
                                                                   null);
         string msg = null;
index c816f16..774f24c 100644 (file)
@@ -111,8 +111,8 @@ internal abstract class Rygel.MediaQueryAction : GLib.Object, StateMachine {
 
         if (this.object_id == null) {
             // Sorry we can't do anything without ObjectID
-            throw new ContentDirectoryError.NO_SUCH_OBJECT
-                                        (_("No such object"));
+            throw new ContentDirectoryError.INVALID_ARGS
+                                        (_("ObjectID argument missing"));
         }
 
         if (index < 0 || requested_count < 0) {
index 4c2cbdf..ce5789c 100644 (file)
@@ -197,8 +197,8 @@ internal class Rygel.ObjectCreator: GLib.Object, Rygel.StateMachine {
 
         if (this.container_id == null) {
             // Sorry we can't do anything without ContainerID
-            throw new ContentDirectoryError.NO_SUCH_OBJECT
-                                        (_("No such object"));
+            throw new ContentDirectoryError.INVALID_ARGS
+                                        (_("Missing ContainerID argument"));
         }
     }