github.com/Axway/agent-sdk@v1.1.101/pkg/util/errors/agenterror.go (about) 1 package errors 2 3 import ( 4 "fmt" 5 ) 6 7 // AgentError - Agent Error 8 type AgentError struct { 9 error 10 11 formattedErr bool 12 Code int `json:"code" structs:"code,omitempty"` 13 Message string `json:"message" structs:"message,omitempty"` 14 formatArgs []interface{} 15 } 16 17 // New - Creates a new Agent Error 18 func New(errCode int, errMessage string) *AgentError { 19 return &AgentError{formattedErr: false, Code: errCode, Message: errMessage} 20 } 21 22 // Newf - Creates a new formatted Agent Error 23 func Newf(errCode int, errMessage string) *AgentError { 24 return &AgentError{formattedErr: true, Code: errCode, Message: errMessage} 25 } 26 27 // Wrap -add additional data to a defined error 28 func Wrap(agentError *AgentError, info string) *AgentError { 29 message := agentError.Message 30 if info != "" { 31 message += fmt.Sprintf(": %s", info) 32 } 33 return &AgentError{ 34 formattedErr: agentError.formattedErr, 35 Code: agentError.Code, 36 Message: message, 37 formatArgs: agentError.formatArgs, 38 } 39 } 40 41 // FormatError - Creates a Error with applied formatting 42 func (e *AgentError) FormatError(args ...interface{}) error { 43 return &AgentError{formattedErr: e.formattedErr, Code: e.Code, Message: e.Message, formatArgs: args} 44 } 45 46 // Error - Returns the formatted error message 47 func (e *AgentError) Error() string { 48 if e.formattedErr { 49 formattedMsg := fmt.Sprintf(e.Message, e.formatArgs...) 50 return fmt.Sprintf("[Error Code %d] - %s", e.Code, formattedMsg) 51 } 52 53 return fmt.Sprintf("[Error Code %d] - %s", e.Code, e.Message) 54 } 55 56 // GetErrorCode - Returns the error code 57 func (e *AgentError) GetErrorCode() int { 58 return e.Code 59 }