2014-10-23 04:45:23 +08:00
|
|
|
package libcontainer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-10-23 07:27:06 +08:00
|
|
|
"io"
|
|
|
|
"text/template"
|
2014-10-23 04:45:23 +08:00
|
|
|
"time"
|
2014-10-23 07:27:06 +08:00
|
|
|
|
|
|
|
"github.com/docker/libcontainer/stacktrace"
|
2014-10-23 04:45:23 +08:00
|
|
|
)
|
|
|
|
|
2014-10-23 07:27:06 +08:00
|
|
|
var errorTemplate = template.Must(template.New("error").Parse(`Timestamp: {{.Timestamp}}
|
|
|
|
Code: {{.ECode}}
|
|
|
|
Message: {{.Err.Error}}
|
|
|
|
Frames:{{range $i, $frame := .Stack.Frames}}
|
|
|
|
---
|
|
|
|
{{$i}}: {{$frame.Function}}
|
|
|
|
Package: {{$frame.Package}}
|
|
|
|
File: {{$frame.File}}{{end}}
|
|
|
|
`))
|
2014-10-23 04:45:23 +08:00
|
|
|
|
|
|
|
func newGenericError(err error, c ErrorCode) Error {
|
2015-02-12 08:45:23 +08:00
|
|
|
if le, ok := err.(Error); ok {
|
|
|
|
return le
|
|
|
|
}
|
2014-10-23 04:45:23 +08:00
|
|
|
return &GenericError{
|
2014-10-23 07:27:06 +08:00
|
|
|
Timestamp: time.Now(),
|
|
|
|
Err: err,
|
|
|
|
ECode: c,
|
|
|
|
Stack: stacktrace.Capture(2),
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 08:45:23 +08:00
|
|
|
func newSystemError(err error) Error {
|
|
|
|
if le, ok := err.(Error); ok {
|
|
|
|
return le
|
|
|
|
}
|
|
|
|
return &GenericError{
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
Err: err,
|
|
|
|
ECode: SystemError,
|
|
|
|
Stack: stacktrace.Capture(2),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-23 04:45:23 +08:00
|
|
|
type GenericError struct {
|
2014-10-23 07:27:06 +08:00
|
|
|
Timestamp time.Time
|
|
|
|
ECode ErrorCode
|
|
|
|
Err error
|
|
|
|
Stack stacktrace.Stacktrace
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GenericError) Error() string {
|
2014-10-23 07:27:06 +08:00
|
|
|
return fmt.Sprintf("[%d] %s: %s", e.ECode, e.ECode, e.Err)
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *GenericError) Code() ErrorCode {
|
2014-10-23 07:27:06 +08:00
|
|
|
return e.ECode
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
|
2014-10-23 07:27:06 +08:00
|
|
|
func (e *GenericError) Detail(w io.Writer) error {
|
|
|
|
return errorTemplate.Execute(w, e)
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|