git-lfs/lfs/errors.go
2015-08-18 11:10:50 -06:00

91 lines
1.5 KiB
Go

package lfs
import (
"errors"
"fmt"
"runtime"
)
var genericError = errors.New("Git LFS client error")
type WrappedError struct {
Err error
Message string
Panic bool
stack []byte
context map[string]string
}
func Error(err error) *WrappedError {
return Errorf(err, "")
}
func Errorf(err error, format string, args ...interface{}) *WrappedError {
if err == nil {
err = genericError
}
e := &WrappedError{
Err: err,
Message: err.Error(),
Panic: true,
stack: Stack(),
}
if len(format) > 0 {
e.Errorf(format, args...)
}
return e
}
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...)
}
func (e *WrappedError) InnerError() string {
return e.Err.Error()
}
func (e *WrappedError) Stack() []byte {
return e.stack
}
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]
}