0

I'm trying to provide better error messages. I've tried several things already that don't work such as regular python fstrings, regular string concatenation. I'm tried to using the sp.string function now, but it still doesn't work.

Example:

x_i = (sp.to_int(params.x + i))
y_j = (sp.to_int(params.y + j))
err_msg = "contains pixel(" + sp.string(x_i) + ") that has been bought already"
sp.failwith(err_msg)

Error message when running it:

Error: Error in Scenario
Literal format error: (string(toInt(add(attr(params 48)x 57)(iter i 55)58)58))
1

It is not possible to cast an int to a string in Michelson.

I recommend you to have static error messages, such as:

class Errors:
  SOME_ERROR = "SOME_ERROR"

# some contract
class Contract(sp.Contract):
  ...

  x_i = (sp.to_int(params.x + I))
  y_j = (sp.to_int(params.y + j))
  sp.failwith(Errors.SOME_ERROR)

SmartPy also gives you the option to define the exception verbosity.

4
  • 2
    Failing is possible on a non constant as well such as: sp.failwith(("contains pixel", x_i, "that has been bought already")). It should also provide all the necessary data (but it will take a lot of place). – FFF Apr 6 at 20:19
  • 1
    Also, sp.string is for string literals, aka, constants. – FFF Apr 6 at 20:20
  • @FFF passing in that tuple worked! (Feel free to write that as a separate answer and I can mark it as accepted) What do you mean it will take a lot of place? – Jaime Apr 6 at 20:37
  • 2
    Strings are major culprits for storage taken by smart contracts. You may not care for a small contract but it’s important for bigger ones when you do this repeatedly (this is the reason why SmartPy compiles location error as the line number only). – FFF Apr 7 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.