forked from facebookresearch/CompilerGym
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGrpcStatusMacros.h
59 lines (53 loc) · 2.18 KB
/
GrpcStatusMacros.h
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
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#pragma once
#include <fmt/format.h>
#include <boost/current_function.hpp>
#include "glog/logging.h"
#include "grpcpp/grpcpp.h"
using grpc::Status;
#undef ASSERT_OK
/**
* Fatal error if expression returns an error status.
*
* @param expr An expression that returns a `grpc::Status`.
*/
#define ASSERT_OK(expr) \
do { \
const Status _status = (expr); \
CHECK(_status.ok()) << _status.error_message(); \
} while (0)
#undef RETURN_IF_ERROR
/**
* Return from the current function if the expression returns an error status,
* or if a std::exception is thrown.
*
* This is equivalent to:
*
* \code{.cpp}
* try {
* Status status = expr;
* if (!status.ok()) {
* return status;
* }
* } catch (std::exception& e) {
* return E_STATUS;
* }
* \endcode
*
* @param expr An expression that return a `grpc::Status`.
*/
#define RETURN_IF_ERROR(expr) \
do { \
try { \
const Status _status = (expr); \
if (!_status.ok()) \
return _status; \
} catch (std::exception & e) { \
return grpc::Status(grpc::StatusCode::INTERNAL, \
fmt::format("Unhandled exception: {}\nSource: {}:{}\nFunction: {}", \
e.what(), __FILE__, __LINE__, BOOST_CURRENT_FUNCTION)); \
} \
} while (0)