Skip to content

Commit

Permalink
[PLAT-10562] Fixed a NullPointerException (#209)
Browse files Browse the repository at this point in the history
* [PLAT-10562] Fixed PlatformException(NullPointerException...........)

* Updated Changelog

---------

Co-authored-by: Robert <robert.smartbear@gmail.com>
  • Loading branch information
robert-smartbear and Robert committed Jul 14, 2023
1 parent d9ffbb4 commit 308eea2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 60 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## TBD

- Additional null safety checks in BugsnagFlutter.java [#209](https://github.com/bugsnag/bugsnag-flutter/pull/209)
- Update bugsnag-cocoa from v6.25.0 to [v6.26.2](https://github.com/bugsnag/bugsnag-cocoa/blob/master/CHANGELOG.md#6262-2023-04-20)
- Update bugsnag-android from v5.28.3 to [v5.30.0](https://github.com/bugsnag/bugsnag-android/blob/master/CHANGELOG.md#5300-2023-05-11)
- Prevent crashing if the stack trace is empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class BugsnagFlutter {

Context context;

JSONObject attach(@NonNull JSONObject args) throws Exception {
JSONObject attach(@Nullable JSONObject args) throws Exception {
JSONObject result = new JSONObject()
.put("config", new JSONObject()
.put("enabledErrorTypes", new JSONObject()
Expand All @@ -71,18 +71,20 @@ JSONObject attach(@NonNull JSONObject args) throws Exception {

client = new InternalHooks(nativeClient);

Notifier notifier = client.getNotifier();
JSONObject notifierJson = args.getJSONObject("notifier");
notifier.setName(notifierJson.getString("name"));
notifier.setVersion(notifierJson.getString("version"));
notifier.setUrl(notifierJson.getString("url"));
notifier.setDependencies(Collections.singletonList(new Notifier()));
if (args != null && args.has("notifier")) {
Notifier notifier = client.getNotifier();
JSONObject notifierJson = args.getJSONObject("notifier");
notifier.setName(notifierJson.getString("name"));
notifier.setVersion(notifierJson.getString("version"));
notifier.setUrl(notifierJson.getString("url"));
notifier.setDependencies(Collections.singletonList(new Notifier()));
}

isAttached = true;
return result;
}

Void start(@NonNull JSONObject args) throws Exception {
Void start(@Nullable JSONObject args) throws Exception {
if (isStarted) {
Log.w("BugsnagFlutter", "bugsnag.start() was called more than once. Ignoring.");
return null;
Expand All @@ -96,38 +98,39 @@ Void start(@NonNull JSONObject args) throws Exception {
if (InternalHooks.getClient() != null) {
throw new IllegalStateException("bugsnag.start() may not be called after starting Bugsnag natively");
}
JSONObject arguments = args != null ? args : new JSONObject();

Configuration configuration = args.has("apiKey")
? new Configuration(args.getString("apiKey"))
Configuration configuration = arguments.has("apiKey")
? new Configuration(arguments.getString("apiKey"))
: Configuration.load(context);

configuration.setAppType(args.optString("appType", configuration.getAppType()));
configuration.setAppVersion(args.optString("appVersion", configuration.getAppVersion()));
configuration.setAutoTrackSessions(args.optBoolean("autoTrackSessions", configuration.getAutoTrackSessions()));
configuration.setAutoDetectErrors(args.optBoolean("autoDetectErrors", configuration.getAutoDetectErrors()));
configuration.setContext(args.optString("context", configuration.getContext()));
configuration.setLaunchDurationMillis(args.optLong("launchDurationMillis", configuration.getLaunchDurationMillis()));
configuration.setSendLaunchCrashesSynchronously(args.optBoolean("sendLaunchCrashesSynchronously", configuration.getSendLaunchCrashesSynchronously()));
configuration.setMaxBreadcrumbs(args.optInt("maxBreadcrumbs", configuration.getMaxBreadcrumbs()));
configuration.setMaxPersistedEvents(args.optInt("maxPersistedEvents", configuration.getMaxPersistedEvents()));
configuration.setMaxPersistedSessions(args.optInt("maxPersistedSessions", configuration.getMaxPersistedSessions()));
configuration.setMaxStringValueLength(args.optInt("maxStringValueLength", configuration.getMaxStringValueLength()));
configuration.setReleaseStage(args.optString("releaseStage", configuration.getReleaseStage()));
configuration.setPersistUser(args.optBoolean("persistUser", configuration.getPersistUser()));

if (args.has("redactedKeys")) {
configuration.setRedactedKeys(unwrap(args.optJSONArray("redactedKeys"), new HashSet<>()));
configuration.setAppType(arguments.optString("appType", configuration.getAppType()));
configuration.setAppVersion(arguments.optString("appVersion", configuration.getAppVersion()));
configuration.setAutoTrackSessions(arguments.optBoolean("autoTrackSessions", configuration.getAutoTrackSessions()));
configuration.setAutoDetectErrors(arguments.optBoolean("autoDetectErrors", configuration.getAutoDetectErrors()));
configuration.setContext(arguments.optString("context", configuration.getContext()));
configuration.setLaunchDurationMillis(arguments.optLong("launchDurationMillis", configuration.getLaunchDurationMillis()));
configuration.setSendLaunchCrashesSynchronously(arguments.optBoolean("sendLaunchCrashesSynchronously", configuration.getSendLaunchCrashesSynchronously()));
configuration.setMaxBreadcrumbs(arguments.optInt("maxBreadcrumbs", configuration.getMaxBreadcrumbs()));
configuration.setMaxPersistedEvents(arguments.optInt("maxPersistedEvents", configuration.getMaxPersistedEvents()));
configuration.setMaxPersistedSessions(arguments.optInt("maxPersistedSessions", configuration.getMaxPersistedSessions()));
configuration.setMaxStringValueLength(arguments.optInt("maxStringValueLength", configuration.getMaxStringValueLength()));
configuration.setReleaseStage(arguments.optString("releaseStage", configuration.getReleaseStage()));
configuration.setPersistUser(arguments.optBoolean("persistUser", configuration.getPersistUser()));

if (arguments.has("redactedKeys")) {
configuration.setRedactedKeys(unwrap(arguments.optJSONArray("redactedKeys"), new HashSet<>()));
}

if (args.has("discardClasses")) {
configuration.setDiscardClasses(unwrap(args.optJSONArray("discardClasses"), new HashSet<>()));
if (arguments.has("discardClasses")) {
configuration.setDiscardClasses(unwrap(arguments.optJSONArray("discardClasses"), new HashSet<>()));
}

if (args.has("enabledReleaseStages")) {
configuration.setEnabledReleaseStages(unwrap(args.optJSONArray("enabledReleaseStages"), new HashSet<>()));
if (arguments.has("enabledReleaseStages")) {
configuration.setEnabledReleaseStages(unwrap(arguments.optJSONArray("enabledReleaseStages"), new HashSet<>()));
}

JSONObject user = args.optJSONObject("user");
JSONObject user = arguments.optJSONObject("user");
if (user != null) {
configuration.setUser(
user.optString("id", null),
Expand All @@ -136,7 +139,7 @@ Void start(@NonNull JSONObject args) throws Exception {
);
}

JSONObject endpoints = args.optJSONObject("endpoints");
JSONObject endpoints = arguments.optJSONObject("endpoints");
if (endpoints != null) {
configuration.setEndpoints(
new EndpointConfiguration(
Expand All @@ -146,7 +149,7 @@ Void start(@NonNull JSONObject args) throws Exception {
);
}

String sendThreads = args.optString("sendThreads");
String sendThreads = arguments.optString("sendThreads");
if (sendThreads.equals("always")) {
configuration.setSendThreads(ThreadSendPolicy.ALWAYS);
} else if (sendThreads.equals("unhandledOnly")) {
Expand All @@ -156,10 +159,10 @@ Void start(@NonNull JSONObject args) throws Exception {
}

configuration.setEnabledBreadcrumbTypes(
EnumHelper.unwrapBreadcrumbTypes(args.optJSONArray("enabledBreadcrumbTypes"))
EnumHelper.unwrapBreadcrumbTypes(arguments.optJSONArray("enabledBreadcrumbTypes"))
);

JSONObject enabledErrorTypes = args.optJSONObject("enabledErrorTypes");
JSONObject enabledErrorTypes = arguments.optJSONObject("enabledErrorTypes");
if (enabledErrorTypes != null) {
ErrorTypes errorTypes = new ErrorTypes();
errorTypes.setUnhandledExceptions(enabledErrorTypes.optBoolean("unhandledExceptions"));
Expand All @@ -169,23 +172,25 @@ Void start(@NonNull JSONObject args) throws Exception {
configuration.setEnabledErrorTypes(errorTypes);
}

unpackMetadata(args.optJSONObject("metadata"), configuration);
unpackMetadata(arguments.optJSONObject("metadata"), configuration);

configuration.addFeatureFlags(unpackFeatureFlags(args.optJSONArray("featureFlags")));
configuration.addFeatureFlags(unpackFeatureFlags(arguments.optJSONArray("featureFlags")));

Notifier notifier = InternalHooks.getNotifier(configuration);
JSONObject notifierJson = args.getJSONObject("notifier");
notifier.setName(notifierJson.getString("name"));
notifier.setVersion(notifierJson.getString("version"));
notifier.setUrl(notifierJson.getString("url"));
notifier.setDependencies(Collections.singletonList(new Notifier()));
if (arguments.has("notifier")) {
JSONObject notifierJson = arguments.getJSONObject("notifier");
notifier.setName(notifierJson.getString("name"));
notifier.setVersion(notifierJson.getString("version"));
notifier.setUrl(notifierJson.getString("url"));
notifier.setDependencies(Collections.singletonList(new Notifier()));
}

if (args.has("persistenceDirectory")) {
configuration.setPersistenceDirectory(new File(args.getString("persistenceDirectory")));
if (arguments.has("persistenceDirectory")) {
configuration.setPersistenceDirectory(new File(arguments.getString("persistenceDirectory")));
}

if (args.has("projectPackages")) {
JSONObject projectPackages = args.optJSONObject("projectPackages");
if (arguments.has("projectPackages")) {
JSONObject projectPackages = arguments.optJSONObject("projectPackages");

JSONArray packageNames = projectPackages.getJSONArray("packageNames");
final int packageCount = packageNames.length();
Expand All @@ -202,12 +207,12 @@ Void start(@NonNull JSONObject args) throws Exception {
configuration.setProjectPackages(packagesSet);
}

if (args.has("telemetry")) {
configuration.setTelemetry(EnumHelper.unwrapTelemetry(args.optJSONArray("telemetry")));
if (arguments.has("telemetry")) {
configuration.setTelemetry(EnumHelper.unwrapTelemetry(arguments.optJSONArray("telemetry")));
}

if (args.has("versionCode")) {
configuration.setVersionCode(args.getInt("versionCode"));
if (arguments.has("versionCode")) {
configuration.setVersionCode(arguments.getInt("versionCode"));
}

client = new InternalHooks(Bugsnag.start(context, configuration));
Expand Down Expand Up @@ -246,10 +251,15 @@ String getContext(@Nullable JSONObject args) {
return Bugsnag.getContext();
}

Void leaveBreadcrumb(@NonNull JSONObject args) throws Exception {
Bugsnag.leaveBreadcrumb(args.getString("name"),
JsonHelper.unwrap(args.getJSONObject("metaData")),
JsonHelper.unpackBreadcrumbType(args.getString("type")));
Void leaveBreadcrumb(@Nullable JSONObject args) throws Exception {
if (args != null &&
args.has("name") &&
args.has("metaData") &&
args.has("type")) {
Bugsnag.leaveBreadcrumb(args.getString("name"),
JsonHelper.unwrap(args.getJSONObject("metaData")),
JsonHelper.unpackBreadcrumbType(args.getString("type")));
}
return null;
}

Expand All @@ -268,8 +278,10 @@ Void addFeatureFlags(@Nullable JSONArray args) {
return null;
}

Void clearFeatureFlag(@NonNull JSONObject args) throws JSONException {
Bugsnag.clearFeatureFlag(args.getString("name"));
Void clearFeatureFlag(@Nullable JSONObject args) throws JSONException {
if (args != null && args.has("name")) {
Bugsnag.clearFeatureFlag(args.getString("name"));
}
return null;
}

Expand All @@ -279,7 +291,7 @@ Void clearFeatureFlags(@Nullable JSONObject args) {
}

Void addMetadata(@Nullable JSONObject args) throws JSONException {
if (args == null) {
if (args == null || !args.has("section") || !args.has("metadata")) {
return null;
}

Expand All @@ -291,7 +303,7 @@ Void addMetadata(@Nullable JSONObject args) throws JSONException {
}

Void clearMetadata(@Nullable JSONObject args) throws JSONException {
if (args == null) {
if (args == null || !args.has("section")) {
return null;
}

Expand All @@ -305,7 +317,7 @@ Void clearMetadata(@Nullable JSONObject args) throws JSONException {
}

JSONObject getMetadata(@Nullable JSONObject args) throws JSONException {
if (args == null) {
if (args == null || !args.has("section")) {
return null;
}

Expand Down Expand Up @@ -341,7 +353,7 @@ JSONObject getLastRunInfo(@Nullable Void args) throws JSONException {

@SuppressWarnings("unchecked")
JSONObject createEvent(@Nullable JSONObject args) throws JSONException {
if (args == null) {
if (args == null || !args.has("error")) {
return null;
}

Expand Down

0 comments on commit 308eea2

Please sign in to comment.