Skip to content

Rethrow without modifying cpptrace::from_current_exception? #214

Open
@KJTsanaktsidis

Description

@KJTsanaktsidis

If you've got nested CPPTRACE_TRY/CPPTRACE_CATCH blocks, like this:

CPPTRACE_TRY {
    foo();
    with_opentelemetry_sub_span([](){
        CPPRACE_TRY {
            bar();
        CPPTRACE_CATCH(const std::exception &e) {
            put_exception_on_opentelemetry_span(e, cpptrace::from_current_exception());
            throw;
        }
    });
    baz();
} CPPTRACE_CATCH(const std::exception &e) {
    put_exception_on_opentelemetry_span(e, cpptrace::from_current_exception());
    throw;
}

The second call to put_exception_on_opentelemetry_span sees the backtrace coming from the re-throw in the inner CPPTRACE_CATCH block. It would be nice if we could rethrow without losing the original call stack.

My use case for this is to be able to put error backtraces on OpenTelemetry spans; without something like this, the bottom frame of all but the innermost span would be the re-throw at the top of the previous span.

I actually was able to hack something to make it work for my usecase:

CPPTRACE_CATCH(...) {
    // stuff
    cpptrace::detail::try_canary extra_cpptrace_canary;
    cpptrace::detail::get_trace_switch() = false;
    throw;
}

which seems to work... is this something we should expose a macro or function for? CPPTRACE_RETHROW or something?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions