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 module with a function divide(), that throws an error if the denominator is zero:

import { object, func } from "@dagger.io/dagger"

@object()
class MyModule {
@func()
divide(a: number, b: number): number {
if (b == 0) {
throw new Error("cannot divide by zero")
}

return a / b
}
}

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