| 1 |
// Copyright 2025 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package micro |
| 15 |
|
| 16 |
import ( |
| 17 |
"errors" |
| 18 |
"testing" |
| 19 |
) |
| 20 |
|
| 21 |
func TestNATSErrorUnwrap(t *testing.T) { |
| 22 |
baseErr := errors.New("base error") |
| 23 |
natsErr := &NATSError{ |
| 24 |
Subject: "test.subject", |
| 25 |
Description: "test description", |
| 26 |
err: baseErr, |
| 27 |
} |
| 28 |
|
| 29 |
unwrapped := errors.Unwrap(natsErr) |
| 30 |
if unwrapped != baseErr { |
| 31 |
t.Errorf("Unwrap() = %v, want %v", unwrapped, baseErr) |
| 32 |
} |
| 33 |
|
| 34 |
// Test with nil underlying error |
| 35 |
natsErr2 := &NATSError{ |
| 36 |
Subject: "test.subject", |
| 37 |
Description: "test description", |
| 38 |
} |
| 39 |
if errors.Unwrap(natsErr2) != nil { |
| 40 |
t.Errorf("Unwrap() with nil err should return nil") |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func TestNATSErrorIs(t *testing.T) { |
| 45 |
natsErr1 := &NATSError{ |
| 46 |
Subject: "test.subject", |
| 47 |
Description: "test description", |
| 48 |
} |
| 49 |
|
| 50 |
natsErr2 := &NATSError{ |
| 51 |
Subject: "test.subject", |
| 52 |
Description: "test description", |
| 53 |
} |
| 54 |
|
| 55 |
natsErr3 := &NATSError{ |
| 56 |
Subject: "different.subject", |
| 57 |
Description: "test description", |
| 58 |
} |
| 59 |
|
| 60 |
natsErr4 := &NATSError{ |
| 61 |
Subject: "test.subject", |
| 62 |
Description: "different description", |
| 63 |
} |
| 64 |
|
| 65 |
// Test equality |
| 66 |
if !errors.Is(natsErr1, natsErr2) { |
| 67 |
t.Errorf("Is() = false, want true for equal errors") |
| 68 |
} |
| 69 |
|
| 70 |
// different subject |
| 71 |
if errors.Is(natsErr1, natsErr3) { |
| 72 |
t.Errorf("Is() = true, want false for different subjects") |
| 73 |
} |
| 74 |
|
| 75 |
// different description |
| 76 |
if errors.Is(natsErr1, natsErr4) { |
| 77 |
t.Errorf("Is() = true, want false for different descriptions") |
| 78 |
} |
| 79 |
|
| 80 |
// non NATSError |
| 81 |
otherErr := errors.New("other error") |
| 82 |
if errors.Is(natsErr1, otherErr) { |
| 83 |
t.Errorf("Is() = true, want false for non-NATSError") |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func TestNATSErrorWrapping(t *testing.T) { |
| 88 |
// Test error chain |
| 89 |
baseErr := errors.New("connection failed") |
| 90 |
natsErr := &NATSError{ |
| 91 |
Subject: "test.subject", |
| 92 |
Description: "service error: connection failed", |
| 93 |
err: baseErr, |
| 94 |
} |
| 95 |
|
| 96 |
// Check that we can find the base error in the chain |
| 97 |
if !errors.Is(natsErr, baseErr) { |
| 98 |
t.Errorf("errors.Is() should find base error in chain") |
| 99 |
} |
| 100 |
|
| 101 |
// Test error message |
| 102 |
expectedMsg := `"test.subject": service error: connection failed` |
| 103 |
if natsErr.Error() != expectedMsg { |
| 104 |
t.Errorf("Error() = %q, want %q", natsErr.Error(), expectedMsg) |
| 105 |
} |
| 106 |
} |