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

const INTERNAL_ERROR = 500;
/** These embed internal storage names and upstream text; other HttpErrors carry deliberate, user-facing messages. */
const MASKED_NAMES = new Set(["ArtifactsUnavailableError"]);

function masked(error: HttpError): boolean {
  return error.status === INTERNAL_ERROR || MASKED_NAMES.has(error.name);
}

/** 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: masked(error) ? "Something went wrong on our side; try again shortly" : error.message,
  }),
  fromSerializable: ({ status, message }) => new HttpError(status, message),
});