aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/yexception.cpp
blob: cccf44ad5081c7113cf320a5c022a9d5461d1888 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "bt_exception.h"
#include "yexception.h"

#include <util/system/backtrace.h>
#include <util/system/type_name.h>

#include <cxxabi.h>

#include <stdexcept>

#include <cstdio>

static void FormatExceptionTo(IOutputStream& out, const std::exception& exception) {
    out << "(" << TypeName(exception) << ") " << exception.what();
}

TString FormatExc(const std::exception& exception) {
    TStringStream out;
    FormatExceptionTo(out, exception);
    return out.Str();
}

TString CurrentExceptionMessage() {
    auto exceptionPtr = std::current_exception();
    if (exceptionPtr) {
        try {
            std::rethrow_exception(exceptionPtr);
        } catch (const yexception& e) {
            const TBackTrace* bt = e.BackTrace();

            if (bt) {
                return TString::Join(bt->PrintToString(), TStringBuf("\n"), FormatExc(e));
            }

            return FormatExc(e);
        } catch (const std::exception& e) {
            return FormatExc(e);
        } catch (...) {
        }

        return "unknown error";
    }

    return "(NO EXCEPTION)";
}

Y_DECLARE_UNUSED static void FormatBackTraceTo(IOutputStream& out, const TBackTrace& backtrace) {
    if (backtrace.size() == 0) {
        out << "backtrace is empty";
        return;
    }
    try {
        backtrace.PrintTo(out);
    } catch (const std::exception& e) {
        out << "Failed to print backtrace: ";
        FormatExceptionTo(out, e);
    }
}

void FormatCurrentExceptionTo(IOutputStream& out) {
    auto exceptionPtr = std::current_exception();
    /*
     * The lack of current exception indicates a logical bug in the client code.
     * Do not make any attempts to workaround it, just panic and abort.
     */
    Y_VERIFY(exceptionPtr != nullptr, "there is no current exception");
#ifdef _YNDX_LIBUNWIND_ENABLE_EXCEPTION_BACKTRACE
    TBackTrace backtrace = TBackTrace::FromCurrentException();
#endif
    try {
        std::rethrow_exception(exceptionPtr);
    } catch (const std::exception& e) {
        out << "Caught:\n";
        FormatExceptionTo(out, e);
        out << "\n";
#ifdef _YNDX_LIBUNWIND_ENABLE_EXCEPTION_BACKTRACE
        FormatBackTraceTo(out, backtrace);
#endif
        return;
    } catch (...) {
        out << "unknown error\n";
        return;
    }
}

TString FormatCurrentException() {
    TStringStream out;
    FormatCurrentExceptionTo(out);
    return out.Str();
}

bool UncaughtException() noexcept {
    return std::uncaught_exceptions() > 0;
}

std::string CurrentExceptionTypeName() {
#if defined(_linux_) || defined(_darwin_)
    std::type_info* currentExceptionTypePtr = abi::__cxa_current_exception_type();
    if (currentExceptionTypePtr) {
        return TypeName(*currentExceptionTypePtr);
    }
#endif
    //There is no abi::__cxa_current_exception_type() on Windows.
    //Emulated it with rethrow - catch construction.
    std::exception_ptr currentException = std::current_exception();
    Y_ASSERT(currentException != nullptr);
    try {
        std::rethrow_exception(currentException);
    } catch (const std::exception& e) {
        return TypeName(typeid(e));
    } catch (...) {
        return "unknown type";
    }
}

void TSystemError::Init() {
    yexception& exc = *this;

    exc << TStringBuf("(");
    exc << TStringBuf(LastSystemErrorText(Status_));
    exc << TStringBuf(") ");
}

NPrivateException::yexception::yexception() {
    ZeroTerminate();
}

TStringBuf NPrivateException::yexception::AsStrBuf() const {
    if (Buf_.Left()) {
        return TStringBuf(Buf_.Data(), Buf_.Filled());
    }

    return TStringBuf(Buf_.Data(), Buf_.Filled() - 1);
}

void NPrivateException::yexception::ZeroTerminate() noexcept {
    char* end = (char*)Buf_.Current();

    if (!Buf_.Left()) {
        --end;
    }

    *end = 0;
}

const char* NPrivateException::yexception::what() const noexcept {
    return Buf_.Data();
}

const TBackTrace* NPrivateException::yexception::BackTrace() const noexcept {
    return nullptr;
}

void fputs(const std::exception& e, FILE* f) {
    char message[256];
    size_t len = Min(sizeof(message) - 2, strlcpy(message, e.what(), sizeof(message) - 1));
    message[len++] = '\n';
    message[len] = 0;
    fputs(message, f);
}

void ::NPrivate::ThrowYException(const ::NPrivate::TSimpleExceptionMessage& sm) {
    throw sm.Location + yexception() << sm.Message;
}

void ::NPrivate::ThrowYExceptionWithBacktrace(const ::NPrivate::TSimpleExceptionMessage& sm) {
    throw sm.Location + TWithBackTrace<yexception>() << sm.Message;
}