// // Copyright Contributors to the MaterialX Project // SPDX-License-Identifier: Apache-2.0 // #include #include #include namespace ems = emscripten; namespace jsexceptions { std::string getExceptionMessage(ems::val exceptionLike) { try { const std::string type = exceptionLike.typeOf().as(); if (type == "number") { // Legacy: numeric pointer to std::exception int exceptionPtr = exceptionLike.as(); return std::string(reinterpret_cast(exceptionPtr)->what()); } if (type == "string") { return exceptionLike.as(); } if (type == "object") { // Try typical Error-like object with a message property bool hasMessage = false; try { hasMessage = exceptionLike.call("hasOwnProperty", std::string("message")); } catch (...) {} if (hasMessage) { return exceptionLike["message"].as(); } // Fallback to toString() try { return exceptionLike.call("toString"); } catch (...) {} } } catch (...) { // Fall through to default } return std::string("Unknown exception"); } } // namespace jsexceptions EMSCRIPTEN_BINDINGS(exceptions) { ems::function("getExceptionMessage", &jsexceptions::getExceptionMessage); }