xref: /aosp_15_r20/external/tink/python/tink/cc/pybind/tink_exception.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16 
17 #ifndef TINK_PYTHON_TINK_CC_PYBIND_TINK_EXCEPTION_H_
18 #define TINK_PYTHON_TINK_CC_PYBIND_TINK_EXCEPTION_H_
19 
20 #include <exception>
21 #include <string>
22 #include <utility>
23 
24 #include "tink/util/status.h"
25 
26 namespace pybind11 {
27 namespace google_tink {
28 
29 class TinkException : public std::exception {
30  public:
TinkException(const crypto::tink::util::Status & status)31   explicit TinkException(const crypto::tink::util::Status& status)
32       : error_code_(static_cast<int>(status.code())),
33         what_(status.ToString()) {}
34 
error_code()35   int error_code() const {
36     return error_code_;
37   }
38 
what()39   const char* what() const noexcept override {
40     return what_.c_str();
41   }
42 
43  private:
44   int error_code_;
45   std::string what_;
46 };
47 
48 }  // namespace google_tink
49 }  // namespace pybind11
50 
51 #endif  // TINK_PYTHON_TINK_CC_PYBIND_TINK_EXCEPTION_H_
52