[rygel] core: Better error handling in HTTPRequest & subclasses



commit ccf2d6b778e133ef5281ba2be786518ec04070b9
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Mon Aug 2 19:58:45 2010 +0300

    core: Better error handling in HTTPRequest & subclasses
    
    Simpler and more robust error handling in HTTPRequest & its subclasses.

 src/rygel/rygel-http-get.vala     |   76 ++++++++++++++----------------------
 src/rygel/rygel-http-post.vala    |   19 +++------
 src/rygel/rygel-http-request.vala |   31 ++++++---------
 3 files changed, 48 insertions(+), 78 deletions(-)
---
diff --git a/src/rygel/rygel-http-get.vala b/src/rygel/rygel-http-get.vala
index 06e9412..bc3dfef 100644
--- a/src/rygel/rygel-http-get.vala
+++ b/src/rygel/rygel-http-get.vala
@@ -47,7 +47,7 @@ internal class Rygel.HTTPGet : HTTPRequest {
         this.subtitle_index = -1;
     }
 
-    protected override async void handle () {
+    protected override async void handle () throws Error {
         this.server.pause_message (this.msg);
 
         yield base.handle ();
@@ -58,20 +58,14 @@ internal class Rygel.HTTPGet : HTTPRequest {
         /* We only entertain 'HEAD' and 'GET' requests */
         if ((this.msg.method != "HEAD" && this.msg.method != "GET") ||
             (header != null && header != "1")) {
-            this.handle_error (new HTTPRequestError.BAD_REQUEST (
-                                        _("Invalid Request")));
-            return;
+            throw new HTTPRequestError.BAD_REQUEST (_("Invalid Request"));
         }
 
-        try {
-            if (uri.transcode_target != null) {
-                var transcoder = this.http_server.get_transcoder (
+        if (uri.transcode_target != null) {
+            var transcoder = this.http_server.get_transcoder (
                                                         uri.transcode_target);
-                this.handler = new HTTPTranscodeHandler (transcoder,
-                                                         this.cancellable);
-            }
-        } catch (Error err) {
-            warning (_("Failed to parse query: %s"), err.message);
+            this.handler = new HTTPTranscodeHandler (transcoder,
+                                                     this.cancellable);
         }
 
         if (this.handler == null) {
@@ -81,7 +75,7 @@ internal class Rygel.HTTPGet : HTTPRequest {
         yield this.handle_item_request ();
     }
 
-    protected override async void find_item () {
+    protected override async void find_item () throws Error {
         yield base.find_item ();
 
         if (this.uri.thumbnail_index >= 0) {
@@ -92,42 +86,32 @@ internal class Rygel.HTTPGet : HTTPRequest {
         }
     }
 
-    private async void handle_item_request () {
-        try {
-            if (HTTPTimeSeek.needed (this)) {
-                this.seek = new HTTPTimeSeek (this);
-            } else if (HTTPByteSeek.needed (this)) {
-                this.seek = new HTTPByteSeek (this);
-            }
-
-            // Add headers
-            this.handler.add_response_headers (this);
-            debug (_("Following HTTP headers appended to response:"));
-            this.msg.response_headers.foreach ((name, value) => {
-                    debug ("%s : %s", name, value);
-            });
-
-            if (this.msg.method == "HEAD") {
-                // Only headers requested, no need to send contents
-                this.server.unpause_message (this.msg);
-                this.end (Soup.KnownStatusCode.OK);
-                return;
-            }
-
-            var response = this.handler.render_body (this);
-
-            yield response.run ();
-
-            this.end (Soup.KnownStatusCode.NONE);
-        } catch (Error error) {
-            this.handle_error (error);
+    private async void handle_item_request () throws Error {
+        if (HTTPTimeSeek.needed (this)) {
+            this.seek = new HTTPTimeSeek (this);
+        } else if (HTTPByteSeek.needed (this)) {
+            this.seek = new HTTPByteSeek (this);
         }
-    }
 
-    protected override void handle_error (Error error) {
-        this.server.unpause_message (this.msg);
+        // Add headers
+        this.handler.add_response_headers (this);
+        debug (_("Following HTTP headers appended to response:"));
+        this.msg.response_headers.foreach ((name, value) => {
+                debug ("%s : %s", name, value);
+                });
+
+        if (this.msg.method == "HEAD") {
+            // Only headers requested, no need to send contents
+            this.server.unpause_message (this.msg);
+            this.end (Soup.KnownStatusCode.OK);
+            return;
+        }
+
+        var response = this.handler.render_body (this);
+
+        yield response.run ();
 
-        base.handle_error (error);
+        this.end (Soup.KnownStatusCode.NONE);
     }
 }
 
diff --git a/src/rygel/rygel-http-post.vala b/src/rygel/rygel-http-post.vala
index 524fb15..568f96c 100644
--- a/src/rygel/rygel-http-post.vala
+++ b/src/rygel/rygel-http-post.vala
@@ -40,33 +40,26 @@ internal class Rygel.HTTPPost : HTTPRequest {
         base (http_server, server, msg);
     }
 
-    protected override async void handle () {
+    protected override async void handle () throws Error {
         this.msg.got_chunk.connect (this.on_got_chunk);
         this.msg.got_body.connect (this.on_got_body);
 
         this.server.pause_message (this.msg);
         yield base.handle ();
 
-        try {
-            this.file = yield this.item.get_writable (this.cancellable);
-            if (this.file == null) {
-                throw new HTTPRequestError.BAD_REQUEST (
+        this.file = yield this.item.get_writable (this.cancellable);
+        if (this.file == null) {
+            throw new HTTPRequestError.BAD_REQUEST (
                                         _("No writable URI for %s available"),
                                         this.item.id);
-            }
+        }
 
-            this.stream = yield this.file.replace_async (
+        this.stream = yield this.file.replace_async (
                                         null,
                                         false,
                                         FileCreateFlags.REPLACE_DESTINATION,
                                         Priority.LOW,
                                         this.cancellable);
-        } catch (Error error) {
-            this.server.unpause_message (this.msg);
-            this.handle_error (error);
-
-            return;
-        }
 
         this.server.unpause_message (this.msg);
         this.handle_continue = this.handle.callback;
diff --git a/src/rygel/rygel-http-request.vala b/src/rygel/rygel-http-request.vala
index 35dd24f..b0b0fff 100644
--- a/src/rygel/rygel-http-request.vala
+++ b/src/rygel/rygel-http-request.vala
@@ -54,47 +54,40 @@ internal abstract class Rygel.HTTPRequest : GLib.Object, Rygel.StateMachine {
     }
 
     public async void run () {
-        yield this.handle ();
-    }
-
-    protected virtual async void handle () {
         try {
-            this.uri = new HTTPItemURI.from_string (this.msg.uri.path,
-                                                    this.http_server);
+            yield this.handle ();
         } catch (Error error) {
             this.handle_error (error);
 
             return;
         }
+    }
+
+    protected virtual async void handle () throws Error {
+        this.uri = new HTTPItemURI.from_string (this.msg.uri.path,
+                                                this.http_server);
 
         yield this.find_item ();
     }
 
-    protected virtual async void find_item () {
+    protected virtual async void find_item () throws Error {
         // Fetch the requested item
-        MediaObject media_object;
-        try {
-            media_object = yield this.root_container.find_object (
+        var media_object = yield this.root_container.find_object (
                                         this.uri.item_id,
                                         null);
-        } catch (Error err) {
-            this.handle_error (err);
-
-            return;
-        }
 
         if (media_object == null || !(media_object is MediaItem)) {
-            this.handle_error (new HTTPRequestError.NOT_FOUND (
+            throw new HTTPRequestError.NOT_FOUND (
                                         _("Requested item '%s' not found"),
-                                        this.uri.item_id));
-            return;
+                                        this.uri.item_id);
         }
 
         this.item = (MediaItem) media_object;
     }
 
-    protected virtual void handle_error (Error error) {
+    protected void handle_error (Error error) {
         warning ("%s", error.message);
+        this.server.unpause_message (this.msg);
 
         uint status;
         if (error is HTTPRequestError) {



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