archethic-node icon indicating copy to clipboard operation
archethic-node copied to clipboard

SC: Handle library functions error

Open Neylix opened this issue 1 year ago • 0 comments

Is your feature request related to a problem?

As we are developing a smart contract language, we need to think about error and exception. In our case there is 3 kind of errors

  • user input error: a user called a smart contract with wrong parameter (wrong type)
  • process error: error that are handled by the code such a network issue
  • unexpected error: error raises by the system (division by 0)

I take the example of the function Json.path_extract(). Currently there is multiple behavior:

  • If the json in input is invalid it raise a Jason.DecodeError which stop the contract execution and return contract_failure
  • If the json is valid and path exists it return the value
  • If the json is valid but path doesn't exist it return an empty string ""

There is some weird behavior with this, for example if the return is an empty string, is it because the path doesn't exist or the path exist and the value is an empty string {"key": ""} ? And if it raise, we only have contract_failure without idea of what sent wrong while we could provide information

Describe the solution you'd like

There is multiple possibility to resolve this issue. As elixir all function could return something like {:ok, result} or {:error, reason}. But since the language is designed to be simple this will need pattern matching to be easy to handle or we'll need to have a lot of if statement.

An other solution would be the erlang way "let it crash" but with understandable and well catched error. For example the function Json.path_extract could throw 2 errors: Invalid json or Path not found. Those error would be catched by the interpreter as a library function error and return an appropriate error to be displayed to the user.

Indeed the last solution impose to have function allowing the user to control the input, in this case the user can use Json.is_valid? and Json.path_match? to be sure the execution of Json.path_extract will not fail

Additional context

No response

Epic

No response

Neylix avatar Jul 27 '23 11:07 Neylix