git-lfs/lfs/errors.go

88 lines
1.4 KiB
Go
Raw Normal View History

2015-03-19 19:30:55 +00:00
package lfs
import (
"fmt"
"runtime"
)
type WrappedError struct {
Err error
Message string
2015-02-17 18:46:08 +00:00
Panic bool
2014-08-08 16:51:47 +00:00
stack []byte
2014-08-08 17:05:24 +00:00
context map[string]string
}
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
}
e := &WrappedError{
Err: err,
Message: err.Error(),
2015-02-17 18:46:08 +00:00
Panic: true,
2014-08-08 16:51:47 +00:00
stack: Stack(),
}
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)
}
func (e *WrappedError) Error() string {
return e.Message
}
func (e *WrappedError) Errorf(format string, args ...interface{}) {
e.Message = fmt.Sprintf(format, args...)
}
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
}
func Stack() []byte {
stackBuf := make([]byte, 1024*1024)
written := runtime.Stack(stackBuf, false)
return stackBuf[:written]
}