From 4d0f73e538774f6ac397beb5206b68bf43fd1e4d Mon Sep 17 00:00:00 2001 From: bartvde Date: Fri, 10 Jun 2011 17:12:22 +0000 Subject: [PATCH] better error handling for the WFS Protocol, note this is currently only done for WFS 1.1.0 and not for WFS 1.0.0, thanks tschaub for the reviews this week, r=tschaub (closes #3354) git-svn-id: http://svn.openlayers.org/trunk/openlayers@12080 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Format/OWSCommon/v1_0_0.js | 1 + lib/OpenLayers/Format/WFST/v1.js | 3 +- lib/OpenLayers/Format/WFST/v1_1_0.js | 4 +- lib/OpenLayers/Protocol.js | 6 +++ lib/OpenLayers/Protocol/WFS/v1.js | 25 ++++++--- tests/Protocol/WFS.html | 62 +++++++++++++++++++++++ 6 files changed, 91 insertions(+), 10 deletions(-) diff --git a/lib/OpenLayers/Format/OWSCommon/v1_0_0.js b/lib/OpenLayers/Format/OWSCommon/v1_0_0.js index 590cca3b6c..c628a3dfb5 100644 --- a/lib/OpenLayers/Format/OWSCommon/v1_0_0.js +++ b/lib/OpenLayers/Format/OWSCommon/v1_0_0.js @@ -33,6 +33,7 @@ OpenLayers.Format.OWSCommon.v1_0_0 = OpenLayers.Class(OpenLayers.Format.OWSCommo readers: { "ows": OpenLayers.Util.applyDefaults({ "ExceptionReport": function(node, obj) { + obj.success = false; obj.exceptionReport = { version: node.getAttribute('version'), language: node.getAttribute('language'), diff --git a/lib/OpenLayers/Format/WFST/v1.js b/lib/OpenLayers/Format/WFST/v1.js index 2517755719..76c1c49374 100644 --- a/lib/OpenLayers/Format/WFST/v1.js +++ b/lib/OpenLayers/Format/WFST/v1.js @@ -26,7 +26,8 @@ OpenLayers.Format.WFST.v1 = OpenLayers.Class(OpenLayers.Format.XML, { xsi: "http://www.w3.org/2001/XMLSchema-instance", wfs: "http://www.opengis.net/wfs", gml: "http://www.opengis.net/gml", - ogc: "http://www.opengis.net/ogc" + ogc: "http://www.opengis.net/ogc", + ows: "http://www.opengis.net/ows" }, /** diff --git a/lib/OpenLayers/Format/WFST/v1_1_0.js b/lib/OpenLayers/Format/WFST/v1_1_0.js index 4c65d386e2..e03e188bd5 100644 --- a/lib/OpenLayers/Format/WFST/v1_1_0.js +++ b/lib/OpenLayers/Format/WFST/v1_1_0.js @@ -6,6 +6,7 @@ /** * @requires OpenLayers/Format/WFST/v1.js * @requires OpenLayers/Format/Filter/v1_1_0.js + * @requires OpenLayers/Format/OWSCommon/v1_0_0.js */ /** @@ -96,7 +97,8 @@ OpenLayers.Format.WFST.v1_1_0 = OpenLayers.Class( }, OpenLayers.Format.WFST.v1.prototype.readers["wfs"]), "gml": OpenLayers.Format.GML.v3.prototype.readers["gml"], "feature": OpenLayers.Format.GML.v3.prototype.readers["feature"], - "ogc": OpenLayers.Format.Filter.v1_1_0.prototype.readers["ogc"] + "ogc": OpenLayers.Format.Filter.v1_1_0.prototype.readers["ogc"], + "ows": OpenLayers.Format.OWSCommon.v1_0_0.prototype.readers["ows"] }, /** diff --git a/lib/OpenLayers/Protocol.js b/lib/OpenLayers/Protocol.js index 5e73099362..601e59548c 100644 --- a/lib/OpenLayers/Protocol.js +++ b/lib/OpenLayers/Protocol.js @@ -248,6 +248,12 @@ OpenLayers.Protocol.Response = OpenLayers.Class({ */ priv: null, + /** + * Property: error + * {Object} The error object in case a service exception was encountered. + */ + error: null, + /** * Constructor: OpenLayers.Protocol.Response * diff --git a/lib/OpenLayers/Protocol/WFS/v1.js b/lib/OpenLayers/Protocol/WFS/v1.js index 12d557ff7e..af83057978 100644 --- a/lib/OpenLayers/Protocol/WFS/v1.js +++ b/lib/OpenLayers/Protocol/WFS/v1.js @@ -249,13 +249,19 @@ OpenLayers.Protocol.WFS.v1 = OpenLayers.Class(OpenLayers.Protocol, { var request = response.priv; if(request.status >= 200 && request.status < 300) { // success - if (options.readOptions && options.readOptions.output == "object") { - OpenLayers.Util.extend(response, - this.parseResponse(request, options.readOptions)); + var result = this.parseResponse(request, options.readOptions); + if (result && result.success !== false) { + if (options.readOptions && options.readOptions.output == "object") { + OpenLayers.Util.extend(response, result); + } else { + response.features = result; + } + response.code = OpenLayers.Protocol.Response.SUCCESS; } else { - response.features = this.parseResponse(request, options.readOptions); + // failure (service exception) + response.code = OpenLayers.Protocol.Response.FAILURE; + response.error = result; } - response.code = OpenLayers.Protocol.Response.SUCCESS; } else { // failure response.code = OpenLayers.Protocol.Response.FAILURE; @@ -346,9 +352,12 @@ OpenLayers.Protocol.WFS.v1 = OpenLayers.Class(OpenLayers.Protocol, { var obj = this.format.read(data) || {}; response.insertIds = obj.insertIds || []; - response.code = (obj.success) ? - OpenLayers.Protocol.Response.SUCCESS : - OpenLayers.Protocol.Response.FAILURE; + if (obj.success) { + response.code = OpenLayers.Protocol.Response.SUCCESS; + } else { + response.code = OpenLayers.Protocol.Response.FAILURE; + response.error = obj; + } options.callback.call(options.scope, response); } }, diff --git a/tests/Protocol/WFS.html b/tests/Protocol/WFS.html index c6e263e2a1..ec4afcf02a 100644 --- a/tests/Protocol/WFS.html +++ b/tests/Protocol/WFS.html @@ -105,6 +105,68 @@ OpenLayers.Request.POST = _POST; } + function test_exception(t) { + t.plan(8); + var url = "http://some.url.org"; + var protocol = new OpenLayers.Protocol.WFS({ + url: url, + version: "1.1.0", + featureNS: "http://namespace.org", + featureType: "type" + }); + // mock up a response + var response = { + priv: { + status: 200, + responseText: 'Update error: Error occurred updating featuresSecond exception line' + } + }; + var log, entry, expected; + + // test GetFeature + log = []; + protocol.handleRead(OpenLayers.Util.extend({}, response), { + callback: function(resp) { + log.push(resp); + } + }); + expected = { + exceptionReport: { + version: "1.0.0", + language: "en", + exceptions: [{ + code: "InvalidParameterValue", + locator: "foo", + texts: [ + "Update error: Error occurred updating features", + "Second exception line" + ] + }] + }, + success: false + }; + + t.eq(log.length, 1, "GetFeature handled"); + entry = log[0]; + t.eq(entry.code, OpenLayers.Protocol.Response.FAILURE, "GetFeature failure reported"); + t.ok(!!entry.error, "GetFeature got error"); + t.eq(entry.error, expected, "GetFeature error matches expected"); + + // test a commit + log = []; + protocol.handleCommit(response, { + callback: function(resp) { + log.push(resp); + } + }); + t.eq(log.length, 1, "commit handled"); + entry = log[0]; + t.eq(entry.code, OpenLayers.Protocol.Response.FAILURE, "commit failure reported"); + t.ok(!!entry.error, "commit got error"); + t.eq(entry.error, expected, "GetFeature error matches expected"); + + } + function test_commit(t){ t.plan(5);