Skip to content

Commit 393d32f

Browse files
authored
Make FirestoreException a type defined by Firestore/core (#6589)
Move FirestoreInternalError to firebase::firestore to match FirestoreException in Android. When importing this into google3, we'll have to delete the Android-specific version of this type.
1 parent 54e0bb8 commit 393d32f

File tree

3 files changed

+98
-13
lines changed

3 files changed

+98
-13
lines changed

Firestore/core/src/util/exception.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Google
2+
* Copyright 2019 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
#include <exception>
2020
#include <stdexcept>
2121

22+
#include "Firestore/core/src/util/firestore_exceptions.h"
2223
#include "Firestore/core/src/util/hard_assert.h"
2324
#include "Firestore/core/src/util/log.h"
2425
#include "absl/strings/str_cat.h"

Firestore/core/src/util/exception.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 Google
2+
* Copyright 2019 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,24 +35,14 @@
3535
#include <stdexcept>
3636
#include <string>
3737

38+
#include "Firestore/core/include/firebase/firestore/firestore_errors.h"
3839
#include "Firestore/core/src/util/string_format.h"
3940
#include "absl/base/attributes.h"
40-
#include "absl/base/config.h"
4141

4242
namespace firebase {
4343
namespace firestore {
4444
namespace util {
4545

46-
#if ABSL_HAVE_EXCEPTIONS
47-
/**
48-
* An exception thrown if Firestore encounters an internal error.
49-
*/
50-
class FirestoreInternalError : public std::logic_error {
51-
public:
52-
using std::logic_error::logic_error;
53-
};
54-
#endif // ABSL_HAVE_EXCEPTIONS
55-
5646
/**
5747
* An enumeration of logical exception types mapping to common user visible
5848
* exceptions we might throw in response do some invalid action in an
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)