2019-05-01 11:44:45 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Error represents an http response error following the api format.
|
2019-05-02 05:54:45 +00:00
|
|
|
// These are used by the services to set the *execution status*
|
2019-05-01 11:44:45 +00:00
|
|
|
// 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:15:44 +00:00
|
|
|
// SetArguments set one or multiple arguments to the error
|
2019-05-01 11:44:45 +00:00
|
|
|
// to be displayed back to API caller
|
2019-05-01 16:15:44 +00:00
|
|
|
func (e *Error) SetArguments(arg0 interface{}, args ...interface{}) {
|
2019-05-01 11:44:45 +00:00
|
|
|
|
2019-05-01 16:15:44 +00:00
|
|
|
// 1. clear arguments */
|
|
|
|
e.Arguments = make([]interface{}, 0)
|
|
|
|
|
|
|
|
// 2. add arg[0]
|
|
|
|
e.Arguments = append(e.Arguments, arg0)
|
2019-05-01 11:44:45 +00:00
|
|
|
|
2019-05-01 16:15:44 +00:00
|
|
|
// 3. add optional other arguments
|
|
|
|
for _, arg := range args {
|
|
|
|
e.Arguments = append(e.Arguments, arg)
|
|
|
|
}
|
2019-05-01 11:44:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements 'error'
|
|
|
|
func (e Error) Error() string {
|
2019-05-01 16:23:57 +00:00
|
|
|
if e.Arguments == nil || len(e.Arguments) < 1 {
|
|
|
|
return fmt.Sprintf("[%d] %s", e.Code, e.Reason)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("[%d] %s (%v)", e.Code, e.Reason, e.Arguments)
|
2019-05-01 11:44:45 +00:00
|
|
|
}
|