@superlayer/core
Version:
Superlayer's core local abstraction
32 lines • 1.22 kB
JavaScript
import { SuperlayerError } from "../SuperlayerError.js";
export class SuperlayerAPIError extends SuperlayerError {
body;
status;
constructor(error) {
// Create a descriptive message based on the error
const message = error.body?.message || `API Error (${error.status})`;
super(message, error.body.hint, error.body?.traceId || error.body['trace-id']);
const actualProto = new.target.prototype;
if (Object.setPrototypeOf) {
Object.setPrototypeOf(this, actualProto);
}
// Maintain proper stack trace for where our error was thrown
if (Error.captureStackTrace) {
Error.captureStackTrace(this, SuperlayerAPIError);
}
this.name = 'SuperlayerAPIError';
this.status = error.status;
this.body = error.body;
}
get [Symbol.toStringTag]() {
return 'SuperlayerAPIError';
}
}
export async function jsonFetch(input, init) {
const res = await fetch(input, init);
const json = await res.json();
return res.status === 200
? Promise.resolve(json)
: Promise.reject(new SuperlayerAPIError({ status: res.status, body: json }));
}
//# sourceMappingURL=fetch.js.map