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