Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Use error names instead of error codes #9490

Merged
merged 2 commits into from
Apr 2, 2020
Merged

Use error names instead of error codes #9490

merged 2 commits into from
Apr 2, 2020

Conversation

felixfbecker
Copy link
Contributor

Errors in ECMAScript have a message and a name. code is something Node-specific (ErrNoException interface in TS), and those errors still all have name. Native errors like TypeError, URIError, AggregateError (coming soon) all only have name.

When we send errors to/from background pages or web workers, we can only reasonably expect name to be present, so we should never rely on code. See e.g. mozilla/webextension-polyfill#210 or comlink source code. This is a preliminary requirement to fix and prevent bugs like https://github.com/sourcegraph/sourcegraph/issues/9411.

Using both in our codebase inconsistently opens the risk to checking for the wrong property, (especially because errors are any in TypeScript) and adds additional code.

This change removes all uses of code in favor of name. There is still a case for having name be optional (e.g. GraphQL errors don't have a name, but do have message), so ErrorLike is still kept for now but we could think about just using the native Error interface in the future.

The error code constants were following NodeJS/Unix errno conventions, which don't make a lot of sense for error names and can become very unreadable. I propose the more readable CONSTANT_CASE we use elsewhere.

Depends on sourcegraph/codeintellify#240

@felixfbecker felixfbecker requested a review from a team April 1, 2020 21:52
@felixfbecker felixfbecker added the debt Technical debt. label Apr 1, 2020
@felixfbecker felixfbecker added this to the 3.15 milestone Apr 1, 2020
@@ -32,7 +32,7 @@ const proxySubscribable = <T>(subscribable: Subscribable<T>): ProxySubscribable<
// Only pass a few well-known Error properties
// TODO should pass all properties serialized recursively, best handled on comlink level
// eslint-disable-next-line @typescript-eslint/no-floating-promises
observer.error(err && { message: err.message, name: err.name, code: err.code, stack: err.stack })
Copy link
Member

Choose a reason for hiding this comment

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

just for debug purposes: might it still make sense to include it, given that there are libraries etc that use code sometimes?

/**
* An Error that aggregates multiple errors
*/
export class AggregateError extends Error {
Copy link
Member

Choose a reason for hiding this comment

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

was this unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We had two implementations of AggregateError, I changed every reference to use the other

@felixfbecker felixfbecker merged commit f72dd98 into master Apr 2, 2020
@felixfbecker felixfbecker deleted the error-names branch April 2, 2020 18:02
@felixfbecker felixfbecker mentioned this pull request Apr 6, 2020
63 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
debt Technical debt.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants