tinygo icon indicating copy to clipboard operation
tinygo copied to clipboard

Error handling patterns

Open charathram opened this issue 3 years ago • 4 comments

This is a general question and I'm posting it here because I don't see a discussion forum anywhere. Apologies in advance.

How does one handle errors from TinyGo programs? For instance, if I were writing a regular Go program, I might do something like:

v, err := doSomething()
if err != nil {
    fmt.Println("An error occurred")
    os.Exit(1)
}

Obviously, that doesn't make sense in the context of an embedded program. So, are there any typical error handling patterns that people follow when writing TinyGo programs for microcontrollers?

charathram avatar Jul 13 '22 17:07 charathram

Hello @charathram

One pattern I have used myself, is to call a "failMessage()` function in that case, which outputs something useful via serial port for debugging.

For example, see https://github.com/tinygo-org/drivers/blob/release/examples/wifinina/mqttclient/main.go#L74

deadprogram avatar Jul 13 '22 17:07 deadprogram

Thank you! That's very helpful.

charathram avatar Jul 15 '22 15:07 charathram

Should this pattern be documented somewhere or can we close this bug?

dgryski avatar Jul 21 '22 22:07 dgryski

It might be useful to have it documented. I can open a PR on the docs site for it - I just need to figure out the appropriate location.

charathram avatar Jul 21 '22 22:07 charathram