[gnome-documents/wip/rishi/onedrive: 5/13] documents: Tie the catch blocks more tightly to the failure sites



commit 9874742871aff94a05525a92c5a3a25e7225150b
Author: Debarshi Ray <debarshir gnome org>
Date:   Wed Apr 5 15:50:37 2017 +0200

    documents: Tie the catch blocks more tightly to the failure sites
    
    Having a lot of code inside a try statement makes it harder to follow
    the error handling logic. The catch statements are far away from the
    code that can throw an exception, so it is difficult to match them
    together. Let's reduce the size of the try blocks by only using them
    for fallible operations.

 src/documents.js |   50 +++++++++++++++++++++++++++++---------------------
 1 files changed, 29 insertions(+), 21 deletions(-)
---
diff --git a/src/documents.js b/src/documents.js
index db296a6..1b0d6f1 100644
--- a/src/documents.js
+++ b/src/documents.js
@@ -525,34 +525,42 @@ const DocCommon = new Lang.Class({
 
         thumbFile.read_async(GLib.PRIORITY_DEFAULT, null, Lang.bind(this,
             function(object, res) {
-                try {
-                    let stream = object.read_finish(res);
-                    let scale = Application.application.getScaleFactor();
-                    GdkPixbuf.Pixbuf.new_from_stream_at_scale_async(stream,
-                        Utils.getIconSize() * scale, Utils.getIconSize() * scale,
-                        true, null, Lang.bind(this,
-                            function(object, res) {
-                                try {
-                                    let pixbuf = GdkPixbuf.Pixbuf.new_from_stream_finish(res);
-                                    this._setOrigPixbuf(pixbuf);
-                                } catch (e) {
-                                    if (!e.matches(GdkPixbuf.PixbufError, 
GdkPixbuf.PixbufError.UNKNOWN_TYPE))
-                                        logError(e, 'Unable to create pixbuf from ' + thumbFile.get_uri());
-
-                                    this._failedThumbnailing = true;
-                                    this._thumbPath = null;
-                                    thumbFile.delete_async(GLib.PRIORITY_DEFAULT, null, null);
-                                }
+                let stream;
 
-                                // close the underlying stream immediately
-                                stream.close_async(0, null, null);
-                            }));
+                try {
+                    stream = object.read_finish(res);
                 } catch (e) {
                     logError(e, 'Unable to read file at ' + thumbFile.get_uri());
                     this._failedThumbnailing = true;
                     this._thumbPath = null;
                     thumbFile.delete_async(GLib.PRIORITY_DEFAULT, null, null);
+                    return;
                 }
+
+                let scale = Application.application.getScaleFactor();
+                GdkPixbuf.Pixbuf.new_from_stream_at_scale_async(stream,
+                    Utils.getIconSize() * scale, Utils.getIconSize() * scale,
+                    true, null, Lang.bind(this,
+                        function(object, res) {
+                            // close the underlying stream immediately
+                            stream.close_async(0, null, null);
+
+                            let pixbuf;
+
+                            try {
+                                pixbuf = GdkPixbuf.Pixbuf.new_from_stream_finish(res);
+                            } catch (e) {
+                                if (!e.matches(GdkPixbuf.PixbufError, GdkPixbuf.PixbufError.UNKNOWN_TYPE))
+                                    logError(e, 'Unable to create pixbuf from ' + thumbFile.get_uri());
+
+                                this._failedThumbnailing = true;
+                                this._thumbPath = null;
+                                thumbFile.delete_async(GLib.PRIORITY_DEFAULT, null, null);
+                                return;
+                            }
+
+                            this._setOrigPixbuf(pixbuf);
+                        }));
             }));
     },
 


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