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
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { assertError } from '@backstage/errors';
import { toError } from '@backstage/errors';
import { IndexableDocument } from '@backstage/plugin-search-common';
import { Writable } from 'node:stream';
@@ -66,8 +66,7 @@ export abstract class BatchSearchEngineIndexer extends Writable {
await this.initialize();
done();
} catch (e) {
assertError(e);
done(e);
done(toError(e));
}
}
@@ -91,8 +90,7 @@ export abstract class BatchSearchEngineIndexer extends Writable {
this.currentBatch = [];
done();
} catch (e) {
assertError(e);
done(e);
done(toError(e));
}
}
@@ -110,8 +108,7 @@ export abstract class BatchSearchEngineIndexer extends Writable {
await this.finalize();
done();
} catch (e) {
assertError(e);
done(e);
done(toError(e));
}
}
}
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { assertError } from '@backstage/errors';
import { toError } from '@backstage/errors';
import { IndexableDocument } from '@backstage/plugin-search-common';
import { Transform } from 'node:stream';
@@ -60,8 +60,7 @@ export abstract class DecoratorBase extends Transform {
await this.initialize();
done();
} catch (e) {
assertError(e);
done(e);
done(toError(e));
}
}
@@ -96,8 +95,7 @@ export abstract class DecoratorBase extends Transform {
this.push(decorated);
done();
} catch (e) {
assertError(e);
done(e);
done(toError(e));
}
}
@@ -110,8 +108,7 @@ export abstract class DecoratorBase extends Transform {
await this.finalize();
done();
} catch (e) {
assertError(e);
done(e);
done(toError(e));
}
}
}