@logsn/arweave
Version:
Arweave JS client library
45 lines (44 loc) • 1.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getError = void 0;
class ArweaveError extends Error {
type;
response;
constructor(type, optional = {}) {
if (optional.message) {
super(optional.message);
}
else {
super();
}
this.type = type;
this.response = optional.response;
}
getType() {
return this.type;
}
}
exports.default = ArweaveError;
// Safely get error string
// from a response, falling back to
// resp.data, statusText or 'unknown'.
// Note: a wrongly set content-type can
// cause what is a json response to be interepted
// as a string or Buffer, so we handle that too.
function getError(resp) {
let data = resp.data;
if (typeof resp.data === "string") {
try {
data = JSON.parse(resp.data);
}
catch (e) { }
}
if (resp.data instanceof ArrayBuffer || resp.data instanceof Uint8Array) {
try {
data = JSON.parse(data.toString());
}
catch (e) { }
}
return data ? data.error || data : resp.statusText || "unknown";
}
exports.getError = getError;