tinygo
tinygo copied to clipboard
Error handling patterns
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?
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
Thank you! That's very helpful.
Should this pattern be documented somewhere or can we close this bug?
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.