#What is the prefered way to fail
1 messages ยท Page 1 of 1 (latest)
Currently dagger.WithContainerFunc is not returning err and sometimes I need to return err while using that func.
I was thinking calling WithExec() with failure message but I'm open to better suggestions
With was meant for calling methods during query building (lazy), i.e., for chaining utilities. You can't chain if you return two values. I get that sometimes you want to conditionally do something based on an execution but you start to stretch the limits of what it can do. In Go you have the error thing. In Python it just raises an exception but you can only await in an outer factory function which doesn't have access to the current container instance for example. ๐คทโโ๏ธ Can you give a simple example of what you're trying to do with the WithExec failure message?
One simple example is sending configuration files to container.
With gale I would love to chain gale operations with dagger.Container flow like
dagger.Container. .With(gale.Something{})
For this operation I'm calling json.Marshal to create some configuration file which is returning error.
func Something(container *dagger.Container) *dagger.Container {
data,err := json.Marshal(Something{})
if err != nil {
return container.WithExec([]string{"sh", "-c", "echo 'failed to marshal github event' && exit 1"})
}
return container.WithNewFile("/some/path", dagger.ContainerWithNewFileOpts{Contents: string(data), Permissions: 0644})
}
This is what I was thinking but not sure if it make sense
You can't chain if you return two values
Yes, exactly. I have alternative ways to make it happen but I want to keep chaining while I have option to raise error.
That's clever. Other than unmarshalling outside, or raising a panic, I think if you need to handle errors, chaining isn't the most appropriate.
I'm trying to create a DX for gale without too much addition code.
If i need to handle outside, gale would have complicated DX just for error handling but I think these errors part of the pipeline definition
Totally get that ๐
Yeah, but explicit error handling is in Go's DNA.
How about panic/recover?
I'm currently conflicted since I'm trying to hack lazy init to half lazy logic.
I don't find panics really nice to read as developer when something is failed.
By the way, I'm not planing to move all error handling here. Just to move errors related with pipeline.
I was just thinking on raising the panic inside "Something", but recovering it outside and returning a normal error instead (for the pipeline, not in general). But I'm not familiar with gale and the DX you're working with.
๐ค
That could work but didn't see any extra benefit when I compare to fail using WithExec()
Btw, on that, you can use Sync to execute that immediately.
ohh totally forget we have Sync now in dagger