Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: rework how TRPCError is created and add appropriate unit tests for it #3880

Merged
merged 17 commits into from
Mar 6, 2023

Conversation

kamilogorek
Copy link
Member

@kamilogorek kamilogorek commented Feb 27, 2023

🎯 Changes

Resolves one outstanding note from the codebase (#331 (comment)).

As for why it didn't work previously, it was most likely due to the odd behavior of

Object.setPrototypeOf(this, new.target.prototype);

in some environments. This is however redundant to call in non-ES6 environments, which based on #3393 is not the case for tRPC, as it requires Proxy API, which is ES6+ only.

We've also been there, but we're tied to pre-ES6 envs - https://github.com/getsentry/sentry-javascript/blob/33243242effa68390d16203917475ce26c8f2037/packages/utils/src/error.ts#L14-L17

✅ Checklist

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.

@vercel
Copy link

vercel bot commented Feb 27, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
www ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Mar 6, 2023 at 3:11AM (UTC)

@vercel
Copy link

vercel bot commented Feb 27, 2023

@kamilogorek is attempting to deploy a commit to the trpc Team on Vercel.

A member of the Team first needs to authorize it.

@KATT KATT changed the title ref: Rework how TRPCError is created and add appropriate unit tests for it chore: rework how TRPCError is created and add appropriate unit tests for it Mar 3, 2023
KATT
KATT previously approved these changes Mar 3, 2023
@KATT KATT enabled auto-merge (squash) March 3, 2023 09:38
expect(trpcError).toBeInstanceOf(TRPCError);
expect(trpcError).toBeInstanceOf(Error);
});
test('should extend original Error class', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

There is more than 1 top-level thing tested in this file, that's why it was split using describe blocks :)

opts.message ?? getMessageFromUnknownError(opts.cause, code);
const cause: Error | undefined =
opts.message ?? getMessageFromUnknownError(opts.cause, opts.code);
const cause =
opts.cause !== undefined ? getErrorFromUnknown(opts.cause) : undefined;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore https://github.com/tc39/proposal-error-cause
super(message, { cause });
Copy link
Member

Choose a reason for hiding this comment

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

I don't have the history on this, what's the reason for not following the Error constructor?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error#syntax

Copy link
Member Author

Choose a reason for hiding this comment

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

In what sense?

Copy link
Member

Choose a reason for hiding this comment

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

The Error constructor accepts a message as the first argument, and then options (or filename and lineNumber), whereas TRPCError only accepts an object combining both the message and options. IMO it would be better to follow the same constructor and extend the options to add the code, but I'm sure there's a reason why it was made like this (but that would require a breaking change).

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah ok, I don't know the history here as well. From what I see it's most likely because message is optional and not always known at compile time. It could feel awkward to do new TRPCError(null, { code: 420 });. Just a guess.

Copy link
Member

Choose a reason for hiding this comment

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

If there is more that can be done that would result in something breaking we can do it on the next branch

@kamilogorek kamilogorek merged commit a0615be into trpc:main Mar 6, 2023
@kamilogorek kamilogorek deleted the trpc-error-rework branch March 6, 2023 13:12
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 5, 2023
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.

None yet

5 participants