errors: add toError utility and migrate assertError usages
Add a `toError` utility function to `@backstage/errors` that converts unknown values to `ErrorLike` objects. If the value is already error-like it is returned as-is. Strings are used directly as the error message, and other values are stringified with a fallback to JSON.stringify to avoid unhelpful `[object Object]` messages. Non-error causes passed to `CustomErrorBase` are now converted and stored using `toError` rather than discarded. Existing `assertError` call sites across the codebase are migrated to `toError`. Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com> Made-with: Cursor
This commit is contained in:
@@ -17,7 +17,7 @@
|
||||
import { ChildProcess, SpawnOptions } from 'node:child_process';
|
||||
import spawn from 'cross-spawn';
|
||||
import { ExitCodeError } from './errors';
|
||||
import { assertError } from '@backstage/errors';
|
||||
import { toError } from '@backstage/errors';
|
||||
|
||||
/**
|
||||
* Callback function that can be used to receive stdout or stderr data from a child process.
|
||||
@@ -193,14 +193,14 @@ export async function runOutput(
|
||||
|
||||
return Buffer.concat(stdoutChunks).toString().trim();
|
||||
} catch (error) {
|
||||
assertError(error);
|
||||
const err = toError(error);
|
||||
|
||||
(error as Error & { stdout?: string }).stdout =
|
||||
(err as Error & { stdout?: string }).stdout =
|
||||
Buffer.concat(stdoutChunks).toString();
|
||||
(error as Error & { stderr?: string }).stderr =
|
||||
(err as Error & { stderr?: string }).stderr =
|
||||
Buffer.concat(stderrChunks).toString();
|
||||
|
||||
throw error;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user