|
| 1 | +// The MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2022 Temporal Technologies Inc. All rights reserved. |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in |
| 13 | +// all copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +// THE SOFTWARE. |
| 22 | + |
| 23 | +package serviceerror_test |
| 24 | + |
| 25 | +import ( |
| 26 | + "errors" |
| 27 | + "testing" |
| 28 | + |
| 29 | + "github.com/gogo/googleapis/google/rpc" |
| 30 | + "github.com/gogo/protobuf/types" |
| 31 | + "github.com/gogo/status" |
| 32 | + "github.com/stretchr/testify/require" |
| 33 | + "google.golang.org/grpc/codes" |
| 34 | + |
| 35 | + "go.temporal.io/api/errordetails/v1" |
| 36 | + "go.temporal.io/api/serviceerror" |
| 37 | +) |
| 38 | + |
| 39 | +func TestFromStatus_NotFound(t *testing.T) { |
| 40 | + var err error |
| 41 | + st1 := status.New(codes.NotFound, "Not found.") |
| 42 | + err1 := serviceerror.FromStatus(st1) |
| 43 | + require.IsType(t, &serviceerror.NotFound{}, err1) |
| 44 | + require.Equal(t, codes.NotFound, err1.(*serviceerror.NotFound).Status().Code()) |
| 45 | + require.Equal(t, "Not found.", err1.(*serviceerror.NotFound).Message) |
| 46 | + |
| 47 | + st2 := status.New(codes.NotFound, "Not found.") |
| 48 | + st2, err = st1.WithDetails(&errordetails.NamespaceNotFoundFailure{ |
| 49 | + Namespace: "test-ns", |
| 50 | + }) |
| 51 | + require.NoError(t, err) |
| 52 | + err2 := serviceerror.FromStatus(st2) |
| 53 | + require.IsType(t, &serviceerror.NamespaceNotFound{}, err2) |
| 54 | + require.Equal(t, codes.NotFound, err2.(*serviceerror.NamespaceNotFound).Status().Code()) |
| 55 | + require.Equal(t, "Not found.", err2.(*serviceerror.NamespaceNotFound).Message) |
| 56 | + require.Equal(t, "test-ns", err2.(*serviceerror.NamespaceNotFound).Namespace) |
| 57 | +} |
| 58 | + |
| 59 | +func TestFromStatus_UnknownErrorDetails(t *testing.T) { |
| 60 | + st1 := status.FromProto(&rpc.Status{ |
| 61 | + Code: int32(codes.NotFound), |
| 62 | + Message: "Not found.", |
| 63 | + Details: []*types.Any{{TypeUrl: "type.googleapis.com/some.unknown.Type"}}, |
| 64 | + }) |
| 65 | + |
| 66 | + err1 := serviceerror.FromStatus(st1) |
| 67 | + require.IsType(t, &serviceerror.NotFound{}, err1) |
| 68 | + require.Equal(t, codes.NotFound, err1.(*serviceerror.NotFound).Status().Code()) |
| 69 | + require.Equal(t, "Not found.", err1.(*serviceerror.NotFound).Message) |
| 70 | +} |
| 71 | + |
| 72 | +func TestToStatus_UnknownErrorDetails(t *testing.T) { |
| 73 | + err1 := status.ErrorProto(&rpc.Status{ |
| 74 | + Code: int32(codes.NotFound), |
| 75 | + Message: "Not found.", |
| 76 | + Details: []*types.Any{{TypeUrl: "type.googleapis.com/some.unknown.Type"}}, |
| 77 | + }) |
| 78 | + |
| 79 | + st1 := serviceerror.ToStatus(err1) |
| 80 | + require.Equal(t, codes.NotFound, st1.Code()) |
| 81 | + require.Equal(t, "Not found.", st1.Message()) |
| 82 | + require.Len(t, st1.Details(), 1) |
| 83 | + require.Equal(t, "type.googleapis.com/some.unknown.Type", st1.Proto().Details[0].TypeUrl) |
| 84 | +} |
| 85 | + |
| 86 | +func TestToStatus_NotServiceError(t *testing.T) { |
| 87 | + err1 := errors.New("some error") |
| 88 | + st1 := serviceerror.ToStatus(err1) |
| 89 | + require.Equal(t, codes.Unknown, st1.Code()) |
| 90 | + require.Equal(t, "some error", st1.Message()) |
| 91 | + require.Len(t, st1.Details(), 0) |
| 92 | +} |
0 commit comments