Debian Codegraph @

semconv/v1.38.0/error_type_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package semconv // import "go.opentelemetry.io/otel/semconv/v1.38.0"
5
6 import (
7 "errors"
8 "testing"
9 )
10
11 const pkg = "go.opentelemetry.io/otel/semconv/v1.38.0"
12
13 func TestErrorType(t *testing.T) {
14 check(t, nil, ErrorTypeOther.Value.AsString())
15 check(t, errors.New("msg"), "*errors.errorString")
16 check(t, custom("aborted"), "aborted")
17 check(t, custom(""), pkg+".ErrCustomType") // empty ErrorType, use concrete type.
18 }
19
20 func check(t *testing.T, err error, want string) {
21 t.Helper()
22 got := ErrorType(err)
23 if got.Key != ErrorTypeKey {
24 t.Errorf("ErrorType(%v) key = %v, want %v", err, got.Key, ErrorTypeKey)
25 }
26 if got.Value.AsString() != want {
27 t.Errorf("ErrorType(%v) value = %v, want %v", err, got.Value.AsString(), want)
28 }
29 }
30
31 func custom(typ string) error {
32 return ErrCustomType{Type: typ}
33 }
34
35 type ErrCustomType struct {
36 Type string
37 }
38
39 func (e ErrCustomType) Error() string {
40 return "custom: " + e.Type
41 }
42
43 func (e ErrCustomType) ErrorType() string {
44 return e.Type
45 }