1 // Copyright © 2021-present 650 Industries, Inc. (aka Expo)
2 
3 #pragma once
4 
5 #include <fbjni/fbjni.h>
6 
7 #include <optional>
8 
9 namespace jni = facebook::jni;
10 
11 namespace expo {
12 /**
13  * A convenient wrapper for the Kotlin CodedException.
14  * It can be used with the `jni::throwNewJavaException` function to throw a cpp exception that
15  * will be automatically changed to the corresponding Java/Kotlin exception.
16  * `jni::throwNewJavaException` creates and throws a C++ exception which wraps a Java exception,
17  * so the C++ flow is interrupted. Then, when translatePendingCppExceptionToJavaException
18  * is called at the topmost level of the native stack, the wrapped Java exception is thrown to the java caller.
19  */
20 class CodedException : public jni::JavaClass<CodedException, jni::JThrowable> {
21 public:
22   static auto constexpr kJavaDescriptor = "Lexpo/modules/kotlin/exception/CodedException;";
23 
24   static jni::local_ref<CodedException> create(const std::string &message);
25 
26   std::string getCode();
27 
28   std::optional<std::string> getLocalizedMessage();
29 };
30 
31 /**
32  * A convenient wrapper for the Kotlin JavaScriptEvaluateException.
33  */
34 class JavaScriptEvaluateException
35   : public jni::JavaClass<JavaScriptEvaluateException, CodedException> {
36 public:
37   static auto constexpr kJavaDescriptor = "Lexpo/modules/kotlin/exception/JavaScriptEvaluateException;";
38 
39   static jni::local_ref<JavaScriptEvaluateException> create(
40     const std::string &message,
41     const std::string &jsStack
42   );
43 };
44 } // namespace expo
45