2014-10-23 04:45:23 +08:00
|
|
|
package libcontainer
|
|
|
|
|
|
|
|
import (
|
2016-04-19 02:37:26 +08:00
|
|
|
"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
|
|
|
|
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 {
|
2016-04-19 02:37:26 +08:00
|
|
|
return createSystemError(err, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSystemErrorWithCausef(err error, cause string, v ...interface{}) Error {
|
|
|
|
return createSystemError(err, fmt.Sprintf(cause, v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSystemErrorWithCause(err error, cause string) Error {
|
|
|
|
return createSystemError(err, cause)
|
|
|
|
}
|
|
|
|
|
|
|
|
// createSystemError creates the specified error with the correct number of
|
|
|
|
// stack frames skipped. This is only to be called by the other functions for
|
|
|
|
// formatting the error.
|
|
|
|
func createSystemError(err error, cause string) Error {
|
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,
|
2016-04-19 02:37:26 +08:00
|
|
|
Cause: cause,
|
|
|
|
Stack: stacktrace.Capture(2),
|
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:"-"`
|
2016-04-19 02:37:26 +08:00
|
|
|
Cause string
|
2015-02-18 13:50:43 +08:00
|
|
|
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-04-19 02:37:26 +08:00
|
|
|
if e.Cause == "" {
|
|
|
|
return e.Message
|
|
|
|
}
|
|
|
|
frame := e.Stack.Frames[0]
|
|
|
|
return fmt.Sprintf("%s:%d: %s caused %q", frame.File, frame.Line, e.Cause, 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
|
|
|
}
|