17 lines · 781 B
import { createSerializationAdapter } from "@tanstack/react-router";
import { HttpError } from "~/server/errors";

/** 5xx messages can name internal storage (Artifacts repo names, upstream text), so clients get a generic one. */
const SERVER_ERROR = 500;

/** Keeps `HttpError.status` across the server → client boundary so error pages can tell 403 from 500. */
export const httpErrorAdapter = createSerializationAdapter({
  key: "gitfor-http-error",
  test: (value): value is HttpError => value instanceof HttpError,
  toSerializable: (error) => ({
    status: error.status,
    message: error.status >= SERVER_ERROR ? "Something went wrong on our side; try again shortly" : error.message,
  }),
  fromSerializable: ({ status, message }) => new HttpError(status, message),
});