From 309a63b6c3e2a0b245aef8bf0bbe853cc9e2c9e3 Mon Sep 17 00:00:00 2001 From: Joris Coenen Date: Tue, 5 Jan 2021 10:01:11 +0100 Subject: [PATCH] Allow UnexpectedError to be unwrapped to orig err This allows displaying the original error without any other text, while not breaking the existing contract of throwing UnexpectedErrors. --- internals/errio/errors.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/internals/errio/errors.go b/internals/errio/errors.go index ea490e0b..77b348b1 100644 --- a/internals/errio/errors.go +++ b/internals/errio/errors.go @@ -158,6 +158,7 @@ func UnexpectedError(err error) PublicError { "an unexpected error occurred: %v\n\nTry again later or contact support@secrethub.io if the problem persists", err, ), + err: err, } } @@ -190,6 +191,7 @@ type PublicError struct { Namespace Namespace `json:"namespace,omitempty"` Code string `json:"code"` Message string `json:"message"` + err error } // PublicError implements the error interface. @@ -221,6 +223,11 @@ func (e PublicError) Type() string { return fmt.Sprintf("%s.%s", e.Namespace, e.Code) } +// Unwrap returns the wrapped error if the PublicError represents an error wrapped as an UnexpectedError. +func (e PublicError) Unwrap() error { + return e.err +} + // PublicStatusError represents an http error. It contains an HTTP status // code and can be json encoded in an HTTP response. type PublicStatusError struct {