2014-10-23 04:45:23 +08:00
|
|
|
package libcontainer
|
|
|
|
|
|
|
|
import (
|
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
|
|
|
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/stacktrace"
|
2014-10-23 04:45:23 +08:00
|
|
|
)
|
|
|
|
|
2015-12-17 17:16:34 +08:00
|
|
|
type syncType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
procReady syncType = iota
|
|
|
|
procError
|
|
|
|
procRun
|
2016-02-17 18:20:06 +08:00
|
|
|
procHooks
|
|
|
|
procResume
|
2015-12-17 17:16:34 +08:00
|
|
|
)
|
|
|
|
|
2016-01-26 10:15:44 +08:00
|
|
|
type syncT struct {
|
|
|
|
Type syncType `json:"type"`
|
|
|
|
}
|
|
|
|
|
2014-10-23 07:27:06 +08:00
|
|
|
var errorTemplate = template.Must(template.New("error").Parse(`Timestamp: {{.Timestamp}}
|
|
|
|
Code: {{.ECode}}
|
2015-02-18 13:50:43 +08:00
|
|
|
{{if .Message }}
|
|
|
|
Message: {{.Message}}
|
2015-02-12 09:42:58 +08:00
|
|
|
{{end}}
|
2014-10-23 07:27:06 +08:00
|
|
|
Frames:{{range $i, $frame := .Stack.Frames}}
|
|
|
|
---
|
|
|
|
{{$i}}: {{$frame.Function}}
|
|
|
|
Package: {{$frame.Package}}
|
2015-02-12 09:42:58 +08:00
|
|
|
File: {{$frame.File}}@{{$frame.Line}}{{end}}
|
2014-10-23 07:27:06 +08:00
|
|
|
`))
|
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
|
|
|
|
}
|
2015-02-27 16:50:20 +08:00
|
|
|
gerr := &genericError{
|
2014-10-23 07:27:06 +08:00
|
|
|
Timestamp: time.Now(),
|
|
|
|
Err: err,
|
|
|
|
ECode: c,
|
2015-02-12 09:42:58 +08:00
|
|
|
Stack: stacktrace.Capture(1),
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
2015-02-27 16:50:20 +08:00
|
|
|
if err != nil {
|
|
|
|
gerr.Message = err.Error()
|
|
|
|
}
|
|
|
|
return gerr
|
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
|
|
|
|
}
|
2015-02-27 16:50:20 +08:00
|
|
|
gerr := &genericError{
|
2015-02-12 08:45:23 +08:00
|
|
|
Timestamp: time.Now(),
|
|
|
|
Err: err,
|
|
|
|
ECode: SystemError,
|
2015-02-12 09:42:58 +08:00
|
|
|
Stack: stacktrace.Capture(1),
|
2015-02-12 08:45:23 +08:00
|
|
|
}
|
2015-02-27 16:50:20 +08:00
|
|
|
if err != nil {
|
|
|
|
gerr.Message = err.Error()
|
|
|
|
}
|
|
|
|
return gerr
|
2015-02-12 08:45:23 +08:00
|
|
|
}
|
|
|
|
|
2015-02-12 09:12:03 +08:00
|
|
|
type genericError struct {
|
2014-10-23 07:27:06 +08:00
|
|
|
Timestamp time.Time
|
|
|
|
ECode ErrorCode
|
2015-02-18 13:50:43 +08:00
|
|
|
Err error `json:"-"`
|
|
|
|
Message string
|
2014-10-23 07:27:06 +08:00
|
|
|
Stack stacktrace.Stacktrace
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
|
2015-02-12 09:12:03 +08:00
|
|
|
func (e *genericError) Error() string {
|
2016-03-12 09:08:03 +08:00
|
|
|
return e.Message
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
|
2015-02-12 09:12:03 +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
|
|
|
}
|
|
|
|
|
2015-02-12 09:12:03 +08:00
|
|
|
func (e *genericError) Detail(w io.Writer) error {
|
2014-10-23 07:27:06 +08:00
|
|
|
return errorTemplate.Execute(w, e)
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|