| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package semconv // import "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
const pkg = "go.opentelemetry.io/otel/semconv/v1.40.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, errors.Join(custom("left"), custom("right")), "left") // first errors.As match is used. |
| 18 |
check(t, custom(""), pkg+".ErrCustomType") // empty ErrorType, use concrete type. |
| 19 |
check(t, wrapped(custom("wrapped-aborted")), "wrapped-aborted") |
| 20 |
check(t, wrapped(custom("")), pkg+".wrappedErr") // empty ErrorType in chain, use concrete top-level type. |
| 21 |
} |
| 22 |
|
| 23 |
func check(t *testing.T, err error, want string) { |
| 24 |
t.Helper() |
| 25 |
got := ErrorType(err) |
| 26 |
if got.Key != ErrorTypeKey { |
| 27 |
t.Errorf("ErrorType(%v) key = %v, want %v", err, got.Key, ErrorTypeKey) |
| 28 |
} |
| 29 |
if got.Value.AsString() != want { |
| 30 |
t.Errorf("ErrorType(%v) value = %v, want %v", err, got.Value.AsString(), want) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func custom(typ string) error { |
| 35 |
return ErrCustomType{Type: typ} |
| 36 |
} |
| 37 |
|
| 38 |
func wrapped(err error) error { |
| 39 |
return wrappedErr{err: err} |
| 40 |
} |
| 41 |
|
| 42 |
type ErrCustomType struct { |
| 43 |
Type string |
| 44 |
} |
| 45 |
|
| 46 |
func (e ErrCustomType) Error() string { |
| 47 |
return "custom: " + e.Type |
| 48 |
} |
| 49 |
|
| 50 |
func (e ErrCustomType) ErrorType() string { |
| 51 |
return e.Type |
| 52 |
} |
| 53 |
|
| 54 |
type wrappedErr struct { |
| 55 |
err error |
| 56 |
} |
| 57 |
|
| 58 |
func (e wrappedErr) Error() string { |
| 59 |
return "wrapped: " + e.err.Error() |
| 60 |
} |
| 61 |
|
| 62 |
func (e wrappedErr) Unwrap() error { |
| 63 |
return e.err |
| 64 |
} |