forked from qdrvm/kagome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutcome_throw.hpp
44 lines (38 loc) · 1.06 KB
/
outcome_throw.hpp
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
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <boost/system/system_error.hpp>
#include <boost/throw_exception.hpp>
#include "outcome/outcome.hpp"
namespace kagome::common {
/**
* @brief throws outcome::result error as boost exception
* @tparam T enum error type, only outcome::result enums are allowed
* @param t error value
*/
template <typename T>
requires std::is_enum_v<T>
void raise(T t) {
std::error_code ec = make_error_code(t);
boost::throw_exception(std::system_error(ec));
}
/**
* @brief throws outcome::result error made of error as boost exception
* @tparam T outcome error type
* @param t outcome error value
*/
template <typename T>
requires(not std::is_enum_v<T>)
void raise(const T &t) {
boost::throw_exception(std::system_error(t.value(), t.category()));
}
template <typename T>
void raise_on_err(const outcome::result<T> &res) {
if (res.has_error()) {
raise(res.error());
}
}
} // namespace kagome::common