Skip to content

Map AggegateError and Error.cause to ApplicationFailureInfo #1734

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

Open
wants to merge 1 commit 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
11 changes: 11 additions & 0 deletions packages/common/src/converter/failure-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,17 @@ export class DefaultFailureConverter implements FailureConverter {
message: String(err.message) ?? '',
stackTrace: cutoffStackTrace(err.stack),
cause: this.optionalErrorToOptionalFailure((err as any).cause, payloadConverter),
applicationFailureInfo:
'errors' in err && Array.isArray(err.errors)
? {
details: {
payloads: toPayloads(
payloadConverter,
err.errors.map((errInner) => this.optionalErrorToOptionalFailure(errInner, payloadConverter))
),
},
}
: undefined,
};
}

Expand Down
25 changes: 24 additions & 1 deletion packages/common/src/failure.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { temporal } from '@temporalio/proto';
import { cutoffStackTrace } from '../lib';
import { errorMessage, isRecord, SymbolBasedInstanceOfError } from './type-helpers';
import { Duration } from './time';
import { makeProtoEnumConverters } from './internal-workflow';
Expand Down Expand Up @@ -412,9 +413,31 @@ export function ensureApplicationFailure(error: unknown): ApplicationFailure {
return error;
}

function toDetails(error: unknown): Record<string, unknown> | undefined {
if (isRecord(error)) {
return {
message: String(error.message),
type: error.constructor?.name ?? error.name,
stack: cutoffStackTrace(String(error.stack)),
cause: toDetails(error.cause),
details: Array.isArray(error.errors) ? error.errors.map(toDetails) : undefined,
};
} else if (error != null) {
return { message: String(error) };
} else {
return undefined;
}
}

const message = (isRecord(error) && String(error.message)) || String(error);
const type = (isRecord(error) && (error.constructor?.name ?? error.name)) || undefined;
const failure = ApplicationFailure.create({ message, type, nonRetryable: false });
const failure = ApplicationFailure.create({
message,
type,
cause: isRecord(error) && error.cause instanceof Error ? error.cause : undefined,
details: isRecord(error) && Array.isArray(error.errors) ? error.errors.map(toDetails) : undefined,
nonRetryable: false,
});
failure.stack = (isRecord(error) && String(error.stack)) || '';
return failure;
}
Expand Down
Loading