Skip to content

Commit

Permalink
Handle wrapped errors, add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michel-laterman committed Nov 28, 2023
1 parent 012ce50 commit ea02fff
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 9 deletions.
18 changes: 9 additions & 9 deletions internal/pkg/api/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,16 +528,16 @@ func ErrorResp(w http.ResponseWriter, r *http.Request, err error) {

if resp.StatusCode >= 500 {
if trans := apm.TransactionFromContext(r.Context()); trans != nil {
switch typ := err.(type) {
case *es.ErrElastic:
esErr := &es.ErrElastic{}
if errors.As(err, &esErr) {
trans.Context.SetLabel("error.type", "ErrElastic")
trans.Context.SetLabel("error.details.status", typ.Status)
trans.Context.SetLabel("error.details.type", typ.Type)
trans.Context.SetLabel("error.details.reason", typ.Reason)
trans.Context.SetLabel("error.details.cause.type", typ.Cause.Type)
trans.Context.SetLabel("error.details.cause.reason", typ.Cause.Reason)
default:
trans.Context.SetLabel("error.type", fmt.Sprintf("%T", typ))
trans.Context.SetLabel("error.details.status", esErr.Status)
trans.Context.SetLabel("error.details.type", esErr.Type)
trans.Context.SetLabel("error.details.reason", esErr.Reason)
trans.Context.SetLabel("error.details.cause.type", esErr.Cause.Type)
trans.Context.SetLabel("error.details.cause.reason", esErr.Cause.Reason)
} else {
trans.Context.SetLabel("error.type", fmt.Sprintf("%T", err))
}
}
apm.CaptureError(r.Context(), err).Send()
Expand Down
90 changes: 90 additions & 0 deletions internal/pkg/api/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package api

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/elastic/fleet-server/v7/internal/pkg/es"
testlog "github.com/elastic/fleet-server/v7/internal/pkg/testing/log"
"github.com/stretchr/testify/require"
"go.elastic.co/apm/v2"
"go.elastic.co/apm/v2/apmtest"
)

func Test_ErrorResp(t *testing.T) {
tests := []struct {
name string
err error
expectedTags map[string]interface{}
}{{
name: "generic error",
err: fmt.Errorf("generic error"),
}, {
name: "elastic error",
err: &es.ErrElastic{},
expectedTags: map[string]interface{}{
"error_type": "ErrElastic",
},
}, {
name: "wrapped elastic error",
err: fmt.Errorf("wrapped error: %w", &es.ErrElastic{}),
expectedTags: map[string]interface{}{
"error_type": "ErrElastic",
},
}}

tracer := apmtest.NewRecordingTracer()

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
logger := testlog.SetLogger(t)
tracer.ResetPayloads()

tx := tracer.StartTransaction("test", "test")
ctx := apm.ContextWithTransaction(context.Background(), tx)
ctx = logger.WithContext(ctx)

wr := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, "GET", "http://localhost", nil)
require.NoError(t, err)

ErrorResp(wr, req, tc.err)
tx.End()
ch := make(chan struct{}, 1)
tracer.Flush(ch)

payloads := tracer.Payloads()
require.Len(t, payloads.Transactions, 1)
require.Len(t, payloads.Errors, 1)

tags := make(map[string]interface{})
for _, tag := range payloads.Transactions[0].Context.Tags {
tags[tag.Key] = tag.Value
}
for k, v := range tc.expectedTags {
require.Contains(t, tags, k, "expected tag is missing")
require.Equal(t, v, tags[k], "expected tag value does not match")
}
})
}
}

func Test_ErrorResp_NoTransaction(t *testing.T) {
tracer := apmtest.NewRecordingTracer()
ctx := testlog.SetLogger(t).WithContext(context.Background())

wr := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, "GET", "http://localhost", nil)
require.NoError(t, err)

ErrorResp(wr, req, fmt.Errorf("some error"))
ch := make(chan struct{}, 1)
tracer.Flush(ch)

payloads := tracer.Payloads()
require.Len(t, payloads.Transactions, 0)
require.Len(t, payloads.Errors, 0)
}

0 comments on commit ea02fff

Please sign in to comment.