maloguertin / msw-trpc

tPRC support for MSW
208 stars 21 forks source link

Issues throwing errors in 2.0.0-beta.0 #33

Closed danilofuchs closed 6 months ago

danilofuchs commented 11 months ago

Describe the bug In version 1.x, to return an error, the experience was not ideal:

const notFound = {
  error: {
    json: {
      message: "NOT_FOUND",
      code: -32004,
      data: {
        code: "NOT_FOUND",
        httpStatus: 404,
        stack: "",
        path: "",
        zodError: null,
      },
    },
    meta: [],
  },
};

server.use(
  api.shipment.getAddress.query((req, res, ctx) => {
    return res(ctx.status(404), ctx.json(notFound));
  })
)

From what I understand, 2.x improves the experience:

server.use(
  api.shipment.getAddress.query(() => {
      throw new TRPCError({
        code: "NOT_FOUND",
      });
  })
)

However, the error on the client side does not match the reality, and I couldn't find an escape hatch:

msw-trpc 2.0.0-beta.0:

{
  "meta": {
    "response": {}
  },
  "name": "TRPCClientError"
}

trpc 10.44.1:

{
  "meta": {
    "response": {},
    "responseJSON": [
      {
        "error": {
          "json": {
            "message": "NOT_FOUND",
            "code": -32004,
            "data": {
              "code": "NOT_FOUND",
              "httpStatus": 404,
              "stack": "TRPCError: NOT_FOUND ...",
              "path": "shipment.getAddress",
              "apiError": null,
              "zodError": null
            }
          }
        }
      }
    ]
  },
  "shape": {
    "message": "NOT_FOUND",
    "code": -32004,
    "data": {
      "code": "NOT_FOUND",
      "httpStatus": 404,
      "stack": "TRPCError: NOT_FOUND ...",
      "path": "shipment.getAddress",
      "apiError": null,
      "zodError": null
    }
  },
  "data": {
    "code": "NOT_FOUND",
    "httpStatus": 404,
    "stack": "TRPCError: NOT_FOUND ...",
    "path": "shipment.getAddress",
    "apiError": null,
    "zodError": null
  },
  "name": "TRPCClientError"
}

To Reproduce Steps to reproduce the behavior:

  1. Create a trpc handler
  2. Setup msw-trpc and throw an NOT_FOUND TRPCError
  3. On the client, JSON.stringify the error

Expected behavior A clear and concise description of what you expected to happen.

Versions typescript: v5.2.2 tRPC: v10.44.1 msw: v2.0.11

danilofuchs commented 11 months ago

@maloguertin I would like to work on this issue.