Skip to content

Commit

Permalink
feat: include action ID in action error string (#539)
Browse files Browse the repository at this point in the history
This allows us to easily debug the failed action from user provided
logs.
  • Loading branch information
jooola authored Oct 21, 2024
1 parent 07727d3 commit ad5417f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions hcloud/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func (e ActionError) Action() *Action {
}

func (e ActionError) Error() string {
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, action.ID)
}
return fmt.Sprintf("%s (%s)", e.Message, e.Code)
}

Expand Down
13 changes: 13 additions & 0 deletions hcloud/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit ad5417f

Please sign in to comment.