From 696b8ae3e4aa434d81ec7fcfb3bd99611e3077fd Mon Sep 17 00:00:00 2001 From: Jeremy Rose Date: Tue, 20 Feb 2024 00:54:10 -0800 Subject: [PATCH] Add some basic exception reporting for emscripten (#71853) --- index.html | 5 ++++- src/emscripten_exception.cpp | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/emscripten_exception.cpp diff --git a/index.html b/index.html index 78d5bd7422dd4..5292ea2e9e70f 100644 --- a/index.html +++ b/index.html @@ -287,7 +287,10 @@ }, }; Module.setStatus("Downloading..."); - window.onerror = function (event) { + window.onerror = function (event, source, lineno, colno, error) { + if (typeof error === 'number' && Module.getExceptionMessage) { + console.log(Module.getExceptionMessage(error)); + } alert("Exception thrown, see JavaScript console."); }; window.onbeforeunload = (e) => { diff --git a/src/emscripten_exception.cpp b/src/emscripten_exception.cpp new file mode 100644 index 0000000000000..573f18071205c --- /dev/null +++ b/src/emscripten_exception.cpp @@ -0,0 +1,13 @@ +#if defined(EMSCRIPTEN) +#include + +std::string getExceptionMessage( intptr_t exceptionPtr ) +{ + return std::string( reinterpret_cast( exceptionPtr )->what() ); +} + +EMSCRIPTEN_BINDINGS( Bindings ) +{ + emscripten::function( "getExceptionMessage", &getExceptionMessage ); +} +#endif