xref: /aosp_15_r20/external/federated-compute/fcp/base/error.h (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2019 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef FCP_BASE_ERROR_H_
18 #define FCP_BASE_ERROR_H_
19 
20 namespace fcp {
21 
22 // An Error indicates that something went wrong. Errors carry data, but are
23 // opaque to most code; error data is intended to support diagnostics, but not
24 // program behavior.
25 //
26 // Code should be written such that values are turned into opaque Errors only
27 // after they are judged to be problematic (something went wrong). If one finds
28 // the need to inspect the "reason" for an Error, some earlier (non-error) value
29 // should be used instead.
30 //
31 // Errors are typically returned inside of a Result.
32 class Error {
33   // TODO(team): Make it possible to get a stack trace from a tracing span
34   // and save this in the error.
35  public:
36   class ConstructorAccess {
37     template <class FlatBufferTable, class... Arg>
38     friend Error TraceError(Arg&&... args);
39 
40    private:
41     constexpr ConstructorAccess() = default;
42     constexpr ConstructorAccess(const ConstructorAccess&) = default;
43   };
44 
45   // Error is copyable but not trivially constructible.
46   // Use global TraceError() function to construct an Error.
Error(ConstructorAccess)47   explicit constexpr Error(ConstructorAccess) {}
48   Error(const Error&) = default;
49 };
50 
51 }  // namespace fcp
52 
53 #endif  // FCP_BASE_ERROR_H_
54