From ab41de68b46a78c06c0404fe4603889f4d0b0c20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Hryniuk?= Date: Sun, 14 Feb 2021 14:00:19 +0100 Subject: [PATCH] Print details of API errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ɓukasz Hryniuk --- cmd/promtool/main.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/cmd/promtool/main.go b/cmd/promtool/main.go index 6323845e1..19f765817 100644 --- a/cmd/promtool/main.go +++ b/cmd/promtool/main.go @@ -497,8 +497,7 @@ func QueryInstant(url *url.URL, query, evalTime string, p printer) int { val, _, err := api.Query(ctx, query, eTime) // Ignoring warnings for now. cancel() if err != nil { - fmt.Fprintln(os.Stderr, "query error:", err) - return 1 + return handleAPIError(err) } p.printValue(val) @@ -572,8 +571,7 @@ func QueryRange(url *url.URL, headers map[string]string, query, start, end strin cancel() if err != nil { - fmt.Fprintln(os.Stderr, "query error:", err) - return 1 + return handleAPIError(err) } p.printValue(val) @@ -609,8 +607,7 @@ func QuerySeries(url *url.URL, matchers []string, start, end string, p printer) cancel() if err != nil { - fmt.Fprintln(os.Stderr, "query error:", err) - return 1 + return handleAPIError(err) } p.printSeries(val) @@ -648,16 +645,25 @@ func QueryLabels(url *url.URL, name string, start, end string, p printer) int { for _, v := range warn { fmt.Fprintln(os.Stderr, "query warning:", v) } - if err != nil { - fmt.Fprintln(os.Stderr, "query error:", err) - return 1 + return handleAPIError(err) } p.printLabelValues(val) return 0 } +func handleAPIError(err error) int { + var apiErr *v1.Error + if errors.As(err, &apiErr) && apiErr.Detail != "" { + fmt.Fprintf(os.Stderr, "query error: %v (detail: %s)\n", apiErr, strings.TrimSpace(apiErr.Detail)) + } else { + fmt.Fprintln(os.Stderr, "query error:", err) + } + + return 1 +} + func parseStartTimeAndEndTime(start, end string) (time.Time, time.Time, error) { var ( minTime = time.Now().Add(-9999 * time.Hour)