|
| 1 | +// Copyright © 2026 Yu Technology Group, LLC d/b/a jitsudo |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package cli |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "google.golang.org/grpc/codes" |
| 11 | + "google.golang.org/grpc/status" |
| 12 | +) |
| 13 | + |
| 14 | +func TestFriendlyError_NonGRPC(t *testing.T) { |
| 15 | + orig := errors.New("plain error") |
| 16 | + got := friendlyError(orig) |
| 17 | + if got != orig { |
| 18 | + t.Errorf("expected same error for non-gRPC error; got %v", got) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func TestFriendlyError_NotFound(t *testing.T) { |
| 23 | + err := status.Error(codes.NotFound, "request req_123 not found") |
| 24 | + got := friendlyError(err) |
| 25 | + if got.Error() != "not found" { |
| 26 | + t.Errorf("expected %q; got %q", "not found", got.Error()) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func TestFriendlyError_StripGRPCWrapper(t *testing.T) { |
| 31 | + err := status.Error(codes.InvalidArgument, "duration must be positive") |
| 32 | + got := friendlyError(err) |
| 33 | + if got.Error() != "duration must be positive" { |
| 34 | + t.Errorf("expected raw message; got %q", got.Error()) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func TestFriendlyError_StripWorkflowPrefix(t *testing.T) { |
| 39 | + err := status.Error(codes.Internal, "workflow: state machine transition denied") |
| 40 | + got := friendlyError(err) |
| 41 | + if got.Error() != "state machine transition denied" { |
| 42 | + t.Errorf("expected prefix stripped; got %q", got.Error()) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestFriendlyError_StripStorePrefix(t *testing.T) { |
| 47 | + err := status.Error(codes.Internal, "store: database connection failed") |
| 48 | + got := friendlyError(err) |
| 49 | + if got.Error() != "database connection failed" { |
| 50 | + t.Errorf("expected prefix stripped; got %q", got.Error()) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestFriendlyError_ChainedPrefixes(t *testing.T) { |
| 55 | + // Both known prefixes should be stripped when chained (e.g. wrapped errors). |
| 56 | + err := status.Error(codes.Internal, "workflow: store: nested prefix") |
| 57 | + got := friendlyError(err) |
| 58 | + if got.Error() != "nested prefix" { |
| 59 | + t.Errorf("expected both chained prefixes stripped; got %q", got.Error()) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestFriendlyError_UnknownGRPCCode(t *testing.T) { |
| 64 | + err := status.Error(codes.PermissionDenied, "not authorized") |
| 65 | + got := friendlyError(err) |
| 66 | + if got.Error() != "not authorized" { |
| 67 | + t.Errorf("expected message for unknown code; got %q", got.Error()) |
| 68 | + } |
| 69 | +} |
0 commit comments