diff --git a/CHANGELOG.md b/CHANGELOG.md index 64ed11e..65b0520 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 2.2.0 + +- Feature #239: add enumeration style object for error codes + ## 2.1.1 - Fixed #224: response type "arraybuffer" and "blob" not working on browser platform diff --git a/README.md b/README.md index 6c18cab..fec18ef 100644 --- a/README.md +++ b/README.md @@ -292,7 +292,7 @@ cordova.plugin.http.post('https://google.com/', { ``` #### failure -The error function receives a response object with 4 properties: status, error, url, and headers (url and headers being optional). **status** is the HTTP response code as numeric value. **error** is the error response from the server as a string. **url** is the final URL obtained after any redirects as a string. **headers** is an object with the headers. The keys of the returned object are the header names and the values are the respective header values. All header names are lowercase. +The error function receives a response object with 4 properties: status, error, url, and headers (url and headers being optional). **status** is a HTTP response code or an internal error code. Positive values are HTTP status codes whereas negative values do represent internal error codes. **error** is the error response from the server as a string or an internal error message. **url** is the final URL obtained after any redirects as a string. **headers** is an object with the headers. The keys of the returned object are the header names and the values are the respective header values. All header names are lowercase. Here's a quick example: @@ -307,6 +307,8 @@ Here's a quick example: } ``` +:warning: An enumeration style object is exposed as `cordova.plugin.http.ErrorCode`. You can use it to check against internal error codes. + ### get Execute a GET request. Takes a URL, parameters, and headers. See the [post](#post) documentation for details on what is returned on success and failure. diff --git a/package.json b/package.json index 1e375a8..6b4d3ff 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cordova-plugin-advanced-http", - "version": "2.1.1", + "version": "2.2.0", "description": "Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning", "scripts": { "updatecert": "node ./scripts/update-e2e-server-cert.js && node ./scripts/update-e2e-client-cert.js", diff --git a/test/js-specs.js b/test/js-specs.js index eb4c1c4..e860eb5 100644 --- a/test/js-specs.js +++ b/test/js-specs.js @@ -147,6 +147,10 @@ describe('Advanced HTTP public interface', function () { it('throws an Error when you try to configure global option for following redirects with a string', () => { (function () { http.setFollowRedirect('myString'); }).should.throw(messages.INVALID_FOLLOW_REDIRECT_VALUE); }); + + it('exposes an enumeration style object with mappings for the error codes', () => { + Object.keys(http.ErrorCode).forEach(key => http.ErrorCode[key].should.be.a('number')); + }); }); describe('URL util', function () { diff --git a/www/public-interface.js b/www/public-interface.js index 371c4da..694900a 100644 --- a/www/public-interface.js +++ b/www/public-interface.js @@ -1,5 +1,14 @@ module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConfigs) { - const publicInterface = { + var ErrorCode = { + Generic: -1, + SslException: -2, + ServerNotFound: -3, + Timeout: -4, + UnsupportedUrl: -5, + NotConnected: -6, + }; + + var publicInterface = { getBasicAuthHeader: getBasicAuthHeader, useBasicAuth: useBasicAuth, getHeaders: getHeaders, @@ -28,7 +37,8 @@ module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConf delete: del, head: head, uploadFile: uploadFile, - downloadFile: downloadFile + downloadFile: downloadFile, + ErrorCode: ErrorCode }; function getBasicAuthHeader(username, password) {