Skip to content

Commit

Permalink
feat: improve stacktrace for invoke error
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Nov 20, 2024
1 parent fd1283f commit 7020160
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/vite/src/shared/moduleRunnerTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ type InvokeableModuleRunnerTransport = Omit<ModuleRunnerTransport, 'invoke'> & {
}

function reviveInvokeError(e: any) {
return Object.assign(new Error(e.message || 'Unknown invoke error'), e)
const innerError = new Error('received error stacktrace')
innerError.stack = e.stack

// set properties to wrapped error, but use the current stacktrace
const wrappedError = new Error(e.message || 'Unknown invoke error', {
cause: innerError,
})
Object.assign(wrappedError, { ...e, stack: wrappedError.stack })
return wrappedError
}

const createInvokeableTransport = (
Expand Down Expand Up @@ -94,7 +102,7 @@ const createInvokeableTransport = (

const { e, r } = data.data
if (e) {
promise.reject(reviveInvokeError(e))
promise.reject(e)
} else {
promise.resolve(r)
}
Expand Down Expand Up @@ -161,7 +169,11 @@ const createInvokeableTransport = (
})
}

return await promise
try {
return await promise
} catch (err) {
throw reviveInvokeError(err)
}
},
}
}
Expand Down

0 comments on commit 7020160

Please sign in to comment.