I have some code where async
functions may throw errors and I'm using unhandledrejection
events to handle them because those uncaught throw
don't error
events are fired because they occur within a Promise
. So my error handler is getting PromiseRejectionEvent
instead of ErrorEvent
.
So far so good, error handling is working fine.
But I have a small problem here, that is, the PromiseRejectionEvent
event lacks location information, unlike the ErrorEvent
event, which puts the location into the code that raised the error Lieutenant General is very useful.
When the code does not use the async
function and the handler gets the ErrorEvent
event, event.filename
, event.lineno## is required. # and
event.colno, but these cphpcn activities are missing from
PromiseRejectionEventphpcnend.
SO My question is: Is there any way I can wrap the PromiseRejectionEvent and re-dispatch it so that in the end I have an
ErrorEvent with location information ?
bigFunctionWithAllTheCodeIncludingAsyncFunctions() .catch(error => { const errorEvent = new ErrorEvent('unhandledrejection', { 'error': error, }); window.dispatchEvent(errorEvent); });The above code generates a correct
ErrorEvent and related error message
but all position information is useless (0 represents row and column, '' represents the file name).
Thank you very much in advance :)
Yes! But it cannot be achieved by building and dispatching
ErrorEvent
yourself. Instead, you can usereportError
: