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:
Patrik Oldsberg
2026-04-03 01:33:34 +02:00
parent 3cdf048f77
commit b2319ffe45
70 changed files with 400 additions and 345 deletions
@@ -32,7 +32,7 @@ import os from 'node:os';
import fs from 'fs-extra';
import { Lockfile } from '../util/Lockfile';
import { memoize } from 'lodash';
import { assertError } from '@backstage/errors';
import { toError } from '@backstage/errors';
import { LoggerService } from '@backstage/backend-plugin-api';
/** @public */
@@ -179,7 +179,7 @@ export class DevToolsBackendApi {
const data = ConfigReader.fromConfigs(sanitizedConfigs).get();
configInfo.config = data;
} catch (error) {
assertError(error);
const err = toError(error);
// The config is not valid for some reason but we want to be able to see it still
const config = {
data: this.config.get() as JsonObject,
@@ -194,10 +194,10 @@ export class DevToolsBackendApi {
const data = ConfigReader.fromConfigs(sanitizedConfigs).get();
configInfo.config = data;
configInfo.error = {
name: error.name,
message: error.message,
messages: error.messages as string[] | undefined,
stack: error.stack,
name: err.name,
message: err.message,
messages: err.messages as string[] | undefined,
stack: err.stack,
};
}