Skip to content

Debugging Revert Errors

Unfortunately, the SDK does not support debugging revert errors with string messages - at least temporarily. This is because the SDK does not support decoding of Sway str slices in the v0 Sway encoding scheme. This problem will be fixed soon once the v1 Sway encoding scheme is adopted.

But for now, if your Sway contract has a revert error with a string message like this:

rust
fn test_function() -> bool {
    require(false, "This is a revert error");
    true
}
See code in context

The SDK will throw an error that says:

ts
expect(() => contract.functions.test_function().call()).rejects.toThrow(
  'String slices can not be decoded from logs. Convert the slice to `str[N]` with `__to_str_array`'
);
See code in context

It will not log out the message associated with the revert error. This can make debugging functions with multiple require statements difficult.

Temporary Workarounds

Using __to_str_array

You can work around this by using the __to_str_array helper function to convert the str slice to a string array:

rust
fn test_function_with_str_array_message() -> bool {
    require(false, __to_str_array("This is also a revert error"));
    true
}
See code in context

The SDK will log out the message associated with the revert error like so:

ts
expect(() => contract.functions.test_function_with_str_array_message().call()).rejects.toThrow(
  'The transaction reverted because a "require" statement has thrown "This is also a revert error".'
);
See code in context

Using custom error enums

You can also work around this by using custom enums to represent the error messages. For example, you can create an enum like this:

rust
enum Errors {
    InvalidInput: (),
}
See code in context

Then, you can use the enum in your contract like this:

rust
fn test_function_with_custom_error() -> bool {
    require(false, Errors::InvalidInput);
    true
}
See code in context

The SDK will log out the message associated with the revert error like so:

ts
expect(() => contract.functions.test_function_with_custom_error().call()).rejects.toThrow(
  'The transaction reverted because a "require" statement has thrown "InvalidInput".'
);
See code in context