Icon HelpCircleForumIcon Link

⌘K

Icon HelpCircleForumIcon Link
Debugging Revert Errors

Icon LinkDebugging 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:

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

The SDK will throw an error that says:

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`'
);

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

Icon LinkTemporary Workarounds

Icon LinkUsing __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:

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

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

expect(() => contract.functions.test_function_with_str_array_message().call()).rejects.toThrow(
  'The script reverted with reason RequireFailed. (Reason: "This is also a revert error")'
);

Icon LinkUsing 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:

enum Errors {
    InvalidInput: (),
}

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

fn test_function_with_custom_error() -> bool {
    require(false, Errors::InvalidInput);
    true
}

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

expect(() => contract.functions.test_function_with_custom_error().call()).rejects.toThrow(
  'The script reverted with reason RequireFailed. (Reason: "InvalidInput")'
);