feat: make CLI error handling more clear

cli-printer
Jeff Malnick 5 years ago
parent dc379ed09f
commit 3d89aedbc3

@ -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.

Loading…
Cancel
Save