aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/exception.cpp
blob: 126dc7b593fb4dc9879d63f4398d56dc6d9da8fb (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "exception.h"

#ifdef VESPALIB_EXCEPTION_USEBACKTRACES
#include <vespa/vespalib/util/backtrace.h>
#endif

namespace vespalib {

ExceptionPtr::ExceptionPtr()
    : _ref(nullptr)
{
}

ExceptionPtr::ExceptionPtr(const Exception &e)
    : _ref(e.clone())
{
}

ExceptionPtr::ExceptionPtr(const ExceptionPtr &rhs)
    : _ref(rhs._ref != nullptr ? rhs._ref->clone() : nullptr)
{
}

ExceptionPtr &
ExceptionPtr::operator=(const Exception &rhs)
{
    ExceptionPtr tmp(rhs);
    swap(tmp);
    return *this;
}

ExceptionPtr &
ExceptionPtr::operator=(const ExceptionPtr &rhs)
{
    ExceptionPtr tmp(rhs);
    swap(tmp);
    return *this;
}

void
ExceptionPtr::swap(ExceptionPtr &other)
{
    std::swap(_ref, other._ref);
}

ExceptionPtr::~ExceptionPtr()
{
    delete _ref;
}

void
swap(ExceptionPtr &a, ExceptionPtr &b)
{
    a.swap(b);
}

//-----------------------------------------------------------------------------

Exception::Exception(stringref msg, stringref location, int skipStack)
    : _what(),
      _msg(msg),
      _location(location),
      _stackframes(getStackTraceFrames(_stack, STACK_FRAME_BUFFER_SIZE)),
      _skipStack(skipStack),
      _cause()
{
}

Exception::Exception(stringref msg, const Exception &cause, stringref location, int skipStack)
    : _what(),
      _msg(msg),
      _location(location),
      _stackframes(getStackTraceFrames(_stack, STACK_FRAME_BUFFER_SIZE)),
      _skipStack(skipStack),
      _cause(cause)
{}

Exception::Exception(const Exception &) = default;
Exception & Exception::operator = (const Exception &) = default;
Exception::Exception(Exception &&) noexcept = default;
Exception & Exception::operator = (Exception &&) noexcept = default;
Exception::~Exception() = default;

const char *
Exception::what() const noexcept
{
    if (_what.empty()) {
        _what.append(toString());
        for (const Exception *next = getCause();
             next != nullptr; next = next->getCause())
        {
            _what.append("\n--> Caused by: ");
            _what.append(next->toString());
        }
    }
    return _what.c_str();
}

const char *
Exception::getName() const
{
    return "Exception";
}

Exception *
Exception::clone() const
{
    return new Exception(*this);
}

void
Exception::throwSelf() const
{
    throw Exception(*this);
}

string
Exception::toString() const
{
    string str;
    str.append(getName());
    str.append(": ");
    str.append(_msg);
    if (!_location.empty()) {
        str.append(" at ");
        str.append(_location);
    }
    if (_stackframes > 0) {
        str.append("\n");
        str.append(getStackTrace(_skipStack, _stack, _stackframes));
    }
    return str;
}

} // namespace vespalib