Repository: hetznercloud/hcloud-go
Pull request #539 (2024-10-21 09:46:30)

feat: include action ID in action error string
This allows us to easily debug the failed action from user provided logs.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/hetznercloud/hcloud-go/pull/539?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 67.43%. Comparing base [(`07727d3`)](https://app.codecov.io/gh/hetznercloud/hcloud-go/commit/07727d3362ae3ebd0966ff09e5616afe9965b17f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud) to head [(`43a0e31`)](https://app.codecov.io/gh/hetznercloud/hcloud-go/commit/43a0e31819c3427d94466c6065da9cd251bb6bb7?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud).

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #539      +/-   ##
==========================================
- Coverage   71.44%   67.43%   -4.02%     
==========================================
  Files          46       46              
  Lines        3933     4870     +937     
==========================================
+ Hits         2810     3284     +474     
- Misses        711     1174     +463     
  Partials      412      412              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/hetznercloud/hcloud-go/pull/539?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud).


Patch:
diff --git a/hcloud/action.go b/hcloud/action.go
index b15b1b7..e75cf02 100644
--- a/hcloud/action.go
+++ b/hcloud/action.go
@@ -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)
 }
 


Test patch:
diff --git a/hcloud/action_test.go b/hcloud/action_test.go
index 6bbf75a..111e9cd 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()