| 78 | explicit AutoReportException(JSContext* cx) : m_cx(cx) {} |
| 79 | |
| 80 | ~AutoReportException() { |
| 81 | if (!JS_IsExceptionPending(m_cx)) |
| 82 | return; |
| 83 | |
| 84 | // Get exception object before printing and clearing exception. |
| 85 | JS::ExceptionStack exnStack(m_cx); |
| 86 | JS::ErrorReportBuilder report(m_cx); |
| 87 | if (!JS::StealPendingExceptionStack(m_cx, &exnStack) || |
| 88 | !report.init(m_cx, exnStack, |
| 89 | JS::ErrorReportBuilder::NoSideEffects)) { |
| 90 | g_printerr("(Unable to print exception)\n"); |
| 91 | JS_ClearPendingException(m_cx); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | g_assert(!report.report()->isWarning()); |
| 96 | |
| 97 | JS::PrintError(stderr, report, /* reportWarnings = */ false); |
| 98 | |
| 99 | if (exnStack.stack()) { |
| 100 | JS::UniqueChars stack_str{ |
| 101 | format_saved_frame(m_cx, exnStack.stack(), 2)}; |
| 102 | if (!stack_str) { |
| 103 | g_printerr("(Unable to print stack trace)\n"); |
| 104 | } else { |
| 105 | Gjs::AutoChar encoded_stack_str{g_filename_from_utf8( |
| 106 | stack_str.get(), -1, nullptr, nullptr, nullptr)}; |
| 107 | if (!encoded_stack_str) |
| 108 | g_printerr("(Unable to print stack trace)\n"); |
| 109 | else |
| 110 | g_printerr("%s", stack_str.get()); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | JS_ClearPendingException(m_cx); |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | // Adapted from https://stackoverflow.com/a/17035073/172999 |
nothing calls this directly
no test coverage detected