UNPKG

@toruslabs/customauth

Version:

CustomAuth login with torus to get user private key

29 lines (27 loc) 1.12 kB
const serializeError = async error => { // Find first Error or create an "unknown" Error to keep stack trace. const isError = error instanceof Error; const isString = typeof error === "string"; const isApiErrorIndex = error && typeof error === "object" && "status" in error && "type" in error; let err; if (isApiErrorIndex) { const apiError = error; const contentType = apiError.headers.get("content-type"); if (contentType.includes("application/json")) { const errJson = await apiError.json(); err = new Error((errJson === null || errJson === void 0 ? void 0 : errJson.error) || (errJson === null || errJson === void 0 ? void 0 : errJson.message) || JSON.stringify(errJson)); } else if (contentType.includes("text/plain")) { err = new Error(await apiError.text()); } else { err = new Error(`${apiError.status} ${apiError.type.toString()} ${apiError.statusText}`); } } else if (isString) { err = new Error(error); } else if (isError) { err = error; } else { err = new Error("Unknown error"); } return err; }; export { serializeError };