Common utilities for error handling within Backstage with structured error classes and serialization functions
63
Build a tiny utility that turns already-consumed failed HTTP responses into structured error payloads and rebuilds errors from those payloads. The functions should lean on the dependency's typed error payload and consumed-response helpers instead of ad-hoc shapes.
error, request, and response.statusCode, produce a structured payload that preserves the parsed error fields and uses the response status for response.statusCode. @testerror.name defaults to Error, error.message equals the body text (or the status text if the body is empty), and response.statusCode matches the HTTP status. @testError instance that uses the payload's error.name and error.message, and includes the response status code somewhere in the message; throw if the payload is missing error. @test@generates
export type StructuredErrorPayload = {
error: {
name: string;
message: string;
stack?: string;
[key: string]: unknown;
};
request?: {
method: string;
url: string;
};
response: {
statusCode: number;
};
};
export type FailedResponseInput = {
response: {
ok: boolean;
status: number;
statusText: string;
url: string;
headers: {
get(name: string): string | null;
};
};
bodyText?: string;
};
export async function buildStructuredPayload(
input: FailedResponseInput
): Promise<StructuredErrorPayload>;
export function rebuildError(payload: StructuredErrorPayload): Error;Provides typed error payload and consumed response helpers for parsing failed HTTP responses.
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