From da7eef2e0427d4f077a46b6ae39749fa8f7abf66 Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Mon, 10 Nov 2014 15:56:23 -0800 Subject: [PATCH] Allow Errors to be an error itself This has Errors implement the error interface, allowing it to pose as an error itself. Use of this in the server may be minimal, for now, but it's useful for passing around opaque client errors. A method, PushErr, has also been add to allow arbitrary errors to be passed into the Errors list. This keeps the errors list flexible, allowing the app to collect and errors before we have codes properly mapped. --- errors.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/errors.go b/errors.go index 751515944..53dcb6bf6 100644 --- a/errors.go +++ b/errors.go @@ -119,7 +119,7 @@ func (e Error) Error() string { // Errors provides the envelope for multiple errors and a few sugar methods // for use within the application. type Errors struct { - Errors []Error `json:"errors,omitempty"` + Errors []error `json:"errors,omitempty"` } // Push pushes an error on to the error stack, with the optional detail @@ -135,13 +135,33 @@ func (errs *Errors) Push(code ErrorCode, details ...interface{}) { detail = details[0] } - errs.Errors = append(errs.Errors, Error{ + errs.PushErr(Error{ Code: code, Message: code.Message(), Detail: detail, }) } +// PushErr pushes an error interface onto the error stack. +func (errs *Errors) PushErr(err error) { + errs.Errors = append(errs.Errors, err) +} + +func (errs *Errors) Error() string { + switch len(errs.Errors) { + case 0: + return "" + case 1: + return errs.Errors[0].Error() + default: + msg := "errors:\n" + for _, err := range errs.Errors { + msg += err.Error() + "\n" + } + return msg + } +} + // detailUnknownLayer provides detail for unknown layer errors, returned by // image manifest push for layers that are not yet transferred. This intended // to only be used on the backend to return detail for this specific error.