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: os: add ErrNotSupported #39436

Closed
bcmills opened this issue Jun 6, 2020 · 6 comments
Closed

proposal: os: add ErrNotSupported #39436

bcmills opened this issue Jun 6, 2020 · 6 comments

Comments

@bcmills
Copy link
Contributor

bcmills commented Jun 6, 2020

We currently have many different syscall errors that indicate operations or functions that are not supported by the current platform:

  • syscall.EWINDOWS indicates an operation not supported by windows.
  • syscall.EPLAN9 indicates an operation not supported by plan 9.
  • On POSIX platforms:
    • ENOSYS indicates function not implemented.
    • ENOTSUP indicates functionality not supported or operation not supported.
    • EOPNOTSUPP indicates operation not supported on socket. On some platforms, it is identical to ENOTSUP.

However, none of those constants is portable: syscall.ENOSYS and syscall.ENOTSUP are not defined on plan9, and errors.Is(syscall.EWINDOWS, syscall.ENOSYS) returns false on Windows, requiring separate windows source files just to be able to name the error code.

In addition:

  • net/http, x/net/websocket, and cmd/go/internal/lockedfile/internal/filelock (see also proposal: os: make the internal lockedfile package public #33974) all define errors named ErrNotSupported with similar semantics.
  • x/net/webdav and x/net/websocket both define similar errors named ErrNotImplemented, although websocket (ironically!) does not appear to ever actually use it.

We obviously can't collapse all of these errors back down into one. However, we can make them easier to manage!

I propose that we add a pre-defined error in the os package, tentatively named ErrNotSupported, with the error string operation not supported. Errors could define Is(os.ErrNotSupported) for any error that means “the operation is not supported for its operands, but the operands are not necessarily invalid”

The oserror and syscall packages would then be modified such that errors.Is(err, os.ErrNotSupported) returns true for all of syscall.EWINDOWS, syscall.EPLAN9, syscall.ENOSYS, syscall.ENOTSUP, and syscall.EOPNOTSUPP.

@gopherbot gopherbot added this to the Proposal milestone Jun 6, 2020
@rsc rsc added this to Incoming in Proposals (old) Jun 10, 2020
@earthboundkid
Copy link
Contributor

If ErrNotSupported were an interface, you could implement it without importing package os.

Simplest way:

func IsErrNotSupported(err error) bool {
	var s interface {
		error
		NotSupported() bool
	}
	if errors.As(err, &s) {
		return s.NotSupported()
	}
	return false
}

type myError struct {
	error
}

func (me myError) NotSupported() bool { return true }

func main() {
	fmt.Println(IsErrNotSupported(errors.New("nope")))
	fmt.Println(IsErrNotSupported(myError{errors.New("yep")}))
}

@rsc
Copy link
Contributor

rsc commented Aug 18, 2021

Is this now a duplicate of #41198?

@rsc
Copy link
Contributor

rsc commented Aug 18, 2021

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@rsc rsc moved this from Incoming to Active in Proposals (old) Aug 18, 2021
@rsc
Copy link
Contributor

rsc commented Aug 18, 2021

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@nightlyone
Copy link
Contributor

If ErrNotSupported were an interface, you could implement it without importing package os.

That seems als much better to me, because that would enable supporting it in your own programs too and even allow to add details why something is not supported.

@rsc rsc moved this from Active to Declined in Proposals (old) Aug 25, 2021
@rsc
Copy link
Contributor

rsc commented Aug 25, 2021

This proposal is a duplicate of a previously discussed proposal, as noted above,
and there is no significant new information to justify reopening the discussion.
The issue has therefore been declined as a duplicate.
— rsc for the proposal review group

@rsc rsc closed this as completed Aug 25, 2021
@golang golang locked and limited conversation to collaborators Aug 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

No branches or pull requests

5 participants