@clickup/rest-client
Version:
A syntax sugar tool around Node fetch() API, tailored to work with TypeScript and response validators
32 lines • 1.84 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const inspectPossibleJSON_1 = __importDefault(require("../internal/inspectPossibleJSON"));
const prependNewlineIfMultiline_1 = __importDefault(require("../internal/prependNewlineIfMultiline"));
const RestError_1 = __importDefault(require("./RestError"));
const RESPONSE_MAX_LEN_IN_ERROR = process.env["NODE_ENV"] === "development" ? Number.MAX_SAFE_INTEGER : 2048;
class RestResponseError extends RestError_1.default {
constructor(message, res) {
super(`HTTP ${res.status}: ` +
(message ? `${message}: ` : "") +
(0, prependNewlineIfMultiline_1.default)((0, inspectPossibleJSON_1.default)(res.headers, res.text, RESPONSE_MAX_LEN_IN_ERROR)));
Object.defineProperty(this, "res", { value: res, enumerable: false }); // hidden from inspect()
const url = new URL(res.req.url);
const search = (url.search || "").replace(/^\?/, "");
this.method = res.req.method;
this.host = res.req.headers.get("host") || url.host;
this.pathname = url.pathname;
this.requestArgs = search.replace(/(?<=[&])/g, "\n");
this.requestBody = (0, inspectPossibleJSON_1.default)(res.headers, res.req.body, RESPONSE_MAX_LEN_IN_ERROR);
this.responseHeaders = [...res.headers]
.map(([name, value]) => `${name}: ${value}`)
.join("\n");
// Don't expose query string parameters in the message as they may contain
// sensitive information.
this.message += `\n ${this.method} ${url.protocol}//${url.host}${url.pathname}`;
}
}
exports.default = RestResponseError;
//# sourceMappingURL=RestResponseError.js.map
;