From c50bc9c22ff90e902dc1a8cecc6d26e2ddb247ab Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Wed, 19 Aug 2015 01:27:40 -0700 Subject: [PATCH] error handling updates --- error.go | 37 +++++++++++++++++-- .../github.com/minio/minio/pkg/probe/probe.go | 20 +++++----- internal/vendor.json | 4 +- print-structs.go | 26 ------------- 4 files changed, 45 insertions(+), 42 deletions(-) diff --git a/error.go b/error.go index 03f7ecbc..e99de18d 100644 --- a/error.go +++ b/error.go @@ -17,6 +17,7 @@ package main import ( + "encoding/json" "os" "github.com/minio/mc/internal/github.com/minio/minio/pkg/probe" @@ -29,7 +30,22 @@ func fatalIf(err *probe.Error) { return } if globalJSONFlag { - console.Println(ErrorMessage{err}) + errorMessage := struct { + // Error string `json:"error"` + Cause error `json:"cause"` + Type string `json:"type"` + CallTrace []probe.TracePoint `json:"trace,omitempty"` + SysInfo map[string]string `json:"sysinfo"` + }{ + Type: "Fatal", + Cause: err.ToGoError(), + SysInfo: err.SysInfo, + } + if globalDebugFlag { + errorMessage.CallTrace = err.CallTrace + } + json, _ := json.Marshal(errorMessage) + console.Println(string(json)) os.Exit(1) } if !globalDebugFlag { @@ -44,13 +60,26 @@ func errorIf(err *probe.Error) { return } if globalJSONFlag { - errorMessage := ErrorMessage{err} - console.Println(errorMessage) + errorMessage := struct { + // Error string `json:"error"` + Cause error `json:"cause"` + Type string `json:"type"` + CallTrace []probe.TracePoint `json:"trace,omitempty"` + SysInfo map[string]string `json:"sysinfo"` + }{ + Type: "Error", + Cause: err.ToGoError(), + SysInfo: err.SysInfo, + } + if globalDebugFlag { + errorMessage.CallTrace = err.CallTrace + } + json, _ := json.Marshal(errorMessage) + console.Println(string(json)) return } if !globalDebugFlag { console.Errorln(err.ToGoError()) - return } console.Errorln(err) } diff --git a/internal/github.com/minio/minio/pkg/probe/probe.go b/internal/github.com/minio/minio/pkg/probe/probe.go index d1cfb8f5..666cde77 100644 --- a/internal/github.com/minio/minio/pkg/probe/probe.go +++ b/internal/github.com/minio/minio/pkg/probe/probe.go @@ -50,18 +50,18 @@ func GetSysInfo() map[string]string { } } -type tracePoint struct { - Line int `json:"Line"` - Filename string `json:"File"` - Function string `json:"Func"` - Env map[string][]string `json:"Env"` +type TracePoint struct { + Line int `json:"Line,omitempty"` + Filename string `json:"File,omitempty"` + Function string `json:"Func,omitempty"` + Env map[string][]string `json:"Env,omitempty"` } // Error implements tracing error functionality. type Error struct { lock sync.RWMutex Cause error `json:"cause,omitempty"` - CallTrace []tracePoint `json:"trace,omitempty"` + CallTrace []TracePoint `json:"trace,omitempty"` SysInfo map[string]string `json:"sysinfo,omitempty"` } @@ -73,7 +73,7 @@ func NewError(e error) *Error { if e == nil { return nil } - Err := Error{lock: sync.RWMutex{}, Cause: e, CallTrace: []tracePoint{}, SysInfo: GetSysInfo()} + Err := Error{lock: sync.RWMutex{}, Cause: e, CallTrace: []TracePoint{}, SysInfo: GetSysInfo()} return Err.trace() } @@ -97,11 +97,11 @@ func (e *Error) trace(fields ...string) *Error { function := runtime.FuncForPC(pc).Name() _, function = filepath.Split(function) file = "..." + strings.TrimPrefix(file, os.Getenv("GOPATH")) // trim gopathSource from file - tp := tracePoint{} + tp := TracePoint{} if len(fields) > 0 { - tp = tracePoint{Line: line, Filename: file, Function: function, Env: map[string][]string{"Tags": fields}} + tp = TracePoint{Line: line, Filename: file, Function: function, Env: map[string][]string{"Tags": fields}} } else { - tp = tracePoint{Line: line, Filename: file, Function: function} + tp = TracePoint{Line: line, Filename: file, Function: function} } e.CallTrace = append(e.CallTrace, tp) return e diff --git a/internal/vendor.json b/internal/vendor.json index e3758f67..ec808f79 100755 --- a/internal/vendor.json +++ b/internal/vendor.json @@ -48,8 +48,8 @@ "canonical": "github.com/minio/minio/pkg/probe", "comment": "", "local": "github.com/minio/minio/pkg/probe", - "revision": "8789c5a49d3212d38ee42b36eec6eaeea45d6778", - "revisionTime": "2015-08-18T23:52:04-07:00" + "revision": "f223c8b36d4553962dd6a0ab587f2db82dc367f7", + "revisionTime": "2015-08-19T01:23:59-07:00" }, { "canonical": "github.com/minio/minio/pkg/quick", diff --git a/print-structs.go b/print-structs.go index d62602ba..00932dfd 100644 --- a/print-structs.go +++ b/print-structs.go @@ -24,35 +24,9 @@ import ( "strings" "time" - "github.com/minio/mc/internal/github.com/minio/minio/pkg/probe" "github.com/minio/mc/pkg/console" ) -// ErrorMessage json container for error messages -type ErrorMessage struct { - *probe.Error -} - -func (e ErrorMessage) String() string { - if !globalJSONFlag { - return e.Cause.Error() - } - if !globalDebugFlag { - errorMessageBytes, err := json.MarshalIndent(struct { - Cause error `json:"cause"` - }{Cause: e.Cause}, "", "\t") - if err != nil { - panic(err) - } - return console.JSON(string(errorMessageBytes) + "\n") - } - errorMessageBytes, err := json.MarshalIndent(e, "", "\t") - if err != nil { - panic(err) - } - return console.JSON(string(errorMessageBytes) + "\n") -} - // DiffJSONMessage json container for diff messages type DiffJSONMessage struct { FirstURL string `json:"first"`