From 3d89aedbc35a373fd198e7642ee1d01a86e2f024 Mon Sep 17 00:00:00 2001 From: Jeff Malnick Date: Mon, 5 Oct 2020 20:10:46 -0700 Subject: [PATCH] feat: make CLI error handling more clear --- api/apierror.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/api/apierror.go b/api/apierror.go index 146e638189..9179134bef 100644 --- a/api/apierror.go +++ b/api/apierror.go @@ -28,27 +28,24 @@ func AsServerError(in error) *Error { // Error satisfies the error interface. func (e *Error) Error() string { - res := fmt.Sprintf("Status: %d, Code: %q, Error: %q", e.Status, e.Code, e.Message) - var dets []string + msg := []string{fmt.Sprintf("%s\n", e.Message), fmt.Sprintf(" %d, %s\n\n", e.Status, e.Code)} + if e.Details != nil { if e.Details.ErrorId != "" { - dets = append(dets, fmt.Sprintf("error_id: %q", e.Details.ErrorId)) + msg = append(msg, fmt.Sprintf(" Error ID: %s\n", e.Details.ErrorId)) } if e.Details.RequestId != "" { - dets = append(dets, fmt.Sprintf("request_id: %q", e.Details.RequestId)) + msg = append(msg, fmt.Sprintf(" Request ID: %s\n", e.Details.RequestId)) } if e.Details.TraceId != "" { - dets = append(dets, fmt.Sprintf("TraceId: %q", e.Details.TraceId)) + msg = append(msg, fmt.Sprintf(" Trace ID: %s\n", e.Details.TraceId)) } for _, rf := range e.Details.RequestFields { - dets = append(dets, fmt.Sprintf("request_field: {name: %q, desc: %q}", rf.Name, rf.Description)) + msg = append(msg, fmt.Sprintf(" '-%s': %s\n", strings.ReplaceAll(rf.Name, "_", "-"), rf.Description)) } } - if len(dets) > 0 { - det := strings.Join(dets, ", ") - res = fmt.Sprintf("%s, Details: {%s}", res, det) - } - return res + + return strings.Join(msg, "") } // Errors are considered the same iff they are both api.Errors and their statuses are the same.