@opendatalabs/vana-sdk
Version:
A TypeScript library for interacting with Vana Network smart contracts.
66 lines • 1.52 kB
JavaScript
class PSError extends Error {
constructor(code, message) {
super(message);
this.code = code;
this.name = "PSError";
}
code;
}
const KNOWN_CODES = /* @__PURE__ */ new Set([
"missing_auth",
"invalid_signature",
"unregistered_builder",
"not_owner",
"expired_token",
"grant_invalid",
"grant_required",
"grant_expired",
"grant_revoked",
"scope_mismatch",
"fee_required",
"ps_unavailable",
"server_not_configured",
"content_too_large"
]);
function isRecord(value) {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
function normalizeCode(value) {
if (typeof value !== "string") {
return null;
}
const code = value.toLowerCase();
return KNOWN_CODES.has(code) ? code : null;
}
function extractPSErrorBody(body) {
if (!isRecord(body)) {
return null;
}
const nested = isRecord(body.error) ? body.error : null;
const code = normalizeCode(
nested?.errorCode ?? nested?.code ?? body.errorCode ?? body.code
);
const message = nested?.message ?? body.message;
if (!code || typeof message !== "string") {
return null;
}
return { code, message };
}
async function parsePSError(response) {
if (response.ok) {
return null;
}
let body;
try {
body = await response.json();
} catch {
return null;
}
const errorBody = extractPSErrorBody(body);
return errorBody ? new PSError(errorBody.code, errorBody.message) : null;
}
export {
PSError,
parsePSError
};
//# sourceMappingURL=ps-errors.js.map