type ApiError = {
error: {
code: string;
message: string;
details?: unknown;
request_id: string;
};
};
const res = await fetch(url, {
headers: { Authorization: `Bearer ${KEY}` },
});
if (!res.ok) {
const body = (await res.json()) as ApiError;
switch (body.error.code) {
case "rate_limited": {
const retryAfter = Number(res.headers.get("Retry-After") ?? "1");
await new Promise((r) => setTimeout(r, retryAfter * 1000));
// retry
break;
}
case "scope_required":
// tell the operator to widen the key's scopes
break;
case "validation_failed":
// surface body.error.details to the user
break;
case "unsupported_key_version":
// you passed a v1 key - mint a v2 one
break;
default:
console.error(
"Productlane error",
body.error.code,
body.error.request_id,
);
}
}