Axios, ExpressMiddleware and TRPCErrors

We have an express based TRPC server and our procedures are calling our endpoints using axios. So when our users credentials provided expired or are incorrect Axios will return with a 401 as it should. I need to get this UNAUTHORIZED 401 error to the frontend but no matter what I try TRPC throws is as a 500 INTERNAL_SERVER_ERROR. I tried error formatter and on error and I hoped to get the httpStatus code from the axios error somehow but I only get access to the message. Can someone here be so kind and point me in the right direction to forward the errors from our Axios Instances as such to the React TRPC client. πŸ™
T
test_1β€’303d ago
did u try something like throw TRPCError({ code: "UNAUTHORIZED"
W
wleistraβ€’302d ago
The issue I'm having is that I cannot access the error response from axios in OnError or ErrorFormatter to base any robust logic to throw the trpcError with unauthorized code. What I would like to avoid is rely on the error message and do some kind of if string of message includes 401... So I was hoping there would be another way or some way to work with the error (httpStatus) from Axios that TRPC receives. I found a solution. The cause key on the error object was my Axios error, and after an isAxiosError check, it can be cast as such, and there I get the actual response from my Axios query. So now I could use the errorFormatter on context creation to return those codes to the client instead of the previous 500/INTERNAL_SERVER_ERROR generic error seen there.
More Posts
base pathHi, don’t seem to be able to find documentation but is there a way to set the base path of the serveAny Benefit using tRPC for only remote api calls?Hi, does it make sense to use trpc if all I will be doing is calling a remote api? I have set it up Can I connect to a regular node express with socket.io?My idea is that I will host the socketio nodejs server in some server, and then use it with my nextjhow does one actually make trpc/client work with the app routerive been using trpc on the pages router for a while now and ive started putting together a next13 stwhen i deploy frontend trpc fails to compileim trying to deploy my create-react-app with tRPC to Vercel it works perfectly fine on localhosthow to rate limit in trpc express/nodejs server (not nextjs)each prodecure will have different rate limit for IP or sometthing idk maybe whole database will havDisable fetch on load```tsx import React, { useState } from "react"; import { api } from "../utils/api"; const Test = ()is it possible to record the requests on the server side ?I wanted to play them back then on the browser side . I wanted to do server side rendering in reactStreaming responses (eg. for streaming ai chat completion text)Hello! Has anyone used Vercel's `ai` package or any similar libraries which stream their responses wHandling errors on the front-endI'm making a mutation from my front-end and I intentionally throw a new TRPCError on my backend, I cTypeScript Alias Imports don't get resolved ont the ClientI have a Monorepo with a multiple `/packages/*` packages, each has its own `tsconfig.json`. I noticmerging other routers to appRouterHey everyone, I am using the https://icflorescu.github.io/trpc-sveltekit package in my SvelteKit pro