Coder Social home page Coder Social logo

onError about trpc-on-the-app-router HOT 3 CLOSED

colinricardo avatar colinricardo commented on August 14, 2024 1
onError

from trpc-on-the-app-router.

Comments (3)

jherr avatar jherr commented on August 14, 2024

That's not issue with the code in this repo. This is a generic question about how to use tRPC. My recommendation would be to either ask a question on the feedback channels provided by tRPC, or ask in the #react or #typescript channels of the Blue Collar Coder Discord server.

from trpc-on-the-app-router.

colinricardo avatar colinricardo commented on August 14, 2024

hmm

its not really a generic question

let me rephrase:

if you were to use the approach specifically endorsed in this repo

how would you deal with handling TRPC errors globally on the client side

given that with this approach you cant use onError from the createTRPCNext adapter? (since we are using createTRPCReact)

thanks for the recommendation -- i have asked a more generic form of the question here: trpc/trpc#4782

from trpc-on-the-app-router.

colinricardo avatar colinricardo commented on August 14, 2024

for when someone else has this issue:

this approach works well enough:

"use client"

import React, { useState } from "react"
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
import { httpBatchLink } from "@trpc/client"
import { toast } from "sonner"

import CustomToast from "@/components/common/CustomToast"

import { trpc } from "./trpc-client"

interface IError {
  code: string
  message: string
}

export default ({ children }: { children: React.ReactNode }) => {
  const [queryClient] = useState(
    () =>
      new QueryClient({
        defaultOptions: {
          mutations: {
            onError: (error) => {
              const { code, message } = error as IError

              if (code === "BAD_REQUEST") {
                toast.custom((t) => (
                  <CustomToast
                    variant="error"
                    message={message}
                    key={t}
                    t={t}
                  />
                ))
              } else {
                toast.custom((t) => (
                  <CustomToast
                    variant="error"
                    message="Something went wrong."
                    key={t}
                    t={t}
                  />
                ))
              }
            },
          },
          queries: {
            onError: (error) => {
              const { code, message } = error as IError

              if (code === "BAD_REQUEST") {
                toast.custom((t) => (
                  <CustomToast
                    variant="error"
                    message={message}
                    key={t}
                    t={t}
                  />
                ))
              } else {
                toast.custom((t) => (
                  <CustomToast
                    variant="error"
                    message="Something went wrong."
                    key={t}
                    t={t}
                  />
                ))
              }
            },
          },
        },
      })
  )
  const [trpcClient] = useState(() =>
    trpc.createClient({
      links: [
        httpBatchLink({
          url: "http://localhost:3000/api/trpc",
        }),
      ],
    })
  )
  return (
    <trpc.Provider client={trpcClient} queryClient={queryClient}>
      <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
    </trpc.Provider>
  )
}

from trpc-on-the-app-router.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.