generic_error.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package libcontainer
  2. import (
  3. "io"
  4. "text/template"
  5. "time"
  6. "github.com/opencontainers/runc/libcontainer/stacktrace"
  7. )
  8. type syncType uint8
  9. const (
  10. procReady syncType = iota
  11. procError
  12. procRun
  13. procHooks
  14. procResume
  15. )
  16. type syncT struct {
  17. Type syncType `json:"type"`
  18. }
  19. var errorTemplate = template.Must(template.New("error").Parse(`Timestamp: {{.Timestamp}}
  20. Code: {{.ECode}}
  21. {{if .Message }}
  22. Message: {{.Message}}
  23. {{end}}
  24. Frames:{{range $i, $frame := .Stack.Frames}}
  25. ---
  26. {{$i}}: {{$frame.Function}}
  27. Package: {{$frame.Package}}
  28. File: {{$frame.File}}@{{$frame.Line}}{{end}}
  29. `))
  30. func newGenericError(err error, c ErrorCode) Error {
  31. if le, ok := err.(Error); ok {
  32. return le
  33. }
  34. gerr := &genericError{
  35. Timestamp: time.Now(),
  36. Err: err,
  37. ECode: c,
  38. Stack: stacktrace.Capture(1),
  39. }
  40. if err != nil {
  41. gerr.Message = err.Error()
  42. }
  43. return gerr
  44. }
  45. func newSystemError(err error) Error {
  46. if le, ok := err.(Error); ok {
  47. return le
  48. }
  49. gerr := &genericError{
  50. Timestamp: time.Now(),
  51. Err: err,
  52. ECode: SystemError,
  53. Stack: stacktrace.Capture(1),
  54. }
  55. if err != nil {
  56. gerr.Message = err.Error()
  57. }
  58. return gerr
  59. }
  60. type genericError struct {
  61. Timestamp time.Time
  62. ECode ErrorCode
  63. Err error `json:"-"`
  64. Message string
  65. Stack stacktrace.Stacktrace
  66. }
  67. func (e *genericError) Error() string {
  68. return e.Message
  69. }
  70. func (e *genericError) Code() ErrorCode {
  71. return e.ECode
  72. }
  73. func (e *genericError) Detail(w io.Writer) error {
  74. return errorTemplate.Execute(w, e)
  75. }