Error Handling
Error handling in Go modules follows typical Go error patterns with explicit error
return values and if err != nil
checks. You can also use error handling to verify user input.
Here is an example module with a function divide()
, that throws an error if the denominator is zero:
// A Dagger module for saying hello world!
package main
import (
"fmt"
)
type MyModule struct {
}
func (*MyModule) Divide(a, b int) (int, error) {
if b == 0 {
return 0, fmt.Errorf("cannot divide by zero")
}
return a / b, nil
}
Here is an example call for this module:
dagger call divide --a=4 --b=2
The result will be:
2
Here is another example call for this module, this time dividing by zero:
dagger call divide --a=4 --b=0
The result will be:
cannot divide by zero