exception.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #ifndef FIRESTORE_CORE_SRC_UTIL_EXCEPTION_H_
  17. #define FIRESTORE_CORE_SRC_UTIL_EXCEPTION_H_
  18. // Routines in this file are used to throw an exception (or crash, depending on
  19. // platform) in response to API usage errors. Exceptions should only be used
  20. // for programmer errors made by consumers of the SDK, e.g. invalid method
  21. // arguments.
  22. //
  23. // These routines avoid conditional compilation in the caller and avoids lint
  24. // warnings around actually throwing exceptions in source. The implementation
  25. // chooses the best way to surface a logic error to the developer.
  26. //
  27. // For recoverable runtime errors, use util::Status, or in pure Objective-C
  28. // code use an NSError** out-parameter.
  29. //
  30. // For internal programming errors, including internal argument checking, use
  31. // HARD_ASSERT or HARD_FAIL().
  32. #include <stdexcept>
  33. #include <string>
  34. #include "Firestore/core/include/firebase/firestore/firestore_errors.h"
  35. #include "Firestore/core/src/util/string_format.h"
  36. #include "absl/base/attributes.h"
  37. namespace firebase {
  38. namespace firestore {
  39. namespace util {
  40. /**
  41. * An enumeration of logical exception types mapping to common user visible
  42. * exceptions we might throw in response do some invalid action in an
  43. * interaction with the Firestore API.
  44. */
  45. enum class ExceptionType {
  46. AssertionFailure,
  47. IllegalState,
  48. InvalidArgument,
  49. };
  50. using ThrowHandler = void (*)(ExceptionType type,
  51. const char* file,
  52. const char* func,
  53. int line,
  54. const std::string& message);
  55. /**
  56. * Overrides the default exception throw handler.
  57. *
  58. * The default essentially just calls std::terminate. While reasonable for C++
  59. * with exceptions disabled, this isn't optimal for platforms that merely use
  60. * the C++ core as their implementation and would otherwise be expected to throw
  61. * a platform specific exception.
  62. *
  63. * @param handler A function that will handle the exception. This function is
  64. * expected not to return. (If it does, std::terminate() will be called
  65. * immediately after it does so.)
  66. * @return A pointer to the previous failure handler.
  67. */
  68. ThrowHandler SetThrowHandler(ThrowHandler handler);
  69. ABSL_ATTRIBUTE_NORETURN void Throw(ExceptionType type,
  70. const char* file,
  71. const char* func,
  72. int line,
  73. const std::string& message);
  74. /**
  75. * Throws an exception indicating that the user passed an invalid argument.
  76. *
  77. * Invalid argument is interpreted pretty broadly and can mean that the user
  78. * made an incompatible chained method call while building up a larger
  79. * structure, like a query.
  80. */
  81. template <typename... FA>
  82. ABSL_ATTRIBUTE_NORETURN void ThrowInvalidArgument(const char* format,
  83. const FA&... args) {
  84. Throw(ExceptionType::InvalidArgument, nullptr, nullptr, 0,
  85. StringFormat(format, args...));
  86. }
  87. /**
  88. * Throws an exception that indicates the user has attempted to use an API
  89. * that's in an illegal state, usually by violating a precondition of the API
  90. * call.
  91. *
  92. * Good uses of these are things like using a write batch after committing or
  93. * trying to use Firestore without initializing FIRApp. Builder-style APIs that
  94. * haven't done anything yet should likely just stick to ThrowInvalidArgument.
  95. */
  96. template <typename... FA>
  97. ABSL_ATTRIBUTE_NORETURN void ThrowIllegalState(const char* format,
  98. const FA&... args) {
  99. Throw(ExceptionType::IllegalState, nullptr, nullptr, 0,
  100. StringFormat(format, args...));
  101. }
  102. } // namespace util
  103. } // namespace firestore
  104. } // namespace firebase
  105. #endif // FIRESTORE_CORE_SRC_UTIL_EXCEPTION_H_