[gnome-maps/wip/routing2: 3/3] RouteService: Add error checking



commit 3301a2e59f062e41b6a6fb78349730cbd2323b97
Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
Date:   Sun May 4 00:42:45 2014 +0200

    RouteService: Add error checking
    
    Add error checking for the route fetching. At some point we want to
    show notifications when we get errors also, but that's not today.

 src/routeService.js |  114 +++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 92 insertions(+), 22 deletions(-)
---
diff --git a/src/routeService.js b/src/routeService.js
index aa77525..1b1c725 100644
--- a/src/routeService.js
+++ b/src/routeService.js
@@ -33,6 +33,55 @@ const RouteQuery = imports.routeQuery;
 const EPAF = imports.epaf;
 const HTTP = imports.http;
 
+const RouteError = new Lang.Class({
+    Name: 'RouteError',
+
+    _init: function(logMsg, debugMsg) {
+        this._logMsg    = logMsg;
+        this._debugMsgs = debugMsg;
+    },
+
+    debug: function() {
+        this._debugMsgs.forEach((function(msg) {
+            Utils.debug(msg);
+        }).bind(this));
+    },
+
+    toString: function() {
+        return this._logMsg;
+    }
+});
+
+const ParseMsgError = new Lang.Class({
+    Name: 'ParseMsgError',
+    Extends: RouteError,
+
+    _init: function(httpCode, httpBody, errors) {
+        let debugMsgs = ["HTTP code: "  + httpCode];
+        if(errors.length > 0) {
+            debugMsgs.push("Errors: {");
+            errors.forEach(function({ details, msg }, i) {
+                debugMsgs.push("    Message[" + (i + 1) + "]: " + msg);
+                debugMsgs.push("    Details[" + (i + 1) + "]: " + details);
+            });
+            debugMsgs.push("}");
+        }
+        debugMsgs.push("HTTP Body: {\n" + httpBody + "\n}");
+        this.parent(_("The route search result had error(s)."),
+                    debugMsgs);
+    }
+});
+
+const CreateRouteError = new Lang.Class({
+    Name: 'CreateRouteError',
+    Extends: RouteError,
+
+    _init: function(path) {
+        this.parent("Couldn't parse the route result JSON.",
+                    [JSON.stringify(path)]);
+    }
+});
+
 const GraphHopper = new Lang.Class({
     Name: 'GraphHopper',
 
@@ -67,11 +116,16 @@ const GraphHopper = new Lang.Class({
         let url = this._buildURL(viaPoints, transportationType);
         let msg = Soup.Message.new('GET', url);
         this._session.queue_message(msg, (function(session, message) {
-            if (message.status_code === 200) {
-                let result = message.response_body.data;
-                this.route.update(this._parseResult(result));
-            } else {
-                log("Error: " + message.status_code);
+            try {
+                let result = this._parseMessage(message);
+                let route  = this._createRoute(result);
+                this.route.update(route);
+            } catch(e) {
+                if(e instanceof RouteError) {
+                    log(e);
+                    e.debug();
+                } else
+                    throw e;
             }
         }).bind(this));
     },
@@ -92,23 +146,39 @@ const GraphHopper = new Lang.Class({
         return url;
     },
 
-    // TODO: error handling
-    _parseResult: function(result) {
-        // Always the first path until GH has alternate routes support
-        let route      = JSON.parse(result).paths[0];
-        let path       = EPAF.decode(route.points);
-        let turnPoints = this._createTurnPoints(path, route.instructions);
-        let bbox       = new Champlain.BoundingBox();
-
-        // GH does lonlat-order and Champlain latlon-order
-        bbox.extend(route.bbox[1], route.bbox[0]);
-        bbox.extend(route.bbox[3], route.bbox[2]);
-
-        return { path:       path,
-                 turnPoints: turnPoints,
-                 distance:   route.distance,
-                 time:       route.time,
-                 bbox:       bbox };
+    _parseMessage: function({ status_code, response_body }) {
+        let errors = [];
+        if (status_code === 200 && response_body) {
+            let result = JSON.parse(response_body.data);
+            let info   = result.info;
+            let paths  = result.paths;
+            if(paths && paths[0])
+                return paths[0];
+            else if(info && info.errors) {
+                errors = info.errors;
+            }
+        }
+        throw new ParseMsgError(status_code, response_body.data, errors);
+    },
+
+    _createRoute: function(result) {
+        try {
+            let path       = EPAF.decode(result.points);
+            let turnPoints = this._createTurnPoints(path, result.instructions);
+            let bbox       = new Champlain.BoundingBox();
+
+            // GH does lonlat-order and Champlain latlon-order
+            bbox.extend(result.bbox[1], result.bbox[0]);
+            bbox.extend(result.bbox[3], result.bbox[2]);
+
+            return { path:       path,
+                     turnPoints: turnPoints,
+                     distance:   result.distance,
+                     time:       result.time,
+                     bbox:       bbox };
+        } catch (e) {
+            throw new CreateRouteError(result);
+        }
     },
 
     _createTurnPoints: function(path, instructions) {


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