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:
parent
af0411420a
commit
da7eef2e04
1 changed files with 22 additions and 2 deletions
24
errors.go
24
errors.go
|
@ -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.
|
||||||
|
|
Loading…
Reference in a new issue