@fullstory/server-api-client
Version:
The official FullStory server API client SDK for NodeJS.
72 lines • 2.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FSApiError = void 0;
const base_1 = require("./base");
/*
* FSApiError represents when server API returns a non-2xx code.
*/
class FSApiError extends base_1.FSBaseError {
constructor(errMsg, httpStatusCode, headers, rawResponse, cause) {
const name = httpStatusCode === 429 ? base_1.FSErrorName.ERROR_RATE_LIMITED : base_1.FSErrorName.ERROR_FULLSTORY;
super(name, errMsg, cause);
this.httpStatusCode = httpStatusCode;
this.headers = headers;
const maybeRspObj = this.maybeParseObject(rawResponse);
if (maybeRspObj) {
const fsTypedResponse = this.getErrorResponse(maybeRspObj);
this.fsErrorPayload = fsTypedResponse;
this.message += ` Error message: ${fsTypedResponse === null || fsTypedResponse === void 0 ? void 0 : fsTypedResponse.message}`;
}
// couldn't parse the response, just pass on the string value
if (!this.fsErrorPayload) {
this.fsErrorPayload = rawResponse;
}
// add any additional properties from the response object
this.addAdditionalProperties(maybeRspObj);
}
maybeParseObject(rspStr) {
if (!rspStr)
return;
try {
return JSON.parse(rspStr);
}
catch (e) {
// throw away any error here and just return undefined
return;
}
}
getErrorResponse(obj) {
// ErrorResponse requires both code and message fields to present
if ((obj === null || obj === void 0 ? void 0 : obj.code) && (obj === null || obj === void 0 ? void 0 : obj.message)) {
return {
code: obj.code,
message: obj.message,
};
}
return;
}
canRetry() {
var _a;
if (this.name === base_1.FSErrorName.ERROR_RATE_LIMITED) {
return true;
}
if ((_a = this.headers) === null || _a === void 0 ? void 0 : _a['retry-after']) {
return true;
}
return false;
}
getRetryAfter() {
var _a;
const ra = (_a = this.headers) === null || _a === void 0 ? void 0 : _a['retry-after'];
if (!ra) {
return 0;
}
const retryAfter = Number.parseInt(ra);
if (isNaN(retryAfter)) {
return 0;
}
return retryAfter * 1000; // in ms
}
}
exports.FSApiError = FSApiError;
//# sourceMappingURL=api.js.map