2019-05-01 11:44:45 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Error represents an http response error following the api format.
|
|
|
|
// These are used by the controllers to set the *execution status*
|
|
|
|
// directly into the response as JSON alongside response output fields.
|
|
|
|
type Error struct {
|
2019-05-01 13:14:49 +00:00
|
|
|
Code int `json:"code"`
|
2019-05-01 11:44:45 +00:00
|
|
|
Reason string `json:"reason"`
|
2019-05-01 13:14:49 +00:00
|
|
|
Arguments []interface{} `json:"arguments"`
|
2019-05-01 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 16:01:32 +00:00
|
|
|
// NewError returns a new error from a base error with errorarguments.
|
|
|
|
func NewError(baseError Error, arguments ...interface{}) Error {
|
|
|
|
for _, arg := range arguments {
|
|
|
|
baseError.Put(arg)
|
|
|
|
}
|
|
|
|
return baseError
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// Put adds an argument to the error
|
|
|
|
// to be displayed back to API caller
|
|
|
|
func (e *Error) Put(arg interface{}) {
|
|
|
|
|
|
|
|
/* (1) Make slice if not */
|
|
|
|
if e.Arguments == nil {
|
|
|
|
e.Arguments = make([]interface{}, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Append argument */
|
|
|
|
e.Arguments = append(e.Arguments, arg)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements 'error'
|
|
|
|
func (e Error) Error() string {
|
|
|
|
return fmt.Sprintf("[%d] %s", e.Code, e.Reason)
|
|
|
|
}
|