Parse strings for W3C and JSON
MODIFY
lib/protocol/errors.js
lib/protocol/errors.js
@@ -552,9 +552,13 @@ class ProxyRequestError extends ES6Error {
super(err || message);
this.w3cStatus = HTTPStatusCodes.BAD_REQUEST;
+
+ // If a string was provided, parse the string
if (_.isString(responseError)) {
- this.jsonwp = JSON.parse(responseError);
- } else {
+ responseError = JSON.parse(responseError);
+ }
+
+ // If the response error is an object and value is an object, it's a W3C error (for JSONWP value is a string)
if (_.isPlainObject(responseError) && _.isPlainObject(responseError.value)) {
this.w3c = responseError.value;
this.w3cStatus = httpStatus || HTTPStatusCodes.BAD_REQUEST;
@@ -562,7 +566,6 @@ class ProxyRequestError extends ES6Error {
this.jsonwp = responseError;
}
}
- }
getActualError () {
// If it's MJSONWP error, returns actual error cause for request failure based on `jsonwp.status`

MODIFY
test/protocol/errors-specs.js
test/protocol/errors-specs.js
@@ -385,6 +385,13 @@ describe('.getActualError', function () {
}).getActualError();
isErrorType(actualError, errors.UnknownError).should.be.true;
});
+ it('should parse a JSON string', function () {
+ const actualError = new errors.ProxyRequestError('Error message does not matter', JSON.stringify({
+ value: 'Does not matter',
+ status: -100
+ })).getActualError();
+ isErrorType(actualError, errors.UnknownError).should.be.true;
+ });
});
describe('W3C', function () {
@@ -414,5 +421,14 @@ describe('.getActualError', function () {
}, 456).getActualError();
isErrorType(actualError, errors.UnknownError).should.be.true;
});
+ it('should parse a JSON string', function () {
+ const actualError = new errors.ProxyRequestError('Error message does not matter', JSON.stringify({
+ value: {
+ error: errors.StaleElementReferenceError.error(),
+
+ },
+ }), HTTPStatusCodes.BAD_REQUEST).getActualError();
+ isErrorType(actualError, errors.StaleElementReferenceError).should.be.true;
+ });
});
});
\ No newline at end of file

Parse strings for W3C and JSON (#184)