Skip to content

Commit

Permalink
fix: Correctly log aggregated errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Dec 30, 2023
1 parent 6a50b52 commit a5bfed6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions xmcl-runtime/logger/pluginLogConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function baseTransform(tag: string) { return new Transform({ transform(c, e, cb)

function getMessageFromError(e: Error): string {
let message = e.stack ?? e.message
if (e instanceof AggregateError) {
message = e.errors.map(getMessageFromError).join('\n')
if (e instanceof AggregateError || ('errors' in e && Array.isArray(e.errors))) {
message = (e as any).errors.map(getMessageFromError).join('\n')
}
if (e.cause && e.cause instanceof Error) {
return `${message}\nCaused by: ${getMessageFromError(e.cause)}`
Expand Down
18 changes: 11 additions & 7 deletions xmcl-runtime/telemetry/pluginTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,24 @@ export const pluginTelemetry: LauncherAppPlugin = async (app) => {
return d
}

const handleException = (exception: Contracts.ExceptionData, e: Error) => {
if (e.cause instanceof Error) {
exception.exceptions.push(createExceptionDetails(e.cause.message, e.cause.name, e.cause.stack))
} else if (e instanceof AggregateError || (Array.isArray((e as any).errors))) {
for (const cause of (e as any).errors) {
handleException(exception, cause)
}
}
}

const client = appInsight.defaultClient

client.addTelemetryProcessor((envelope, contextObjects) => {
if (contextObjects?.error) {
const exception = envelope.data.baseData as Contracts.ExceptionData
const e = contextObjects?.error
if (e instanceof Error) {
if (e.cause instanceof Error) {
exception.exceptions.push(createExceptionDetails(e.cause.message, e.cause.name, e.cause.stack))
} else if (e instanceof AggregateError) {
for (const cause of e.errors) {
exception.exceptions.push(createExceptionDetails(cause.message, cause.name, cause.stack))
}
}
handleException(exception, e)
}
}
return true
Expand Down

0 comments on commit a5bfed6

Please sign in to comment.