Skip to content

Commit

Permalink
Kotlinify SoftAssertions (#43911)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #43911

Changelog: [Internal]

As part of the Sustainability Week (see [post](https://fb.workplace.com/groups/251759413609061/permalink/742797531171911/)).

Reviewed By: tdn120

Differential Revision: D55797031

fbshipit-source-id: eb53ae671b0a7b5d277b931a11eb0fcd84c51a19
  • Loading branch information
fabriziocucci authored and facebook-github-bot committed Apr 9, 2024
1 parent 8b8b85b commit 51552e6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 58 deletions.
10 changes: 5 additions & 5 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -1466,11 +1466,11 @@ public class com/facebook/react/bridge/RuntimeScheduler {
public fun <init> (Lcom/facebook/jni/HybridData;)V
}

public class com/facebook/react/bridge/SoftAssertions {
public fun <init> ()V
public static fun assertCondition (ZLjava/lang/String;)V
public static fun assertNotNull (Ljava/lang/Object;)Ljava/lang/Object;
public static fun assertUnreachable (Ljava/lang/String;)V
public final class com/facebook/react/bridge/SoftAssertions {
public static final field INSTANCE Lcom/facebook/react/bridge/SoftAssertions;
public static final fun assertCondition (ZLjava/lang/String;)V
public static final fun assertNotNull (Ljava/lang/Object;)Ljava/lang/Object;
public static final fun assertUnreachable (Ljava/lang/String;)V
}

public abstract interface class com/facebook/react/bridge/Systrace : com/facebook/react/bridge/JavaScriptModule {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.bridge

/**
* Utility class to make assertions that should not hard-crash the app but instead be handled by the
* Catalyst app [JSExceptionHandler]. See the javadoc on that class for more information about our
* opinion on when these assertions should be used as opposed to assertions that might throw
* AssertionError Throwables that will cause the app to hard crash.
*/
public object SoftAssertions {

/**
* Throw [AssertionException] with a given message. Use this method surrounded with `if` block
* with assert condition in case you plan to do string concatenation to produce the message. This
* logs an assertion with ReactSoftExceptionLogger, which decides whether or not to actually
* throw.
*/
@JvmStatic
public fun assertUnreachable(message: String): Unit {
ReactSoftExceptionLogger.logSoftException("SoftAssertions", AssertionException(message))
}

/**
* Asserts the given condition, throwing an [AssertionException] if the condition doesn't hold.
* This logs an assertion with ReactSoftExceptionLogger, which decides whether or not to actually
* throw.
*/
@JvmStatic
public fun assertCondition(condition: Boolean, message: String): Unit {
if (!condition) {
ReactSoftExceptionLogger.logSoftException("SoftAssertions", AssertionException(message))
}
}

/**
* Asserts that the given object isn't null, throwing an [AssertionException] if it was. This logs
* an assertion with ReactSoftExceptionLogger, which decides whether or not to actually throw.
*/
@JvmStatic
public fun <T> assertNotNull(instance: T?): T? {
if (instance == null) {
ReactSoftExceptionLogger.logSoftException(
"SoftAssertions", AssertionException("Expected object to not be null!"))
}
return instance
}
}

0 comments on commit 51552e6

Please sign in to comment.