Skip to content

Commit 63a2213

Browse files
committed
feat: unwrap errors
Formerly the otelgqlgen reports always the errors' type is gqlerror.Error, but now it reports the underlying errors' type.
1 parent 9edf201 commit 63a2213

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

test/resolvers/main.resolvers.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/resolvers/resolver.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ package resolvers
55
// It serves as dependency injection for your app, add any dependencies you require here.
66

77
type Resolver struct{}
8+
9+
type ForbiddenError struct{}
10+
11+
func (ForbiddenError) Error() string {
12+
return "forbidden"
13+
}

test/tracer_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ func TestTracer(t *testing.T) {
285285
Name: semconv.ExceptionEventName,
286286
Attributes: []attribute.KeyValue{
287287
attribute.String("graphql.errors.path", "user"),
288-
semconv.ExceptionTypeKey.String("*gqlerror.Error"),
289-
semconv.ExceptionMessageKey.String("input: user forbidden"),
288+
semconv.ExceptionTypeKey.String("github.com/aereal/otelgqlgen/test/resolvers.ForbiddenError"),
289+
semconv.ExceptionMessageKey.String("forbidden"),
290290
attrStacktrace,
291291
},
292292
},
@@ -301,8 +301,8 @@ func TestTracer(t *testing.T) {
301301
Name: semconv.ExceptionEventName,
302302
Attributes: []attribute.KeyValue{
303303
attribute.String("graphql.errors.path", "user"),
304-
semconv.ExceptionTypeKey.String("*gqlerror.Error"),
305-
semconv.ExceptionMessageKey.String("input: user forbidden"),
304+
semconv.ExceptionTypeKey.String("github.com/aereal/otelgqlgen/test/resolvers.ForbiddenError"),
305+
semconv.ExceptionMessageKey.String("forbidden"),
306306
attrStacktrace,
307307
},
308308
},
@@ -372,17 +372,17 @@ func TestTracer(t *testing.T) {
372372
Name: semconv.ExceptionEventName,
373373
Attributes: []attribute.KeyValue{
374374
attribute.String("graphql.errors.path", "user.name"),
375-
semconv.ExceptionTypeKey.String("*gqlerror.Error"),
376-
semconv.ExceptionMessageKey.String("input: user.name invalid name"),
375+
semconv.ExceptionTypeKey.String("*errors.errorString"),
376+
semconv.ExceptionMessageKey.String("invalid name"),
377377
attrStacktrace,
378378
},
379379
},
380380
{
381381
Name: semconv.ExceptionEventName,
382382
Attributes: []attribute.KeyValue{
383383
attribute.String("graphql.errors.path", "user.age"),
384-
semconv.ExceptionTypeKey.String("*gqlerror.Error"),
385-
semconv.ExceptionMessageKey.String("input: user.age invalid age"),
384+
semconv.ExceptionTypeKey.String("*errors.errorString"),
385+
semconv.ExceptionMessageKey.String("invalid age"),
386386
attrStacktrace,
387387
},
388388
},
@@ -397,17 +397,17 @@ func TestTracer(t *testing.T) {
397397
Name: semconv.ExceptionEventName,
398398
Attributes: []attribute.KeyValue{
399399
attribute.String("graphql.errors.path", "user.name"),
400-
semconv.ExceptionTypeKey.String("*gqlerror.Error"),
401-
semconv.ExceptionMessageKey.String("input: user.name invalid name"),
400+
semconv.ExceptionTypeKey.String("*errors.errorString"),
401+
semconv.ExceptionMessageKey.String("invalid name"),
402402
attrStacktrace,
403403
},
404404
},
405405
{
406406
Name: semconv.ExceptionEventName,
407407
Attributes: []attribute.KeyValue{
408408
attribute.String("graphql.errors.path", "user.age"),
409-
semconv.ExceptionTypeKey.String("*gqlerror.Error"),
410-
semconv.ExceptionMessageKey.String("input: user.age invalid age"),
409+
semconv.ExceptionTypeKey.String("*errors.errorString"),
410+
semconv.ExceptionMessageKey.String("invalid age"),
411411
attrStacktrace,
412412
},
413413
},

tracer.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,23 @@ func recordGQLErrors(span trace.Span, errs gqlerror.List) {
229229
attrs := []attribute.KeyValue{
230230
keyErrorPath.String(e.Path.String()),
231231
}
232-
span.RecordError(e, trace.WithStackTrace(true), trace.WithAttributes(attrs...))
232+
err := unwrapErr(e)
233+
span.RecordError(err, trace.WithStackTrace(true), trace.WithAttributes(attrs...))
234+
}
235+
}
236+
237+
func unwrapErr(err error) error {
238+
var underlying error = err
239+
for {
240+
wrapped, ok := underlying.(interface{ Unwrap() error })
241+
if !ok {
242+
return underlying
243+
}
244+
unwrapped := wrapped.Unwrap()
245+
if unwrapped == nil {
246+
return underlying
247+
}
248+
underlying = unwrapped
233249
}
234250
}
235251

0 commit comments

Comments
 (0)