[gnome-maps/wip/routing2] RouteService: Add error checking
- From: Mattias Bengtsson <mattiasb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-maps/wip/routing2] RouteService: Add error checking
- Date: Sat, 3 May 2014 22:59:18 +0000 (UTC)
commit 964ac632dc385dc933fcaac8305f5db15e91af46
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 | 115 ++++++++++++++++++++++++++++++++++++++++----------
1 files changed, 92 insertions(+), 23 deletions(-)
---
diff --git a/src/routeService.js b/src/routeService.js
index aa77525..fdea5a0 100644
--- a/src/routeService.js
+++ b/src/routeService.js
@@ -33,6 +33,53 @@ const RouteQuery = imports.routeQuery;
const EPAF = imports.epaf;
const HTTP = imports.http;
+const RouteError = new Lang.Class({
+ Name: 'RouteError',
+
+ _init: function(message, debugMessages) {
+ this._message = message;
+ this._debugMessages = debugMessages;
+ },
+
+ debug: function() {
+ this._debugMessages.forEach(Utils.debug);
+ },
+
+ toString: function() {
+ return this._message;
+ }
+});
+
+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 +114,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));
},
@@ -85,30 +137,47 @@ const GraphHopper = new Lang.Class({
key: this._key,
vehicle: vehicle,
locale: this._locale,
- point: points
+ point: points,
+ debug: Utils.debugEnabled
});
let url = this._baseURL + query.toString();
Utils.debug("Sending route request to: " + url);
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]