summaryrefslogtreecommitdiff
path: root/ext/librethinkdbxx/src/error.h
blob: ab75e248b4ca5e51dea0374de52d51b48aa1eeea (plain)
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
#pragma once

#include <cstdarg>
#include <cstring>
#include <string>
#include <cerrno>

namespace RethinkDB {

// All errors thrown by the server have this type
struct Error {
    template <class ...T>
    explicit Error(const char* format_, T... A) {
        format(format_, A...);
    }

    Error() = default;
    Error(Error&&) = default;
    Error(const Error&) = default;

    Error& operator= (Error&& other) {
        message = std::move(other.message);
        return *this;
    }

    static Error from_errno(const char* str){
        return Error("%s: %s", str, strerror(errno));
    }

    // The error message
    std::string message;

private:
    const size_t max_message_size = 2048;

    void format(const char* format_, ...) {
        va_list args;
        va_start(args, format_);
        char message_[max_message_size];
        vsnprintf(message_, max_message_size, format_, args);
        va_end(args);
        message = message_;
    }
};

}