From 43a0e31819c3427d94466c6065da9cd251bb6bb7 Mon Sep 17 00:00:00 2001 From: jo Date: Mon, 21 Oct 2024 11:46:01 +0200 Subject: [PATCH 1/3] feat: include action ID in action error string This allows us to easily debug the failed action from user provided logs. --- hcloud/action.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/hcloud/action.go b/hcloud/action.go index b15b1b74..cbbc0965 100644 --- a/hcloud/action.go +++ b/hcloud/action.go @@ -64,6 +64,10 @@ func (e ActionError) Action() *Action { } func (e ActionError) Error() string { + if e.action != nil { + // For easier debugging, the error string contains the Action ID. + return fmt.Sprintf("%s (%s, %d)", e.Message, e.Code, e.action.ID) + } return fmt.Sprintf("%s (%s)", e.Message, e.Code) } From 9e83a5396695b490dd9fdc70f6befc9ff4a03144 Mon Sep 17 00:00:00 2001 From: jo Date: Mon, 21 Oct 2024 11:48:03 +0200 Subject: [PATCH 2/3] refactor: use existing getter --- hcloud/action.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/hcloud/action.go b/hcloud/action.go index cbbc0965..e75cf027 100644 --- a/hcloud/action.go +++ b/hcloud/action.go @@ -64,9 +64,10 @@ func (e ActionError) Action() *Action { } func (e ActionError) Error() string { - if e.action != nil { + action := e.Action() + if action != nil { // For easier debugging, the error string contains the Action ID. - return fmt.Sprintf("%s (%s, %d)", e.Message, e.Code, e.action.ID) + return fmt.Sprintf("%s (%s, %d)", e.Message, e.Code, action.ID) } return fmt.Sprintf("%s (%s)", e.Message, e.Code) } From 21e5be583739cdb8fdaf48fcbf191e3359d984c9 Mon Sep 17 00:00:00 2001 From: jo Date: Mon, 21 Oct 2024 12:08:30 +0200 Subject: [PATCH 3/3] test: add unit test --- hcloud/action_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/hcloud/action_test.go b/hcloud/action_test.go index 6bbf75a7..111e9cdb 100644 --- a/hcloud/action_test.go +++ b/hcloud/action_test.go @@ -7,9 +7,22 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + "github.com/hetznercloud/hcloud-go/v2/hcloud/schema" ) +func TestActionError(t *testing.T) { + assert.Equal(t, + "action failed (failed)", + ActionError{Code: "failed", Message: "action failed"}.Error(), + ) + assert.Equal(t, + "action failed (failed, 12345)", + ActionError{Code: "failed", Message: "action failed", action: &Action{ID: 12345}}.Error(), + ) +} + func TestActionClientGetByID(t *testing.T) { env := newTestEnv() defer env.Teardown()