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:
@@ -23,6 +23,7 @@ import {
|
||||
WriteResult,
|
||||
} from '@google-cloud/firestore';
|
||||
|
||||
import { toError } from '@backstage/errors';
|
||||
import { AnyJWK, KeyStore, StoredKey } from './types';
|
||||
|
||||
export type FirestoreKeyStoreSettings = Settings & Options;
|
||||
@@ -66,14 +67,11 @@ export class FirestoreKeyStore implements KeyStore {
|
||||
try {
|
||||
await keyStore.verify();
|
||||
} catch (error) {
|
||||
const err = toError(error);
|
||||
if (process.env.NODE_ENV !== 'development') {
|
||||
throw new Error(
|
||||
`Failed to connect to database: ${(error as Error).message}`,
|
||||
);
|
||||
throw new Error(`Failed to connect to database: ${err.message}`);
|
||||
}
|
||||
logger?.warn(
|
||||
`Failed to connect to database: ${(error as Error).message}`,
|
||||
);
|
||||
logger?.warn(`Failed to connect to database: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
import { AuthService, LoggerService } from '@backstage/backend-plugin-api';
|
||||
import { Config } from '@backstage/config';
|
||||
import { assertError, NotFoundError } from '@backstage/errors';
|
||||
import { NotFoundError, toError } from '@backstage/errors';
|
||||
import {
|
||||
AuthOwnershipResolver,
|
||||
AuthProviderFactory,
|
||||
@@ -104,14 +104,17 @@ export function bindProviderRouters(
|
||||
|
||||
targetRouter.use(`/${providerId}`, r);
|
||||
} catch (e) {
|
||||
assertError(e);
|
||||
if (process.env.NODE_ENV !== 'development') {
|
||||
throw new Error(
|
||||
`Failed to initialize ${providerId} auth provider, ${e.message}`,
|
||||
`Failed to initialize ${providerId} auth provider, ${
|
||||
toError(e).message
|
||||
}`,
|
||||
);
|
||||
}
|
||||
|
||||
logger.warn(`Skipping ${providerId} auth provider, ${e.message}`);
|
||||
logger.warn(
|
||||
`Skipping ${providerId} auth provider, ${toError(e).message}`,
|
||||
);
|
||||
|
||||
targetRouter.use(`/${providerId}`, () => {
|
||||
// If the user added the provider under auth.providers but the clientId and clientSecret etc. were not found.
|
||||
|
||||
Reference in New Issue
Block a user