|
| 1 | +/* |
| 2 | + * Copyright 2020 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 FIRESTORE_CORE_SRC_UTIL_FIRESTORE_EXCEPTIONS_H_ |
| 18 | +#define FIRESTORE_CORE_SRC_UTIL_FIRESTORE_EXCEPTIONS_H_ |
| 19 | + |
| 20 | +// Common C++ exception declarations across iOS and Android. These aren't a |
| 21 | +// public API surface. |
| 22 | +// |
| 23 | +// See exception.h for how to throw exceptions in a platform-agnostic way. |
| 24 | + |
| 25 | +#include <exception> |
| 26 | +#include <string> |
| 27 | + |
| 28 | +#include "Firestore/core/include/firebase/firestore/firestore_errors.h" |
| 29 | + |
| 30 | +#if defined(_STLPORT_VERSION) |
| 31 | +// Abseil does not support STLPort, so avoid their config.h here. |
| 32 | +// |
| 33 | +// TODO(b/163140650): Remove once the Firebase support floor moves to NDK R18. |
| 34 | +// |
| 35 | +// Meanwhile, NDK R16b (the current minimum) includes Clang 5.0.3 and GCC 4.9. |
| 36 | +// While Clang supports `__cpp_exceptions` at that version, GCC does not. Both |
| 37 | +// support `__EXCEPTIONS`. |
| 38 | +#if __EXCEPTIONS |
| 39 | +#define FIRESTORE_HAVE_EXCEPTIONS 1 |
| 40 | +#endif |
| 41 | + |
| 42 | +#else // !defined(_STLPORT_VERSION) |
| 43 | +// On any other supported platform, just take Abseil's word for it. |
| 44 | +#include "absl/base/config.h" |
| 45 | + |
| 46 | +#if ABSL_HAVE_EXCEPTIONS |
| 47 | +#define FIRESTORE_HAVE_EXCEPTIONS 1 |
| 48 | +#endif |
| 49 | +#endif // defined(_STLPORT_VERSION) |
| 50 | + |
| 51 | +namespace firebase { |
| 52 | +namespace firestore { |
| 53 | + |
| 54 | +#if FIRESTORE_HAVE_EXCEPTIONS |
| 55 | + |
| 56 | +/** |
| 57 | + * An exception thrown if Firestore encounters an unhandled error. |
| 58 | + */ |
| 59 | +class FirestoreException : public std::exception { |
| 60 | + public: |
| 61 | + FirestoreException(const std::string& message, Error code) |
| 62 | + : message_(message), code_(code) { |
| 63 | + } |
| 64 | + |
| 65 | + const char* what() const noexcept override { |
| 66 | + return message_.c_str(); |
| 67 | + } |
| 68 | + |
| 69 | + Error code() const { |
| 70 | + return code_; |
| 71 | + } |
| 72 | + |
| 73 | + private: |
| 74 | + std::string message_; |
| 75 | + Error code_; |
| 76 | +}; |
| 77 | + |
| 78 | +/** |
| 79 | + * An exception thrown if Firestore encounters an internal, unrecoverable error. |
| 80 | + */ |
| 81 | +class FirestoreInternalError : public FirestoreException { |
| 82 | + public: |
| 83 | + FirestoreInternalError(const std::string& message, |
| 84 | + Error code = Error::kErrorInternal) |
| 85 | + : FirestoreException(message, code) { |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +#endif // FIRESTORE_HAVE_EXCEPTIONS |
| 90 | + |
| 91 | +} // namespace firestore |
| 92 | +} // namespace firebase |
| 93 | + |
| 94 | +#endif // FIRESTORE_CORE_SRC_UTIL_FIRESTORE_EXCEPTIONS_H_ |
0 commit comments