Common utilities for error handling within Backstage with structured error classes and serialization functions
63
Wrap a fetch call so successful responses yield parsed JSON, while failed responses throw the dependency's standardized response error built directly from the original response. The thrown error must expose parsed error bodies and HTTP metadata so callers can inspect status, status text, and the server-provided error.
{"ok":true} resolves to that parsed object and does not produce a response error object @testcontent-type: application/json and body {"error":{"name":"NotFoundError","message":"User missing"},"response":{"statusCode":404},"request":{"method":"GET","url":"/users/123"}}, the helper throws the dependency's response error type carrying statusCode: 404, statusText: "Not Found", and a body.error matching the remote name and message @testcause, enabling downstream checks of cause.name and cause.message @testcontent-type: text/plain and body Service unavailable, the helper throws a response error whose statusCode is 503, statusText is "Service Unavailable", and whose parsed body.error.message mentions the raw body text for context @test@generates
export type FetchLike = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
export type FetchClientOptions = {
/** Custom fetch implementation, defaults to global fetch */
fetch?: FetchLike;
/** Optional base URL prepended when path is relative */
baseUrl?: string;
};
/**
* Performs an HTTP request and returns parsed JSON data for successful responses.
* Throws a structured response error built from the dependency when the response is not ok.
*/
export async function fetchJsonWithResponseError(
path: string,
init?: RequestInit,
options?: FetchClientOptions
): Promise<unknown>;Use the package's response error helper to convert non-ok fetch responses into structured errors that expose HTTP metadata, parsed bodies, and a deserialized cause. @satisfied-by
Install with Tessl CLI
npx tessl i tessl/npm-backstage--errorsevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10