mirror of
https://github.com/opencontainers/runc.git
synced 2025-10-09 09:20:10 +08:00

This ensures that anything written to the logs are synced as they happen. This also changes the error message of the libcontainer error. The original idea was to have this extra information in the message but it makes it hard to parse and if the caller needed this information they can just get it from the error type. Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
88 lines
1.5 KiB
Go
88 lines
1.5 KiB
Go
package libcontainer
|
|
|
|
import (
|
|
"io"
|
|
"text/template"
|
|
"time"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/stacktrace"
|
|
)
|
|
|
|
type syncType uint8
|
|
|
|
const (
|
|
procReady syncType = iota
|
|
procError
|
|
procRun
|
|
procHooks
|
|
procResume
|
|
)
|
|
|
|
type syncT struct {
|
|
Type syncType `json:"type"`
|
|
}
|
|
|
|
var errorTemplate = template.Must(template.New("error").Parse(`Timestamp: {{.Timestamp}}
|
|
Code: {{.ECode}}
|
|
{{if .Message }}
|
|
Message: {{.Message}}
|
|
{{end}}
|
|
Frames:{{range $i, $frame := .Stack.Frames}}
|
|
---
|
|
{{$i}}: {{$frame.Function}}
|
|
Package: {{$frame.Package}}
|
|
File: {{$frame.File}}@{{$frame.Line}}{{end}}
|
|
`))
|
|
|
|
func newGenericError(err error, c ErrorCode) Error {
|
|
if le, ok := err.(Error); ok {
|
|
return le
|
|
}
|
|
gerr := &genericError{
|
|
Timestamp: time.Now(),
|
|
Err: err,
|
|
ECode: c,
|
|
Stack: stacktrace.Capture(1),
|
|
}
|
|
if err != nil {
|
|
gerr.Message = err.Error()
|
|
}
|
|
return gerr
|
|
}
|
|
|
|
func newSystemError(err error) Error {
|
|
if le, ok := err.(Error); ok {
|
|
return le
|
|
}
|
|
gerr := &genericError{
|
|
Timestamp: time.Now(),
|
|
Err: err,
|
|
ECode: SystemError,
|
|
Stack: stacktrace.Capture(1),
|
|
}
|
|
if err != nil {
|
|
gerr.Message = err.Error()
|
|
}
|
|
return gerr
|
|
}
|
|
|
|
type genericError struct {
|
|
Timestamp time.Time
|
|
ECode ErrorCode
|
|
Err error `json:"-"`
|
|
Message string
|
|
Stack stacktrace.Stacktrace
|
|
}
|
|
|
|
func (e *genericError) Error() string {
|
|
return e.Message
|
|
}
|
|
|
|
func (e *genericError) Code() ErrorCode {
|
|
return e.ECode
|
|
}
|
|
|
|
func (e *genericError) Detail(w io.Writer) error {
|
|
return errorTemplate.Execute(w, e)
|
|
}
|