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:
@@ -20,7 +20,7 @@ import { McpService } from '../services/McpService';
|
||||
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
||||
import { LATEST_PROTOCOL_VERSION } from '@modelcontextprotocol/sdk/types.js';
|
||||
import { HttpAuthService, LoggerService } from '@backstage/backend-plugin-api';
|
||||
import { isError } from '@backstage/errors';
|
||||
import { toError } from '@backstage/errors';
|
||||
import { MetricsService } from '@backstage/backend-plugin-api/alpha';
|
||||
import { bucketBoundaries, McpServerSessionAttributes } from '../metrics';
|
||||
import { McpServerConfig } from '../config';
|
||||
@@ -83,11 +83,10 @@ export const createStreamableRouter = ({
|
||||
sessionDuration.record(durationSeconds, baseAttributes);
|
||||
});
|
||||
} catch (error) {
|
||||
const errorType = isError(error) ? error.name : 'Error';
|
||||
const err = toError(error);
|
||||
const errorType = err.name;
|
||||
|
||||
if (isError(error)) {
|
||||
logger.error(error.message);
|
||||
}
|
||||
logger.error(err.message);
|
||||
|
||||
if (!res.headersSent) {
|
||||
res.status(500).json({
|
||||
|
||||
Reference in New Issue
Block a user