Flow 1.0.1
Flow project: Full implementation reference.
error_fwd.hpp
Go to the documentation of this file.
1/* Flow
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
19#pragma once
20
22#include "flow/common.hpp"
23
24/**
25 * Flow module that facilitates working with error codes and exceptions; essentially comprised of niceties on top
26 * boost.system's error facility. As of this writing, we feel the latter facility is fantastic and quite complete;
27 * so as of this writing flow::error is intended to contain only minor syntactic-sugary items.
28 *
29 * @note Flow uses the convention wherein error-related facilities of a particular Flow module X (in
30 * `namespace flow::X`) -- especially in particular X's dedicated error code set, if any -- are to reside
31 * in `namespace flow::X::error`, mirroring the naming of up-one-level-from-that `flow::error`.
32 *
33 * @see flow::Error_code whose doc header introduces the error-emission conventions used in all Flow modules.
34 */
35namespace flow::error
36{
37// Types.
38
39// Find doc headers near the bodies of these compound types.
40
41class Runtime_error;
42
43// Free functions.
44
45/**
46 * Helper for FLOW_ERROR_EXEC_AND_THROW_ON_ERROR() macro that does everything in the latter not needing a preprocessor.
47 * Probably not to be called except by that macro.
48 *
49 * This function is the meat of it. I omit details, since they are explained in FLOW_ERROR_EXEC_AND_THROW_ON_ERROR()
50 * doc header. The present function does what is described therein -- except for preprocessor-requiring actions:
51 *
52 * Preprocessor is needed to (1) supply halfway-decent "context" (file/line #/etc.) info to the
53 * exception object; and (2) to trigger the invoker of the macro (the actual user-facing API or very close to it)
54 * to return XYZ to *its* caller (user code or very close to it). So this function cannot do that, but instead it
55 * respectively (1) takes the context string as an argument; and (2) returns `true` if and only if the invoker of the
56 * macro should in fact immediately return the value `*ret`, where `ret` is an "out" argument
57 * to the present function.
58 *
59 * @note ATTENTION! This is a (rare) case where documentation is more complicated than just looking at the
60 * implementation. This embarrassingly verbose doc header is sort of a pedantic formality in this particular
61 * (rare) case and is probably not worth reading for most people.
62 * @tparam Func
63 * Any type such that given an instance `Func f`, the expression `r = f(&e_c)` is valid,
64 * assuming `e_c` is an #Error_code, and `r` is a `Ret`. In practice, this is usually the insane
65 * type of whatever concoction lambda or `bind()` conjures out of a plain, hard-working method like
66 * Peer_socket::send() and a bunch of innocent, API user-originated arguments thereto.
67 * @tparam Ret
68 * The return type of the operation `func()`. Should equal `ARG_ret_type` of
69 * FLOW_ERROR_EXEC_AND_THROW_ON_ERROR().
70 * @param func
71 * The operation, with the return type and argument as described above in `Func` template argument doc,
72 * that performs whatever possibly error-generating actions are being wrapped by all this; and returns
73 * whatever value is intended for the ultimate API caller. The `Error_code*` passed into `func()` will NOT be
74 * null; thus `func()` must NOT throw Runtime_error on error.
75 * @param ret
76 * Non-null pointer to a value into which the present function will place the return value of `func()`, if
77 * indeed it executes the latter (and thus `true` is returned).
78 * @param err_code
79 * Null; or non-null pointer to an error code in memory; probably originating from the API caller.
80 * `*err_code` is set if and only if `err_code` was non-null; and `func()` was executed (thus `true` returned);
81 * and `func()` encountered an error.
82 * @param context
83 * Value suitable for the `context` argument of Runtime_error constructor.
84 * Most likely this should be something it takes little to no runtime computation to obtain;
85 * so either your own string literal; or FLOW_UTIL_WHERE_AM_I_LITERAL().
86 * @return `true` if and only if `err_code` is null; and therefore `func()` was called.
87 * `false` otherwise (i.e., `err_code` is not null; nothing was done; and the caller should perform the
88 * equivalent of `func()` while safely assuming `*err_code` may be assigned to upon error).
89 */
90template<typename Func, typename Ret>
91bool exec_and_throw_on_error(const Func& func, Ret* ret,
92 Error_code* err_code, util::String_view context);
93
94/**
95 * Equivalent of exec_and_throw_on_error() for operations with `void` return type. Unlike the latter function,
96 * however, the present function is to be used directly as opposed to via macro.
97 *
98 * @tparam Func
99 * Any type such that given an instance `Func f`, the expression `f(&e_c)` is valid,
100 * assuming `e_c` is an #Error_code.
101 * @param func
102 * The operation, with the return type `void` and argument as described above in `Func` template argument doc,
103 * that performs whatever possibly error-generating actions are being wrapped by all this.
104 * The `Error_code*` passed into `func()` will NOT be null; thus `func()` must NOT throw Runtime_error on error.
105 * @param err_code
106 * See exec_and_throw_on_error().
107 * @param context
108 * See exec_and_throw_on_error().
109 * @return See exec_and_throw_on_error().
110 */
111template<typename Func>
112bool exec_void_and_throw_on_error(const Func& func, Error_code* err_code, util::String_view context);
113
114} // namespace flow::error
Flow module that facilitates working with error codes and exceptions; essentially comprised of niceti...
Definition: error.cpp:22
bool exec_void_and_throw_on_error(const Func &func, Error_code *err_code, util::String_view context)
Equivalent of exec_and_throw_on_error() for operations with void return type.
Definition: error.hpp:168
bool exec_and_throw_on_error(const Func &func, Ret *ret, Error_code *err_code, util::String_view context)
Helper for FLOW_ERROR_EXEC_AND_THROW_ON_ERROR() macro that does everything in the latter not needing ...
Definition: error.hpp:128
Basic_string_view< char > String_view
Commonly used char-based Basic_string_view. See its doc header.
boost::system::error_code Error_code
Short-hand for a boost.system error code (which basically encapsulates an integer/enum error code and...
Definition: common.hpp:502