URING++
Loading...
Searching...
No Matches
error.h
1#pragma once
2#include <cerrno>
3#include <cstddef>
4#include <cstdio>
5#include <cstring>
6#include <stdexcept>
7
8namespace uringpp {
9
10constexpr size_t kErrorStringBufferSize = 1024;
11
12static inline void throw_with(const char *message) {
13 throw std::runtime_error(message);
14}
15
16template <class... Args>
17static inline void throw_with(const char *format, Args... args) {
18 char buffer[kErrorStringBufferSize];
19 ::snprintf(buffer, sizeof(buffer), format, args...);
20 throw std::runtime_error(buffer);
21}
22
23static inline void check_rc(int rc, const char *message) {
24 if (rc != 0) [[unlikely]] {
25 throw_with("%s: %s (rc=%d)", message, ::strerror(rc), rc);
26 }
27}
28
29static inline void check_ptr(void *ptr, const char *message) {
30 if (ptr == nullptr) [[unlikely]] {
31 throw_with("%s: %s (errno=%d)", message, ::strerror(errno), errno);
32 }
33}
34
35static inline void check_errno(int rc, const char *message) {
36 if (rc < 0) [[unlikely]] {
37 throw_with("%s: %s (errno=%d)", message, ::strerror(errno), errno);
38 }
39}
40
41static inline void check_nerrno(int nerrno, const char *message) {
42 if (nerrno < 0) [[unlikely]] {
43 throw_with("%s: %s (errno=%d)", message, ::strerror(-nerrno), -nerrno);
44 }
45}
46
47} // namespace uringpp