Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/public-api-server/pkg/proxy/errors_test.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package proxy
6
7
import (
8
"context"
9
"errors"
10
"fmt"
11
"testing"
12
13
"github.com/bufbuild/connect-go"
14
protocol "github.com/gitpod-io/gitpod/gitpod-protocol"
15
"github.com/sourcegraph/jsonrpc2"
16
"github.com/stretchr/testify/require"
17
)
18
19
func TestConvertError(t *testing.T) {
20
scenarios := []struct {
21
Input error
22
ExpectedError error
23
}{
24
{
25
Input: &protocol.ErrBadHandshake{
26
URL: "https://foo.bar",
27
},
28
ExpectedError: connect.NewError(connect.CodeUnauthenticated, fmt.Errorf("Failed to establish caller identity")),
29
},
30
{
31
Input: &jsonrpc2.Error{
32
Code: 400,
33
Message: "user id is a required argument",
34
},
35
ExpectedError: connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("user id is a required argument")),
36
},
37
{
38
Input: &jsonrpc2.Error{
39
Code: 401,
40
Message: "user is not authenticated",
41
},
42
ExpectedError: connect.NewError(connect.CodeUnauthenticated, fmt.Errorf("user is not authenticated")),
43
},
44
{
45
Input: &jsonrpc2.Error{
46
Code: 409,
47
Message: "already exists",
48
},
49
ExpectedError: connect.NewError(connect.CodeAlreadyExists, fmt.Errorf("already exists")),
50
},
51
{
52
Input: &jsonrpc2.Error{
53
Code: 429,
54
Message: "too many requests",
55
},
56
ExpectedError: connect.NewError(connect.CodeResourceExhausted, fmt.Errorf("too many requests")),
57
},
58
{
59
Input: &jsonrpc2.Error{
60
Code: 470,
61
Message: "user blocked",
62
},
63
ExpectedError: connect.NewError(connect.CodePermissionDenied, fmt.Errorf("user blocked")),
64
},
65
{
66
Input: &jsonrpc2.Error{
67
Code: 499,
68
Message: "catch all client error",
69
},
70
ExpectedError: connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("catch all client error")),
71
},
72
{
73
Input: &jsonrpc2.Error{
74
Code: 599,
75
Message: "catch all server error",
76
},
77
ExpectedError: connect.NewError(connect.CodeInternal, fmt.Errorf("catch all server error")),
78
},
79
{
80
Input: &jsonrpc2.Error{
81
Code: -32603,
82
Message: "Request getWorkspace failed with message: No workspace with id 'some-id' found.",
83
},
84
ExpectedError: connect.NewError(connect.CodeInternal, fmt.Errorf("Request getWorkspace failed with message: No workspace with id 'some-id' found.")),
85
},
86
{
87
Input: nil,
88
ExpectedError: nil,
89
},
90
{
91
Input: errors.New("some other random error returns internal error"),
92
ExpectedError: connect.NewError(connect.CodeInternal, fmt.Errorf("some other random error returns internal error")),
93
},
94
{
95
Input: context.Canceled,
96
ExpectedError: connect.NewError(connect.CodeDeadlineExceeded, fmt.Errorf("Request timed out")),
97
},
98
}
99
100
for _, s := range scenarios {
101
converted := ConvertError(s.Input)
102
require.Equal(t, s.ExpectedError, converted, s.Input)
103
}
104
}
105
106