@iotile/iotile-cloud
Version:
A typescript library for interfacing with the IOTile Cloud API
142 lines • 5.03 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var HttpError = /** @class */ (function () {
function HttpError(resp) {
var _this = this;
this.raw = resp;
var data = resp.response || resp;
this.status = data.status || -1;
this.statusText = data.statusText || 'Unknown Issue';
this.nonFieldErrors = [];
this.fieldErrors = {};
if (data.data && (typeof data.data === "object")) {
var fieldNames = Object.keys(data.data);
fieldNames.forEach(function (name) {
if (name === 'status') {
// Ignore
}
else if (name === 'message' || name === 'detail') {
_this.nonFieldErrors.push(data.data[name]);
}
else if (name === 'non_field_errors') {
var that_1 = _this;
data.data[name].forEach(function (err) {
if (!that_1.nonFieldErrors) {
that_1.nonFieldErrors = [];
}
that_1.nonFieldErrors.push(err);
});
}
else {
var msg_1 = '';
if (data.data[name] instanceof Array) {
data.data[name].forEach(function (err) {
msg_1 += err;
});
}
else {
// It seems like in some cases, data.response shows arrays and in some cases, strings
msg_1 += data.data[name];
}
_this.fieldErrors[name] = msg_1;
}
});
}
else if ((typeof data.data === "string") && data.data !== '') {
this.heuristicallyParseErrorData(data.data);
}
else if (this.status == -1) {
this.nonFieldErrors.push("Could not reach iotile.cloud. Check your internet connection.");
}
if (resp.config) {
if (resp.config.method) {
this.method = resp.config.method.toUpperCase();
}
if (resp.config.url) {
this.url = resp.config.url;
}
}
}
/*
* Do our best to extract a meaningful error message from a string message thrown
* by some deep part of the network stack.
*/
HttpError.prototype.heuristicallyParseErrorData = function (data) {
if (data.indexOf('ENOTFOUND') !== -1) {
this.nonFieldErrors.push("Could not reach iotile.cloud. Check your internet connection.");
}
else {
this.nonFieldErrors.push(data);
}
};
HttpError.prototype._formatFieldErrors = function () {
var _this = this;
var msg = '';
if (this.nonFieldErrors && this.nonFieldErrors.length) {
this.nonFieldErrors.forEach(function (err) {
msg += ' ' + err;
});
}
else {
if (Object.keys(this.fieldErrors).length > 0) {
msg += ' (';
var fieldNames = Object.keys(this.fieldErrors);
fieldNames.forEach(function (name) {
msg += ' ' + name + ': ' + _this.fieldErrors[name];
});
msg += ' )';
}
}
return msg;
};
HttpError.prototype.shortUserErrorMsg = function () {
var msg = 'Error!';
var fieldErrors = this._formatFieldErrors();
if (fieldErrors) {
msg += fieldErrors;
}
else {
// If no field or non-field errors were found, still give generic error
msg += ' ' + this.statusText;
}
return msg;
};
HttpError.prototype.longUserErrorMsg = function () {
var msg = 'Error ' + this.status + '! ';
msg += this.statusText;
var fieldErrors = this._formatFieldErrors();
if (fieldErrors) {
msg += ':' + fieldErrors;
}
return msg;
};
HttpError.prototype.extraInfo = function () {
var msg = this.longUserErrorMsg();
if (this.method && this.url) {
msg += ' -> ' + this.method + ':' + this.url;
}
else {
if (this.status === -1) {
msg += ' -> ' + JSON.stringify(this.raw);
}
}
return msg;
};
Object.defineProperty(HttpError.prototype, "message", {
get: function () {
return this.shortUserErrorMsg();
},
enumerable: true,
configurable: true
});
Object.defineProperty(HttpError.prototype, "userMessage", {
get: function () {
return this.shortUserErrorMsg();
},
enumerable: true,
configurable: true
});
return HttpError;
}());
exports.HttpError = HttpError;
//# sourceMappingURL=http-error.js.map