Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/stackit/client/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ func withResponseID[T any](ctx context.Context, call func(context.Context) (T, e
resp, err := call(ctx)
if err != nil {
var zero T
err = stackiterrors.WrapError(err, "trace-id", runtime.GetTraceId(ctx))
if httpResp != nil {
reqID := httpResp.Header.Get(sdkWait.XRequestIDHeader)
return zero, stackiterrors.WrapErrorWithResponseID(err, reqID)
err = stackiterrors.WrapErrorWithResponseID(err, reqID)
}
return zero, err
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/stackit/client/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package client

import (
"context"
"errors"
"net/http"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
sdkconfig "github.com/stackitcloud/stackit-sdk-go/core/config"
)

var _ = Describe("withResponseID", func() {
It("wraps API errors with trace and request IDs", func() {
_, err := withResponseID(context.Background(), func(ctx context.Context) (int, error) {
response, ok := ctx.Value(sdkconfig.ContextHTTPResponse).(**http.Response)
Expect(ok).To(BeTrue())
*response = &http.Response{Header: http.Header{
"X-Trace-Id": {"trace-123"},
"X-Request-Id": {"request-456"},
}}
return 0, errors.New("api error")
})

Expect(err).To(MatchError("[X-Request-Id:request-456]: [trace-id:trace-123]: api error"))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would format the key in a similar fashion. Now we have the request ID with upper and lowercase + the X- upfront, while the trace-id is all lowercase and without the prefix.
We should keep one scheme

@nschad nschad Sep 15, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree maybe we should switch the format to something like structured logging? I would prefer if we just could do a.logger.WithValues("Trace-ID", "128382189429")

But this will probably be an extra story.

})
})
14 changes: 9 additions & 5 deletions pkg/stackit/stackiterrors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,20 @@ func IgnoreNotFound(err error) error {
return err
}

// WrapErrorWithResponseID wraps the error with the X-Request-Id but only if the error is not nil
func WrapErrorWithResponseID(err error, reqID string) error {
// WrapError wraps the error with an identifier but only if the error is not nil.
func WrapError(err error, name, id string) error {
if err == nil {
return nil
}
// if the request id is empty we don't wrap the error
if reqID == "" {
if id == "" {
return err
}
return fmt.Errorf("[%s:%s]: %w", wait.XRequestIDHeader, reqID, err)
return fmt.Errorf("[%s:%s]: %w", name, id, err)
}

// WrapErrorWithResponseID wraps the error with the X-Request-Id.
func WrapErrorWithResponseID(err error, reqID string) error {
return WrapError(err, wait.XRequestIDHeader, reqID)
}

func IsInvalidError(err error) bool {
Expand Down
17 changes: 17 additions & 0 deletions pkg/stackit/stackiterrors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ var _ = Describe("Errors", func() {
})
})

Describe("WrapError", func() {
It("wraps the error with the provided identifier", func() {
err := errors.New("test error")
expected := fmt.Errorf("[trace-id:12345]: %w", err)
Expect(WrapError(err, "trace-id", "12345")).To(Equal(expected))
})

It("returns the original error when the identifier is empty", func() {
err := errors.New("test error")
Expect(WrapError(err, "trace-id", "")).To(Equal(err))
})

It("returns nil when the error is nil", func() {
Expect(WrapError(nil, "trace-id", "12345")).To(Succeed())
})
})

Describe("IsInvalidError", func() {
Context("when error is a BadRequest error", func() {
It("should return true", func() {
Expand Down