#What is the prefered way to fail

1 messages ยท Page 1 of 1 (latest)

winged whale
#

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

gentle nest
#

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?

winged whale
#

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.

gentle nest
#

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.

winged whale
#

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

gentle nest
#

Yeah, but explicit error handling is in Go's DNA.

#

How about panic/recover?

winged whale
#

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.

gentle nest
#

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.

winged whale
#

๐Ÿค”

#

That could work but didn't see any extra benefit when I compare to fail using WithExec()

gentle nest
winged whale
#

ohh totally forget we have Sync now in dagger