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.
This commit is contained in:
Stephen J Day 2014-11-10 15:56:23 -08:00
parent af0411420a
commit da7eef2e04

View file

@ -119,7 +119,7 @@ func (e Error) Error() string {
// Errors provides the envelope for multiple errors and a few sugar methods // Errors provides the envelope for multiple errors and a few sugar methods
// for use within the application. // for use within the application.
type Errors struct { 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 // 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] detail = details[0]
} }
errs.Errors = append(errs.Errors, Error{ errs.PushErr(Error{
Code: code, Code: code,
Message: code.Message(), Message: code.Message(),
Detail: detail, 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 "<nil>"
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 // detailUnknownLayer provides detail for unknown layer errors, returned by
// image manifest push for layers that are not yet transferred. This intended // 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. // to only be used on the backend to return detail for this specific error.