From 4a4cc7dc3794535f3d65736cde4293ecf0e565e0 Mon Sep 17 00:00:00 2001 From: Ian Gudger Date: Thu, 3 Jan 2019 15:09:31 -0800 Subject: [PATCH] Allow creating syserr.Errors at runtime. Not allowing this was an oversight. PiperOrigin-RevId: 227757813 Change-Id: I845800ab69028b7320afca36d832c477ff17c5ce --- pkg/syserr/syserr.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/syserr/syserr.go b/pkg/syserr/syserr.go index 6a66e23a2..dad83e80c 100644 --- a/pkg/syserr/syserr.go +++ b/pkg/syserr/syserr.go @@ -68,8 +68,21 @@ func New(message string, linuxTranslation *linux.Errno) *Error { return err } +// NewDynamic creates a new error with a dynamic error message and an errno +// translation. +// +// 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 { + return &Error{message: message, errno: linuxTranslation} +} + // NewWithoutTranslation creates a new Error. If translation is attempted on // the error, translation will fail. +// +// NewWithoutTranslation may be called at any time, but static errors should +// be declared as global variables and dynamic errors should be used sparingly. func NewWithoutTranslation(message string) *Error { return &Error{message: message, noTranslation: true} }