| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package semconv // import "go.opentelemetry.io/otel/semconv/v1.36.0" |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"reflect" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel/attribute" |
| 13 |
) |
| 14 |
|
| 15 |
type CustomError struct{} |
| 16 |
|
| 17 |
func (CustomError) Error() string { |
| 18 |
return "custom error" |
| 19 |
} |
| 20 |
|
| 21 |
func TestErrorType(t *testing.T) { |
| 22 |
customErr := CustomError{} |
| 23 |
builtinErr := errors.New("something went wrong") |
| 24 |
var nilErr error |
| 25 |
|
| 26 |
wantCustomType := reflect.TypeFor[CustomError]() |
| 27 |
wantCustomStr := fmt.Sprintf("%s.%s", wantCustomType.PkgPath(), wantCustomType.Name()) |
| 28 |
|
| 29 |
tests := []struct { |
| 30 |
name string |
| 31 |
err error |
| 32 |
want attribute.KeyValue |
| 33 |
}{ |
| 34 |
{ |
| 35 |
name: "BuiltinError", |
| 36 |
err: builtinErr, |
| 37 |
want: attribute.String("error.type", "*errors.errorString"), |
| 38 |
}, |
| 39 |
{ |
| 40 |
name: "CustomError", |
| 41 |
err: customErr, |
| 42 |
want: attribute.String("error.type", wantCustomStr), |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "NilError", |
| 46 |
err: nilErr, |
| 47 |
want: ErrorTypeOther, |
| 48 |
}, |
| 49 |
} |
| 50 |
|
| 51 |
for _, tt := range tests { |
| 52 |
t.Run(tt.name, func(t *testing.T) { |
| 53 |
got := ErrorType(tt.err) |
| 54 |
if got != tt.want { |
| 55 |
t.Errorf("ErrorType(%v) = %v, want %v", tt.err, got, tt.want) |
| 56 |
} |
| 57 |
}) |
| 58 |
} |
| 59 |
} |