Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error formatting #488

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions packages/core/src/model/GraphProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type { NodeImpl } from './NodeImpl.js';
import type { UserInputNode } from './nodes/UserInputNode.js';
import PQueueImport from 'p-queue';
import { getError } from '../utils/errors.js';
import type { NodeError } from '../utils/errors.js';
import Emittery from 'emittery';
import { entries, fromEntries, values } from '../utils/typeSafety.js';
import { isNotNull } from '../utils/genericUtilFunctions.js';
Expand Down Expand Up @@ -859,19 +860,19 @@ export class GraphProcessor {
if (erroredNodes.length && !this.#abortSuccessfully) {
let error = this.#abortError;
if (!error) {
const message = `Graph ${this.#graph.metadata!.name} (${
this.#graph.metadata!.id
}) failed to process due to errors in nodes:\n${erroredNodes
.map(([nodeId]) => `- ${this.#nodesById[nodeId]!.title} (${nodeId})`)
.join('\n')}`;
const { name, id } = this.#graph.metadata!;
const message = `Graph ${name} (${id}) failed to process due to errors in nodes`;
if (erroredNodes.length === 1) {
const [, nodeError] = erroredNodes[0]!;
error = new Error(message, { cause: nodeError });
} else {
error = new AggregateError(erroredNodes.map(([, nodeError]) => nodeError), message);
error = new AggregateError(
erroredNodes.map(([, nodeError]) => nodeError),
message,
);
}
}

await this.#emitter.emit('graphError', { graph: this.#graph, error });

if (!this.#isSubProcessor) {
Expand Down Expand Up @@ -1440,6 +1441,7 @@ export class GraphProcessor {

#nodeErrored(node: ChartNode, e: unknown, processId: ProcessId) {
const error = getError(e);
(error as NodeError).node = node;
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.#emitter.emit('nodeError', { node, error, processId });
this.#emitTraceEvent(`Node ${node.title} (${node.id}-${processId}) errored: ${error.stack}`);
Expand Down
44 changes: 44 additions & 0 deletions packages/core/src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import type { ChartNode } from '../model/NodeBase.js';

export interface NodeError extends Error {
node: ChartNode;
}

export function isNodeError(error: Error): error is NodeError {
return 'node' in error && typeof (error.node as ChartNode)?.id === 'string';
}

/** Gets an Error from an unknown error object (strict unknown errors is enabled, helper util). */
export function getError(error: unknown): Error {
const errorInstance =
Expand All @@ -6,3 +16,37 @@ export function getError(error: unknown): Error {
: new Error(error != null ? error.toString() : 'Unknown error');
return errorInstance;
}

export function rivetErrorToString(error: unknown, spaces = 0): string {
if (!(error instanceof Error)) {
if (error == null) {
return 'Unknown error';
}

return String(error);
}

if (error instanceof AggregateError) {
return error.message + '\n' + error.errors.map((e) => ' - ' + rivetErrorToString(e, spaces + 4)).join('\n');
}

let message = error.message;
if (isNodeError(error)) {
const { node } = error;
message = `${node.title} (${node.id}): ${error.message}`;
}

if (error.cause) {
message += `\nCaused by: ${rivetErrorToString(error.cause)}`;
}

return indent(message, spaces);
}

function indent(str: string, spaces: number): string {
const spacing = ' '.repeat(spaces);
return str
.split('\n')
.map((line, i) => (i === 0 ? line : spacing + line))
.join('\n');
}
40 changes: 40 additions & 0 deletions packages/core/test/utils/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { it, describe } from 'node:test';
import { strict as assert } from 'node:assert';

import { type NodeId } from '../../src/index.js';
import { NodeError, rivetErrorToString } from '../../src/utils/errors.js';

describe('rivetErrorToString', () => {
it('should handle AggregateError', () => {
const nodeError = new Error('Error 2');
(nodeError as NodeError).node = {
data: undefined,
id: 'nodeId' as NodeId,
title: 'Node title',
type: 'type',
visualData: {} as any, // Unused
};

assert.equal(
rivetErrorToString(
new AggregateError(
[
new Error('Error 1', { cause: new Error('Root cause') }), //
nodeError,
'Error 3',
null,
],
'Multiple errors',
),
),
`
Multiple errors
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer snapshots for stuff like this, but I don't think there's an easy way to do this with the native package

- Error 1
Caused by: Root cause
- Node title (nodeId): Error 2
- Error 3
- Unknown error
`.trim(),
);
});
});
Loading