@httpx/exception
Ecosystem

Ecosystem

Serialization

Tupleson

Example with tupleson (opens in a new tab) experimental serializer.

import { createTson, type TsonType } from "tupleson";
import { createHttpException, HttpException, HttpUnprocessableEntity } from "@httpx/exception";
import { fromJson, toJson, type SerializerError } from "@httpx/exception/serializer";
 
const exceptionSerializer: TsonType<HttpException | SerializerError, string> = {
  deserialize: (v) => fromJson(v),
  key: "HttpException",
  serialize: (v) => toJson(v),
  test: (v) => v instanceof HttpException,
};
 
const tson = createTson({
  types: [exceptionSerializer],
});
 
const obj = {
  e422: new HttpUnprocessableEntity({
    issues: [
      {
        message: "Invalid address",
        path: ["addresses", 0, "line1"],
        code: "empty_string",
      },
    ],
  }),
  e404: createHttpException(404),
};
 
const serialized = tson.serialize(obj);
const deserialized = tson.deserialize(serialized);
expect(deserialized).toStrictEqual(obj);