cabbie-sync
Version:
A synchronous webdriver client
110 lines (82 loc) • 3.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _httpResponse = require('../flow-types/http-response');
var _webdriverResponse = require('../flow-types/webdriver-response');
var _errors = require('./errors');
var _flowRuntime = require('flow-runtime');
var _flowRuntime2 = _interopRequireDefault(_flowRuntime);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
*
* This file is generated automatically, run npm run build to re-generate.
**/
var WebdriverResponse = _flowRuntime2.default.tdz(function () {
return _webdriverResponse.WebdriverResponse;
});
/*
* Parse a webdriver response, throwing errors if the status suggests it
*/
var HttpResponse = _flowRuntime2.default.tdz(function () {
return _httpResponse.HttpResponse;
});
function parseResponse(res /*: HttpResponse*/) /*: WebdriverResponse*/ {
var _resType = _flowRuntime2.default.ref(HttpResponse);
var _returnType = _flowRuntime2.default.return(_flowRuntime2.default.ref(WebdriverResponse));
_flowRuntime2.default.param('res', _resType).assert(res);
var bodyString = res.body.toString('utf8');
if (res.statusCode >= 0 && res.statusCode < 100) {
throw new Error('Server responded with status code (' + res.statusCode + '):\n' + bodyString);
} else if (res.statusCode >= 400 && res.statusCode < 500) {
// 400s
throw new Error('Invalid request (' + res.statusCode + '):\n' + bodyString);
} else if (res.statusCode >= 500 && res.statusCode < 600) {
// 500s
var body = void 0;
try {
body = JSON.parse(bodyString);
} catch (ex) {
throw new Error('Failed command (' + res.statusCode + '):\n' + bodyString);
}
throw new Error('Failed command (' + res.statusCode + '):\n' + body.value.message + (body.value.class ? '\nClass: ' + body.value.class : '') + (body.value.stackTrace ? '\nStack-trace:\n ' + stringifyStackTrace(body.value.stackTrace) : ''));
} else if (res.statusCode >= 200 && res.statusCode < 300) {
if (res.statusCode === 204) {
return _returnType.assert(null);
} else {
var _body = void 0;
try {
_body = JSON.parse(bodyString);
} catch (ex) {
throw new Error('Failed parsing JSON response (with status code ' + res.statusCode + '):\n' + bodyString);
}
if (_body.status === 0) {
return _returnType.assert(_body.value);
} else {
throw (0, _errors.fromBody)(_body);
}
}
} else {
throw new Error('Unknown status code (' + res.statusCode + '):\n' + bodyString);
}
}
/*
* Turns a selenium stack-trace into a string
*/
_flowRuntime2.default.annotate(parseResponse, _flowRuntime2.default.function(_flowRuntime2.default.param('res', _flowRuntime2.default.ref(HttpResponse)), _flowRuntime2.default.return(_flowRuntime2.default.ref(WebdriverResponse))));
function stringifyStackTrace(stackTrace /*: Array<any>*/) /*: string*/ {
var _stackTraceType = _flowRuntime2.default.array(_flowRuntime2.default.any());
var _returnType2 = _flowRuntime2.default.return(_flowRuntime2.default.string());
_flowRuntime2.default.param('stackTrace', _stackTraceType).assert(stackTrace);
var i,
len,
result = [];
for (i = 0, len = stackTrace.length; i < len; i++) {
if (stackTrace[i]) {
result.push(stackTrace[i].methodName + '::' + stackTrace[i].className + ' (' + stackTrace[i].fileName + ':' + stackTrace[i].lineNumber + ')');
}
}
return _returnType2.assert(result.join('\n'));
}
_flowRuntime2.default.annotate(stringifyStackTrace, _flowRuntime2.default.function(_flowRuntime2.default.param('stackTrace', _flowRuntime2.default.array(_flowRuntime2.default.any())), _flowRuntime2.default.return(_flowRuntime2.default.string())));
exports.default = parseResponse;