Skip to content

Improve handling of HTTP statuses & headers - #811

Merged
KATT merged 48 commits into
mainfrom
feature/error-example
Aug 24, 2021
Merged

KATT merged 48 commits into
mainfrom
feature/error-example

Conversation

@KATT

@KATT KATT commented Aug 20, 2021 •

Copy link
Copy Markdown
Member

This PR enables Next.js to send actual 404 status codes for instance when doing SSR.

  • Exposes httpStatus as part of error object as the default error shape
  • httpStatus will be used to determine the actual HTTP Status code set
  • Expose TRPCClientError.data as a shortcut to error.shape.data as that's the only one I find myself using
  • Stricter ErrorFormatter - data has to be a Record<string, unknown>
  • TRPCError we infer in @trpc/react is now TRPCErrorLike and is serialized for SSR in @trpc/next
  • add new method responseMeta() that is triggered before a request ends - both as part of @trpc/next & @trpc/server
    • on @trpc/server receives type, paths, errors, and the unserialized data[] that is going out
    • on @trpc/next it client errors
  • refactored some internals and removed some unnecessary exports on adapters - minor breaking changes if someone relied on something they shouldn't have :)

See docs at πŸ‘‰ https://www-git-feature-error-example.tmp.trpc.io/


Next.js error example

For a reference on how to use this you can do a component like the following:

In withTRPC()

export default withTRPC<AppRouter>({
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  config() { /* ... */ }
  /**
   * @link https://trpc.io/docs/ssr
   */
  ssr: true,
  /**
   * Set headers or status code when doing SSR
   */
  responseMeta({ clientErrors }) {
    if (clientErrors.length) {
      // grab the first error's httpStatus
      return {
        status: clientErrors[0].data?.httpStatus ?? 500,
      };
    }

    return {};
  },
})(MyApp);

In page

import { useRouter } from 'next/dist/client/router';
import { trpc } from 'utils/trpc';
import NextError from 'next/error';

export default function PostViewPage() {
  const id = useRouter().query.id as string;
  const postQuery = trpc.useQuery(['post.byId', id]);
  if (postQuery.error) {
    const statusCode = postQuery.error.data?.httpStatus ?? 500;
    return (
      <NextError title={postQuery.error.message} statusCode={statusCode} />
    );
  }
  if (postQuery.status === 'loading') {
    return <>Loading...</>;
  }
  return (
    <>
      <h1>{postQuery.data?.title}</h1>

      <h2>Raw data:</h2>
      <pre>{JSON.stringify(postQuery.data ?? null, null, 4)}</pre>
    </>
  );
}

@vercel

vercel Bot commented Aug 20, 2021 •

Copy link
Copy Markdown

@KATT KATT changed the title Improve handling of HTTP status & headers Improve handling of HTTP statuses & headers Aug 23, 2021

@cyrus-za cyrus-za left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. Can't wait to use it

This branch was successfully deployed

2 active deployments
Preview – www β€” 2e65f4ef Deployed Aug 24, 2021 by vercel[bot]
Preview – todomvc β€” 2e65f4ef Deployed Aug 24, 2021 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants