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,7 +23,7 @@ import {
|
||||
import { useCallback } from 'react';
|
||||
import useAsync from 'react-use/esm/useAsync';
|
||||
import useAsyncFn from 'react-use/esm/useAsyncFn';
|
||||
import { isError } from '@backstage/errors';
|
||||
import { toError } from '@backstage/errors';
|
||||
|
||||
interface Session {
|
||||
id: string;
|
||||
@@ -111,9 +111,7 @@ export const useConsentSession = (opts: { sessionId?: string }) => {
|
||||
if (sessionState.error) {
|
||||
return {
|
||||
status: 'error',
|
||||
error: isError(sessionState.error)
|
||||
? sessionState.error.message
|
||||
: 'Failed to load consent request',
|
||||
error: toError(sessionState.error).message,
|
||||
};
|
||||
}
|
||||
if (sessionState.value) {
|
||||
@@ -133,7 +131,7 @@ export const useConsentSession = (opts: { sessionId?: string }) => {
|
||||
await handleActionInternal(action, state.session);
|
||||
} catch (err) {
|
||||
alertApi.post({
|
||||
message: isError(err) ? err.message : `Failed to ${action} consent`,
|
||||
message: toError(err).message,
|
||||
severity: 'error',
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user