summaryrefslogtreecommitdiffhomepage
path: root/pkg/syserr/syserr.go
diff options
context:
space:
mode:
authorZach Koopmans <zkoopmans@google.com>2021-05-11 17:21:24 -0700
committergVisor bot <gvisor-bot@google.com>2021-05-11 17:23:08 -0700
commit49eb3da98aeb436011ec9d895727b237ec2ea93f (patch)
tree68fb13d15928380344ea101b27bdd24caaba21d6 /pkg/syserr/syserr.go
parentf8d22292ee8a8eeecd9da9ca9355250ebbd505f7 (diff)
[syserror] Refactor abi/linux.Errno
PiperOrigin-RevId: 373265454
Diffstat (limited to 'pkg/syserr/syserr.go')
-rw-r--r--pkg/syserr/syserr.go25
1 files changed, 10 insertions, 15 deletions
diff --git a/pkg/syserr/syserr.go b/pkg/syserr/syserr.go
index b5881ea3c..d70521f32 100644
--- a/pkg/syserr/syserr.go
+++ b/pkg/syserr/syserr.go
@@ -34,24 +34,19 @@ type Error struct {
// linux.Errno.
noTranslation bool
- // errno is the linux.Errno this Error should be translated to. nil means
- // that this Error should be translated to a nil linux.Errno.
- errno *linux.Errno
+ // errno is the linux.Errno this Error should be translated to.
+ errno linux.Errno
}
// New creates a new Error and adds a translation for it.
//
// New must only be called at init.
-func New(message string, linuxTranslation *linux.Errno) *Error {
+func New(message string, linuxTranslation linux.Errno) *Error {
err := &Error{message: message, errno: linuxTranslation}
- if linuxTranslation == nil {
- return err
- }
-
// TODO(b/34162363): Remove this.
- errno := linuxTranslation.Number()
- if errno <= 0 || errno >= len(linuxBackwardsTranslations) {
+ errno := linuxTranslation
+ if errno < 0 || int(errno) >= len(linuxBackwardsTranslations) {
panic(fmt.Sprint("invalid errno: ", errno))
}
@@ -74,7 +69,7 @@ func New(message string, linuxTranslation *linux.Errno) *Error {
// NewDynamic should only be used sparingly and not be used for static error
// messages. Errors with static error messages should be declared with New as
// global variables.
-func NewDynamic(message string, linuxTranslation *linux.Errno) *Error {
+func NewDynamic(message string, linuxTranslation linux.Errno) *Error {
return &Error{message: message, errno: linuxTranslation}
}
@@ -87,7 +82,7 @@ func NewWithoutTranslation(message string) *Error {
return &Error{message: message, noTranslation: true}
}
-func newWithHost(message string, linuxTranslation *linux.Errno, hostErrno unix.Errno) *Error {
+func newWithHost(message string, linuxTranslation linux.Errno, hostErrno unix.Errno) *Error {
e := New(message, linuxTranslation)
addLinuxHostTranslation(hostErrno, e)
return e
@@ -119,10 +114,10 @@ func (e *Error) ToError() error {
if e.noTranslation {
panic(fmt.Sprintf("error %q does not support translation", e.message))
}
- if e.errno == nil {
+ errno := int(e.errno)
+ if errno == linux.NOERRNO {
return nil
}
- errno := e.errno.Number()
if errno <= 0 || errno >= len(linuxBackwardsTranslations) || !linuxBackwardsTranslations[errno].ok {
panic(fmt.Sprintf("unknown error %q (%d)", e.message, errno))
}
@@ -131,7 +126,7 @@ func (e *Error) ToError() error {
// ToLinux converts the Error to a Linux ABI error that can be returned to the
// application.
-func (e *Error) ToLinux() *linux.Errno {
+func (e *Error) ToLinux() linux.Errno {
if e.noTranslation {
panic(fmt.Sprintf("No Linux ABI translation available for %q", e.message))
}