moloch
moloch
Explore posts from servers
TtRPC
Created by moloch on 10/4/2023 in #❓-help
Missing keys in TRPCError when returning as JSON
Hey all! I am returning a TRPCError from a custom TRPC server but only the code and name keys seems to be serialized and returned to the client. It's quite strange. If I directly return a key such as error.message I get the string no problem, but if I return the entire error object I only recieve error.code and error.name . Any idea why this is? Here's a little code snippet:
catch (error) {
if (error instanceof TRPCError) {
console.log(Object.keys(error)) // [ 'code', 'name' ]
console.log(error.cause) // The actual cause of the error
console.log(error.message) // The error message!!!
// An error from tRPC occured
const httpCode = getHTTPStatusCodeFromError(error);
return res.status(httpCode).json({ ...error });
}
// Another error occured
console.error(error);
res.status(500).json({ message: "Internal server error" });
}
catch (error) {
if (error instanceof TRPCError) {
console.log(Object.keys(error)) // [ 'code', 'name' ]
console.log(error.cause) // The actual cause of the error
console.log(error.message) // The error message!!!
// An error from tRPC occured
const httpCode = getHTTPStatusCodeFromError(error);
return res.status(httpCode).json({ ...error });
}
// Another error occured
console.error(error);
res.status(500).json({ message: "Internal server error" });
}
1 replies
TtRPC
Created by moloch on 3/30/2023 in #❓-help
Is there a way to define the Websocket protocol when using wsLink()
I am attempting to use tRPC with Azure's Pub/Sub Websockets service it appears to require custom protocols to be defined as explained here: https://learn.microsoft.com/en-us/azure/azure-web-pubsub/reference-json-webpubsub-subprotocol They give the following code example:
var pubsub = new WebSocket('wss://test.webpubsub.azure.com/client/hubs/hub1', 'json.webpubsub.azure.v1');
var pubsub = new WebSocket('wss://test.webpubsub.azure.com/client/hubs/hub1', 'json.webpubsub.azure.v1');
How might this be done using:
const client = createWSClient({
url: `wss://test.webpubsub.azure.com/client/hubs/hub1`,
});
const client = createWSClient({
url: `wss://test.webpubsub.azure.com/client/hubs/hub1`,
});
1 replies
TtRPC
Created by moloch on 3/9/2023 in #❓-help
Cannot get subscription event to fire
Ripping my hair out here trying to get Websockets working with tRPC and Next. Everything seems to be working and the frontend client is connecting to my websocket server, however triggering the addFact mutation does not not successfully emit the new catFact despite there not being any errors. I've looked at various repos that achieve similar functionality and haven't been able to spot the problem, does anyone have any guidance?
export const catFactsRouter = createTRPCRouter({
fact: publicProcedure.query(({ ctx }) => {

const fact = ctx.prisma.catFact.findFirst({
orderBy: {
createdAt: "desc",
},
});

return fact;
}),
addFact: protectedProcedure
.input(
z.object({
token: z.string().min(1),
fact: z.string().min(1)
})
)
.mutation(async ({ input, ctx }) => {
console.log(`protectedProcedure.mutation()`);
const fact = await ctx.prisma.catFact.create({
data: {
fact: input.fact,
}
});
console.log(`ctx.ee.emit(Events.NEW_CAT_FACT, fact)`);
ctx.ee.emit(Events.NEW_CAT_FACT, fact);
console.log(`ee.emit(Events.NEW_CAT_FACT, fact);`);
return fact;
}),
onFact: publicProcedure.subscription(({ ctx }) => {
console.log("publicProcedure.subscription")

// `resolve()` is triggered for each client when they start subscribing `onFact`
// return an `observable` with a callback which is triggered immediately
return observable<CatFact>((emit) => {
const onCatFact = (data: CatFact) => {
console.log(`Emitting fact to client:`, data);
// emit data to client
emit.next(data);
};
console.log("ctx.ee.on(Events.NEW_CAT_FACT, onCatFact);")

ctx.ee.on(Events.NEW_CAT_FACT, onCatFact);

return () => {
ctx.ee.off(Events.NEW_CAT_FACT, onCatFact);
};
});
}),
});
export const catFactsRouter = createTRPCRouter({
fact: publicProcedure.query(({ ctx }) => {

const fact = ctx.prisma.catFact.findFirst({
orderBy: {
createdAt: "desc",
},
});

return fact;
}),
addFact: protectedProcedure
.input(
z.object({
token: z.string().min(1),
fact: z.string().min(1)
})
)
.mutation(async ({ input, ctx }) => {
console.log(`protectedProcedure.mutation()`);
const fact = await ctx.prisma.catFact.create({
data: {
fact: input.fact,
}
});
console.log(`ctx.ee.emit(Events.NEW_CAT_FACT, fact)`);
ctx.ee.emit(Events.NEW_CAT_FACT, fact);
console.log(`ee.emit(Events.NEW_CAT_FACT, fact);`);
return fact;
}),
onFact: publicProcedure.subscription(({ ctx }) => {
console.log("publicProcedure.subscription")

// `resolve()` is triggered for each client when they start subscribing `onFact`
// return an `observable` with a callback which is triggered immediately
return observable<CatFact>((emit) => {
const onCatFact = (data: CatFact) => {
console.log(`Emitting fact to client:`, data);
// emit data to client
emit.next(data);
};
console.log("ctx.ee.on(Events.NEW_CAT_FACT, onCatFact);")

ctx.ee.on(Events.NEW_CAT_FACT, onCatFact);

return () => {
ctx.ee.off(Events.NEW_CAT_FACT, onCatFact);
};
});
}),
});
8 replies