Skip to content

Commit ad5417f

Browse files
authored
feat: include action ID in action error string (#539)
This allows us to easily debug the failed action from user provided logs.
1 parent 07727d3 commit ad5417f

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

hcloud/action.go

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ func (e ActionError) Action() *Action {
6464
}
6565

6666
func (e ActionError) Error() string {
67+
action := e.Action()
68+
if action != nil {
69+
// For easier debugging, the error string contains the Action ID.
70+
return fmt.Sprintf("%s (%s, %d)", e.Message, e.Code, action.ID)
71+
}
6772
return fmt.Sprintf("%s (%s)", e.Message, e.Code)
6873
}
6974

hcloud/action_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,22 @@ import (
77
"testing"
88
"time"
99

10+
"github.com/stretchr/testify/assert"
11+
1012
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
1113
)
1214

15+
func TestActionError(t *testing.T) {
16+
assert.Equal(t,
17+
"action failed (failed)",
18+
ActionError{Code: "failed", Message: "action failed"}.Error(),
19+
)
20+
assert.Equal(t,
21+
"action failed (failed, 12345)",
22+
ActionError{Code: "failed", Message: "action failed", action: &Action{ID: 12345}}.Error(),
23+
)
24+
}
25+
1326
func TestActionClientGetByID(t *testing.T) {
1427
env := newTestEnv()
1528
defer env.Teardown()

0 commit comments

Comments
 (0)