-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: oclif-friendly error logging (#1080)
| 🚥 Resolves RM-11449 | | :------------------- | ## 🧰 Changes Fixes up the error logging so it maintains parity with `rdme@8`, while doing so in a way that's friendly with oclif. Also slightly reworked our vitest config to load env variables a little bit more smoothly. ## 🧬 QA & Testing Do tests still pass?
- Loading branch information
1 parent
6439425
commit 73c5f4c
Showing
7 changed files
with
75 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import { format } from 'node:util'; | |
|
||
import * as core from '@actions/core'; | ||
import { Command as OclifCommand } from '@oclif/core'; | ||
import chalk from 'chalk'; | ||
import debugPkg from 'debug'; | ||
|
||
import { isGHA, isTest } from './isCI.js'; | ||
|
@@ -44,10 +45,40 @@ export default abstract class BaseCommand<T extends typeof OclifCommand> extends | |
|
||
abstract run(): Promise<string>; | ||
|
||
protected async catch(err: Error & { exitCode?: number }): Promise<unknown> { | ||
// add any custom logic to handle errors from the command | ||
// or simply return the parent class error handling | ||
return super.catch(err); | ||
protected async catch(err: Error & { exitCode?: number }) { | ||
if (isTest()) { | ||
return super.catch(err); | ||
} | ||
|
||
let message = `Yikes, something went wrong! Please try again and if the problem persists, get in touch with our support team at ${chalk.underline( | ||
'[email protected]', | ||
)}.`; | ||
|
||
if (err.message) { | ||
message = err.message; | ||
} | ||
|
||
/** | ||
* If we're in a GitHub Actions environment, log errors with that formatting instead. | ||
* | ||
* @see {@link https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message} | ||
* @see {@link https://github.com/actions/toolkit/tree/main/packages/core#annotations} | ||
*/ | ||
if (isGHA()) { | ||
return core.setFailed(message); | ||
} | ||
|
||
// If this is a soft error then we should output the result as a regular log but exit the CLI | ||
// with an error status code. | ||
if (err.name === 'SoftError') { | ||
// eslint-disable-next-line no-console | ||
console.log(err.message); | ||
} else { | ||
// eslint-disable-next-line no-console | ||
console.error(chalk.red(`\n${message}\n`)); | ||
} | ||
|
||
return process.exit(process.exitCode ?? err.exitCode ?? 1); | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters