import tryo, { errorRule } from "tryo";
class HttpError extends Error {
constructor(
public status: number,
public url: string,
public body?: unknown
) {
super("HTTP error");
}
}
type HttpAppError = {
code: "HTTP";
message: string;
status?: number;
meta?: { url: string; body?: unknown };
cause?: unknown;
};
const runner = tryo({
rules: [
errorRule.instance(HttpError).toError<HttpAppError>((e) => ({
code: "HTTP",
message: `Request failed (${e.status})`,
status: e.status,
meta: { url: e.url, body: e.body },
cause: e,
})),
],
});