2015-01-30 18:13:15 +00:00
|
|
|
package hawser
|
2014-08-07 22:04:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
type WrappedError struct {
|
|
|
|
Err error
|
|
|
|
Message string
|
2014-08-08 16:51:47 +00:00
|
|
|
stack []byte
|
2014-08-08 17:05:24 +00:00
|
|
|
context map[string]string
|
2014-08-07 22:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Error(err error) *WrappedError {
|
|
|
|
return Errorf(err, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Errorf(err error, format string, args ...interface{}) *WrappedError {
|
2014-08-08 16:51:47 +00:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-07 22:04:04 +00:00
|
|
|
e := &WrappedError{
|
|
|
|
Err: err,
|
|
|
|
Message: err.Error(),
|
2014-08-08 16:51:47 +00:00
|
|
|
stack: Stack(),
|
2014-08-07 22:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(format) > 0 {
|
|
|
|
e.Errorf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2014-08-08 17:05:24 +00:00
|
|
|
func (e *WrappedError) Set(key, value string) {
|
|
|
|
if e.context == nil {
|
|
|
|
e.context = make(map[string]string)
|
|
|
|
}
|
|
|
|
e.context[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *WrappedError) Get(key string) string {
|
|
|
|
if e.context == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return e.context[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *WrappedError) Del(key string) {
|
|
|
|
if e.context == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delete(e.context, key)
|
|
|
|
}
|
|
|
|
|
2014-08-07 22:04:04 +00:00
|
|
|
func (e *WrappedError) Error() string {
|
|
|
|
return e.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *WrappedError) Errorf(format string, args ...interface{}) {
|
|
|
|
e.Message = fmt.Sprintf(format, args...)
|
|
|
|
}
|
2014-08-07 22:08:47 +00:00
|
|
|
|
2014-08-08 16:51:47 +00:00
|
|
|
func (e *WrappedError) InnerError() string {
|
|
|
|
return e.Err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *WrappedError) Stack() []byte {
|
|
|
|
return e.stack
|
|
|
|
}
|
|
|
|
|
2014-08-08 17:05:24 +00:00
|
|
|
func (e *WrappedError) Context() map[string]string {
|
|
|
|
if e.context == nil {
|
|
|
|
e.context = make(map[string]string)
|
|
|
|
}
|
|
|
|
return e.context
|
|
|
|
}
|
|
|
|
|
2014-08-07 22:08:47 +00:00
|
|
|
func Stack() []byte {
|
|
|
|
stackBuf := make([]byte, 1024*1024)
|
|
|
|
written := runtime.Stack(stackBuf, false)
|
|
|
|
return stackBuf[:written]
|
|
|
|
}
|