cal
cal
Explore posts from servers
TTCTheo's Typesafe Cult
Created by cal on 7/9/2023 in #questions
Handle custom error class in error UI boundary
Hey guys, I am throwing a custom error
throw new GQLError(workout.message, workout.__typename);
throw new GQLError(workout.message, workout.__typename);
in a subpage. I then try to handle it using a error UI boundary. Everything works as expected but
const isGQLError = error instanceof GQLError;
const isGQLError = error instanceof GQLError;
inside of error.tsx is always false. I assume nextjs internally handles/wraps the error. Is there a way to get a hold of the thrown error directly in order to display different UI in case of an GQLError? Thank you in advance
2 replies
TTCTheo's Typesafe Cult
Created by cal on 6/28/2023 in #questions
Where would I initialize a 3rd party API to use in TRPC routes?
I am currently initializing this client in server/client.ts similar to the global definition of prisma. I then import client in my TRPC router. Does this make sense or would it be smarter to do that somewhere else.
interface Client {
$ad: typeof api.$ad;
$ads: api.Ads;
$geo: api.Geo;
}

const globalForClient = globalThis as unknown as {
immoledo: Client | undefined;
abortController: AbortController | undefined;
};

const initClient = async () => {
if (globalForClient.abortController) {
globalForClient.abortController.abort();
}

globalForClient.abortController = new AbortController();
const { signal } = globalForClient.abortController;

api.init(env.API_URL);

api.$global.net.errors.onUnauthorized = () =>
console.warn("Client unauthorized");

try {
const success = await api.$anonymousUser.profile.login(
env.API_CLIENT_ID,
env.API_CLIENT_SECRET,
true,
signal
);
console.log(`🏠 Client login ${success ? "✅" : "❌"}`);
} catch (error) {
console.error("🏚️ Client login error:\n", error);
}

return api;
};

export const client = await initClient();
interface Client {
$ad: typeof api.$ad;
$ads: api.Ads;
$geo: api.Geo;
}

const globalForClient = globalThis as unknown as {
immoledo: Client | undefined;
abortController: AbortController | undefined;
};

const initClient = async () => {
if (globalForClient.abortController) {
globalForClient.abortController.abort();
}

globalForClient.abortController = new AbortController();
const { signal } = globalForClient.abortController;

api.init(env.API_URL);

api.$global.net.errors.onUnauthorized = () =>
console.warn("Client unauthorized");

try {
const success = await api.$anonymousUser.profile.login(
env.API_CLIENT_ID,
env.API_CLIENT_SECRET,
true,
signal
);
console.log(`🏠 Client login ${success ? "✅" : "❌"}`);
} catch (error) {
console.error("🏚️ Client login error:\n", error);
}

return api;
};

export const client = await initClient();
Best reards and thank you in advance 🙂
1 replies