Skip to main content

Error Handling

Dagger modules handle errors in the same way as the language they are written in. This allows you to support any kind of error handling that your application requires. You can also use error handling to verify user input.

Here is an example Dagger Function that performs division and 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
}

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 call for this Dagger Function:

dagger call divide --a=4 --b=2

The result will be:

2

Here is another example call for this Dagger Function, this time dividing by zero:

dagger call divide --a=4 --b=0

The result will be:

cannot divide by zero