Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: all: unify context and return err idioms for error handling #69088

Closed
myaaaaaaaaa opened this issue Aug 27, 2024 · 6 comments
Closed
Labels
error-handling Language & library change proposals that are about error handling. Proposal v2 An incompatible library change
Milestone

Comments

@myaaaaaaaaa
Copy link

myaaaaaaaaa commented Aug 27, 2024

Proposal Details

Go is capable of achieving concise and elegant error handling today, without the need for any language changes.

The following code is the archetypal CopyFile() example, rewritten in terms of a unified context and return err pattern:

func CopyFile(ctx *context, src, dst string) {
	if ctx.Err() != nil {
		return
	}

	r := os.Open(ctx, src)
	defer r.Close(ctx) // must be robust to nil pointers
	w := os.Create(ctx, dst)

	if ctx.Err() != nil {
		// no need for fmt.Errorf("...: %v", ..., err)
		ctx.Push(fmt.Errorf("copy %s %s", src, dst))
		return
	}

	io.Copy(ctx, w, r)
	w.Close(ctx) // must close even if ctx.Err() != nil

	// a vet error can catch any accidental returns without checking ctx.Err()

	if ctx.Err() != nil {
		os.Remove(new(context), dst)
		ctx.Push(fmt.Errorf("copy %s %s", src, dst))
	}
}

Given that a v2 of the standard library is in the works, this is probably the best time to consider an API overhaul like this.

On the other hand, func(...) (T,error) may be too ingrained into Go culture at this point for a change like this.

@gopherbot gopherbot added this to the Proposal milestone Aug 27, 2024
@earthboundkid
Copy link
Contributor

This feels like reinventing C's errno system.

Conceptually, you can split up functions into:

  • Pure functions, which can't fail
  • Functions which can fail but don't do syscalls and aren't time sensitive (e.g. converting a string to an int)
  • Functions which do syscalls and/or are time sensitive and therefore can fail

I appreciate then that once context is involved, you always also need errors, but I don't think obscuring the return type helps. These are different kinds of functions, they should have different kinds of signatures.

If I were making a new language, I would use goroutine safe dynamic package variables instead of context and result types instead of error return tuples, but it's probably too late to change Go in either of those ways at this point, and it would be more than you could bolt on for a new standard library package.

@ianlancetaylor ianlancetaylor added the error-handling Language & library change proposals that are about error handling. label Aug 27, 2024
@ianlancetaylor
Copy link
Member

I'm not clear on what the proposal is. You are adding a Push method to context.Context? What are the semantics?

As you say, we could only make such a change in a context/v2 package.

@doggedOwl
Copy link

doggedOwl commented Aug 27, 2024

context package is already handling too many reponsibilites and it's polluting every function. this will make it impossible to write any function without the god-like context

@qiulaidongfeng
Copy link
Member

qiulaidongfeng commented Aug 28, 2024

A major problem with go error handling is repeat code such as:

if err!=nil{
    return nil,err
}

The proposal does not solve that.

The API with and without a receive context has swollen the number of standard library apis, and if try func(...) (T,error) and func(context,...) (T) Two different forms, adding API is a lot of work, not worth the loss.

@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Aug 28, 2024
@ianlancetaylor ianlancetaylor added the v2 An incompatible library change label Aug 28, 2024
@myaaaaaaaaa
Copy link
Author

Closing due to unanimous disapproval (see emoji votes)

@myaaaaaaaaa myaaaaaaaaa closed this as not planned Won't fix, can't repro, duplicate, stale Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error-handling Language & library change proposals that are about error handling. Proposal v2 An incompatible library change
Projects
None yet
Development

No branches or pull requests

7 participants