From ffe8a1753a370de1327b0eda0b37cc9ad12ebf8e Mon Sep 17 00:00:00 2001 From: Jonas L Date: Mon, 6 May 2024 10:58:02 +0200 Subject: [PATCH] fix: improve error message format with correlation id (#430) This improves the format of our error message, as they might be printed to the users. Before: ``` hcloud: server name is already used (uniqueness_error) (Correlation ID: d5064a1f0bb9de4b) ``` After: ``` hcloud: server name is already used (uniqueness_error, d5064a1f0bb9de4b) ``` (cherry picked from commit 013477f4227bdc69f376d8f13a875b09c32171f6) --- hcloud/error.go | 2 +- hcloud/error_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/hcloud/error.go b/hcloud/error.go index 7403e420..371a92e3 100644 --- a/hcloud/error.go +++ b/hcloud/error.go @@ -104,7 +104,7 @@ func (e Error) Error() string { correlationID := resp.internalCorrelationID() if correlationID != "" { // For easier debugging, the error string contains the Correlation ID of the response. - return fmt.Sprintf("%s (%s) (Correlation ID: %s)", e.Message, e.Code, correlationID) + return fmt.Sprintf("%s (%s, %s)", e.Message, e.Code, correlationID) } } return fmt.Sprintf("%s (%s)", e.Message, e.Code) diff --git a/hcloud/error_test.go b/hcloud/error_test.go index c4816110..fb57b60d 100644 --- a/hcloud/error_test.go +++ b/hcloud/error_test.go @@ -39,13 +39,13 @@ func TestError_Error(t *testing.T) { Header: func() http.Header { headers := http.Header{} // [http.Header] requires normalized header names, easiest to do by using the Set method - headers.Set("X-Correlation-ID", "foobar") + headers.Set("X-Correlation-ID", "d5064a1f0bb9de4b") return headers }(), }, }, }, - want: "Creating image failed because of an unknown error. (unknown_error) (Correlation ID: foobar)", + want: "Creating image failed because of an unknown error. (unknown_error, d5064a1f0bb9de4b)", }, { name: "internal server error without correlation id",