Skip to content

chore: switch to throw error instead of this.error#216

Closed
edezekiel wants to merge 4 commits into
mainfrom
ee/cli-error
Closed

chore: switch to throw error instead of this.error#216
edezekiel wants to merge 4 commits into
mainfrom
ee/cli-error

Conversation

@edezekiel
Copy link
Copy Markdown
Contributor

Previously, we had two separate mechanisms for handling errors:

  1. Invoking this.error from components;
  2. Throwing an error from a service

Invoking this.error is conventional in oclif, but this kind of error handling masks the underlying behavior and throws off our linter.

This PR consolidates our error handling to use the basic Error type and take advantage of the 'cause' Node api.[1]

[1]https://nodejs.org/api/errors.html#errorcause

@edezekiel edezekiel requested a review from a team as a code owner April 30, 2025 17:17
Comment thread bin/main.js
});
} catch (error) {
process.exit(1);
if (error instanceof Error) {
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.

question: in what cases is this condition not met? I think all or most thrown errors use Error or extend it (just curious)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In reality I don't know how anything besides an Error could reach this catch block. You're right that our application code always throws Errors.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Its always an instance of error. You could add an assert

Comment thread src/commands/report/purls.ts Outdated
case 'EACCES':
this.error('Permission denied: Cannot write to output file');
break;
throw new Error('Permission denied: Cannot write to output file');
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.

question: do we need to pass the cause to all of these cases as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it would probably be useful context! I'll add the cause here as well.

@marco-ippolito
Copy link
Copy Markdown
Member

If there are multiple try catches the root cause will create a chain of causes which make the stack super long. Can you share what a stacktrace looks like?

@edezekiel edezekiel closed this May 8, 2025
@edezekiel edezekiel deleted the ee/cli-error branch May 8, 2025 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants