Error handling a method called by a go routine

Asked

Viewed 41 times

1

When we have a method like this:

func (vu *VideoUpload) ProcessUpload(concurrency int, doneUpload chan string) error {
    in := make(chan int, runtime.NumCPU())
    returnChannel := make(chan string)

    err := vu.loadPaths()

    if err != nil {
        return err
    }

    uploadClient, ctx, err := getClientUpload()

    if err != nil {
        return err
    }
    
    for process := 0; process < concurrency; process++ {
        go vu.uploadWorker(in, returnChannel, uploadClient, ctx)
    }

    go func() {
        for x:= 0; x < len(vu.Paths); x++ {
            in <- x
        }
        close(in)
    }()

    for r := range returnChannel {
        if r != "" {
            doneUpload <- r
            break
        }
    }

    return nil
}

But he’s called a go routine:

go videoUpload.ProcessUpload(concurrency, doneUpload)

How can we treat this error? Because we usually do:

err := vu.loadPaths()

    if err != nil {
        return err
    }
  • I can speak for myself, usually what I do is put a call to some log base, where I can check the data in a second moment. I believe that the golang way to treat this flow would be to use Channels to communicate between processes. But I never used this language functionality. :-/

1 answer

-1

I believe that the easiest way would be to create a Channel of Handler error, send the error messages so that they are treated as they should be (Can be used a system of Tags to separate them by context), and follow with the application or finish it depending on the error generated

Browser other questions tagged

You are not signed in. Login or sign up in order to post.