From b3f907b19beb280c79630ac164549159718dbf8c Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Fri, 14 Aug 2015 06:42:12 -0700 Subject: [PATCH] Add JSON() printing for error and fatal messages --- error.go | 27 +++++++++++++++++++++++++++ ls.go | 4 ++-- print-structs.go | 25 ++++++++++++++++++++++--- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/error.go b/error.go index 934e4462..702182e0 100644 --- a/error.go +++ b/error.go @@ -17,6 +17,9 @@ package main import ( + "os" + "reflect" + "github.com/minio/mc/internal/github.com/minio/minio/pkg/probe" "github.com/minio/mc/pkg/console" ) @@ -26,6 +29,18 @@ func fatalIf(err *probe.Error) { if err == nil { return } + if globalJSONFlag { + errorMessage := ErrorMessage{ + Type: "Fatal", + TypedError: reflect.TypeOf(err.ToError()).String(), + EmbeddedError: err.ToError(), + } + if globalDebugFlag { + errorMessage.ProbeError = err.JSON() + } + console.Println(errorMessage) + os.Exit(1) + } if !globalDebugFlag { console.Fatalln(err.ToError()) } @@ -37,6 +52,18 @@ func errorIf(err *probe.Error) { if err == nil { return } + if globalJSONFlag { + errorMessage := ErrorMessage{ + Type: "Error", + TypedError: reflect.TypeOf(err.ToError()).String(), + EmbeddedError: err.ToError(), + } + if globalDebugFlag { + errorMessage.ProbeError = err.JSON() + } + console.Println(errorMessage) + return + } if !globalDebugFlag { console.Errorln(err.ToError()) return diff --git a/ls.go b/ls.go index bca04734..addbbe76 100644 --- a/ls.go +++ b/ls.go @@ -35,8 +35,8 @@ const ( ) // parseContent parse client Content container into printer struct -func parseContent(c *client.Content) Content { - content := Content{} +func parseContent(c *client.Content) ContentMessage { + content := ContentMessage{} content.Time = c.Time.Local().Format(printDate) // guess file type diff --git a/print-structs.go b/print-structs.go index 416d6f28..cc39d957 100644 --- a/print-structs.go +++ b/print-structs.go @@ -27,6 +27,25 @@ import ( "github.com/minio/mc/pkg/console" ) +// ErrorMessage json container for error messages +type ErrorMessage struct { + Type string `json:"type"` + TypedError string `json:"typed-error"` + EmbeddedError error `json:"embedded-error"` + ProbeError string `json:"probe-error"` +} + +func (e ErrorMessage) String() string { + if !globalJSONFlag { + return e.EmbeddedError.Error() + } + 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"` @@ -87,8 +106,8 @@ func (s sessionV2) String() string { return console.JSON(string(sessionJSONBytes) + "\n") } -// Content container for content message structure -type Content struct { +// ContentMessage container for content message structure +type ContentMessage struct { Filetype string `json:"type"` Time string `json:"last-modified"` Size string `json:"size"` @@ -96,7 +115,7 @@ type Content struct { } // String string printer for Content metadata -func (c Content) String() string { +func (c ContentMessage) String() string { if !globalJSONFlag { message := console.Time("[%s] ", c.Time) message = message + console.Size("%6s ", c.Size)