Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: add PGCode to admin api errors #98601

Merged
merged 1 commit into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ go_library(
"//pkg/sql/optionalnodeliveness",
"//pkg/sql/parser",
"//pkg/sql/pgwire",
"//pkg/sql/pgwire/pgcode",
"//pkg/sql/pgwire/pgerror",
"//pkg/sql/pgwire/pgwirecancel",
"//pkg/sql/physicalplan",
Expand Down Expand Up @@ -482,6 +483,8 @@ go_test(
"//pkg/sql/clusterunique",
"//pkg/sql/execinfrapb",
"//pkg/sql/idxusage",
"//pkg/sql/pgwire/pgcode",
"//pkg/sql/pgwire/pgerror",
"//pkg/sql/roleoption",
"//pkg/sql/sem/catconstants",
"//pkg/sql/sem/tree",
Expand Down
26 changes: 20 additions & 6 deletions pkg/server/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/isql"
"github.com/cockroachdb/cockroach/pkg/sql/optionalnodeliveness"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/roleoption"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
Expand Down Expand Up @@ -109,10 +111,6 @@ func nonTableDescriptorRangeCount() int64 {
}))
}

// apiServerMessage is the standard body for all HTTP 500 responses.
var errAdminAPIError = grpcstatus.Errorf(codes.Internal, "An internal server error "+
"has occurred. Please check your CockroachDB logs for more details.")

// A adminServer provides a RESTful HTTP API to administration of
// the cockroach cluster.
type adminServer struct {
Expand Down Expand Up @@ -309,14 +307,30 @@ func (s *adminServer) RegisterGateway(
// the RPC endpoint method.
func serverError(ctx context.Context, err error) error {
log.ErrorfDepth(ctx, 1, "%+v", err)
return errAdminAPIError

// Include the PGCode in the message for easier troubleshooting
errCode := pgerror.GetPGCode(err).String()
if errCode != pgcode.Uncategorized.String() {
errMessage := fmt.Sprintf("%s Error Code: %s", errAPIInternalErrorString, errCode)
return grpcstatus.Errorf(codes.Internal, errMessage)
}

// The error is already grpcstatus formatted error.
// Likely calling serverError multiple times on same error.
grpcCode := grpcstatus.Code(err)
if grpcCode != codes.Unknown {
return err
}

// Fallback to generic message
return errAPIInternalError
}

// serverErrorf logs the provided error and returns an error that should be returned by
// the RPC endpoint method.
func serverErrorf(ctx context.Context, format string, args ...interface{}) error {
log.ErrorfDepth(ctx, 1, format, args...)
return errAdminAPIError
return errAPIInternalError
}

// isNotFoundError returns true if err is a table/database not found error.
Expand Down
18 changes: 18 additions & 0 deletions pkg/server/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/idxusage"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
Expand Down Expand Up @@ -3249,6 +3251,22 @@ func TestAdminPrivilegeChecker(t *testing.T) {
}
}

func TestServerError(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()
pgError := pgerror.New(pgcode.OutOfMemory, "TestServerError.OutOfMemory")
err := serverError(ctx, pgError)
require.Equal(t, "rpc error: code = Internal desc = An internal server error has occurred. Please check your CockroachDB logs for more details. Error Code: 53200", err.Error())

err = serverError(ctx, err)
require.Equal(t, "rpc error: code = Internal desc = An internal server error has occurred. Please check your CockroachDB logs for more details. Error Code: 53200", err.Error())

err = fmt.Errorf("random error that is not pgerror or grpcstatus")
err = serverError(ctx, err)
require.Equal(t, "rpc error: code = Internal desc = An internal server error has occurred. Please check your CockroachDB logs for more details.", err.Error())
}

func TestDatabaseAndTableIndexRecommendations(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
Expand Down