Improve exception thrown when classes can't compile to be more clear (#33897)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/33897

Now the exception will display the class which caused the exception as well as helpful information as to why.

We've seen this happen a bunch due and have been very confused by the error message. It turns out that this processor runs before the classes listed are compiled. This means that if there's a compile error (or a missing import) the user will only see that this processor crashed, and not the compile error.

The additional information in the error is:

`java.lang.RuntimeException: Could not load classes set in ReactModuleList.nativeModules. Check that they exist and are imported correctly on class: com.meta.x.y.ReactPackage`

In this case,  `com.meta.x.y.ReactPackage` is the class which needs to be fixed. Before, the error message made no mention of this class or the annotation.

Changelog: [Internal] This will change the way the annotation processor crashes. It will throw a RuntimeException instead of a ClassCastException.

Reviewed By: javache

Differential Revision: D36606279

fbshipit-source-id: aedf9682286fba49e23716b7eda16b2dd3b13422
This commit is contained in:
Daniel Abramowitz
2022-05-26 08:13:22 -07:00
committed by Facebook GitHub Bot
parent 833222fa10
commit a3e25b23c6
2 changed files with 4 additions and 7 deletions
@@ -22,8 +22,6 @@ rn_java_library(
source = "8",
target = "8",
deps = [
react_native_dep("libraries/fbcore/src/main/java/com/facebook/common/logging:logging"),
react_native_target("java/com/facebook/react/common:common"),
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
react_native_dep("third-party/java/javapoet:javapoet"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
@@ -10,9 +10,7 @@ package com.facebook.react.module.processing;
import static javax.lang.model.element.Modifier.PUBLIC;
import static javax.tools.Diagnostic.Kind.ERROR;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.SuppressFieldNotInitialized;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.module.annotations.ReactModuleList;
import com.facebook.react.module.model.ReactModuleInfo;
@@ -94,9 +92,10 @@ public class ReactModuleSpecProcessor extends AbstractProcessor {
try {
reactModuleList = typeElement.getAnnotation(ReactModuleList.class);
} catch (Exception ex) {
FLog.i(
ReactConstants.TAG, "Could not reactModuleList from typeElement.getAnnotation()", ex);
throw ex;
throw new RuntimeException(
"Could not load classes set in @ReactModuleList.nativeModules. Check that they exist and are imported correctly on class: "
+ typeElement.getQualifiedName(),
ex);
}
if (reactModuleList == null) {