Skip to content

Commit

Permalink
PMM-12913 add the routing error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ademidoff committed Jun 5, 2024
1 parent d49e281 commit 605ed44
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
1 change: 1 addition & 0 deletions managed/cmd/pmm-managed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ func runHTTP1Server(ctx context.Context, deps *http1ServerDeps) {
proxyMux := grpc_gateway.NewServeMux(
grpc_gateway.WithMarshalerOption(grpc_gateway.MIMEWildcard, marshaller),
grpc_gateway.WithErrorHandler(pmmerrors.PMMHTTPErrorHandler),
grpc_gateway.WithRoutingErrorHandler(pmmerrors.PMMRoutingErrorHandler),
)

opts := []grpc.DialOption{
Expand Down
22 changes: 2 additions & 20 deletions qan-api2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"sync"
"time"

"github.com/gogo/status"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_validator "github.com/grpc-ecosystem/go-grpc-middleware/validator"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
Expand All @@ -44,7 +43,6 @@ import (
"golang.org/x/sys/unix"
"google.golang.org/grpc"
channelz "google.golang.org/grpc/channelz/service"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/reflection"
Expand All @@ -56,6 +54,7 @@ import (
aservice "github.com/percona/pmm/qan-api2/services/analytics"
rservice "github.com/percona/pmm/qan-api2/services/receiver"
"github.com/percona/pmm/qan-api2/utils/interceptors"
pmmerrors "github.com/percona/pmm/utils/errors"
"github.com/percona/pmm/utils/logger"
"github.com/percona/pmm/utils/sqlmetrics"
"github.com/percona/pmm/version"
Expand Down Expand Up @@ -150,7 +149,7 @@ func runJSONServer(ctx context.Context, grpcBindF, jsonBindF string) {

proxyMux := grpc_gateway.NewServeMux(
grpc_gateway.WithMarshalerOption(grpc_gateway.MIMEWildcard, marshaller),
grpc_gateway.WithRoutingErrorHandler(handleRoutingError),
grpc_gateway.WithRoutingErrorHandler(pmmerrors.PMMRoutingErrorHandler),
)
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}

Expand Down Expand Up @@ -247,23 +246,6 @@ func runDebugServer(ctx context.Context, debugBindF string) {
cancel()
}

// handleRoutingError customized the http status code for routes that can't be found (i.e. 404).
func handleRoutingError(ctx context.Context, mux *grpc_gateway.ServeMux, marshaler grpc_gateway.Marshaler, w http.ResponseWriter, r *http.Request, httpStatus int) {
if httpStatus != http.StatusNotFound {
grpc_gateway.DefaultRoutingErrorHandler(ctx, mux, marshaler, w, r, httpStatus)
return
}

// Use HTTPStatusError to customize the DefaultHTTPErrorHandler status code
msg := fmt.Sprintf("Endpoint not found: %s, http method: %s", r.URL.Path, r.Method)
err := &grpc_gateway.HTTPStatusError{
HTTPStatus: httpStatus,
Err: status.Error(codes.NotFound, msg),
}

grpc_gateway.DefaultHTTPErrorHandler(ctx, mux, marshaler, w, r, err)
}

func main() {
log.SetFlags(0)
log.SetPrefix("stdlog: ")
Expand Down
17 changes: 17 additions & 0 deletions utils/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,20 @@ func handleForwardResponseTrailer(w http.ResponseWriter, md runtime.ServerMetada
}
}
}

// PMMRoutingErrorHandler customizes the http status code for routes that can't be found (i.e. 404).
func PMMRoutingErrorHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, httpStatus int) {
if httpStatus != http.StatusNotFound {
runtime.DefaultRoutingErrorHandler(ctx, mux, marshaler, w, r, httpStatus)
return
}

// Use HTTPStatusError to customize the DefaultHTTPErrorHandler status code
msg := fmt.Sprintf("Endpoint not found: %s, http method: %s", r.URL.Path, r.Method)
err := &runtime.HTTPStatusError{
HTTPStatus: httpStatus,
Err: status.Error(codes.NotFound, msg),
}

runtime.DefaultHTTPErrorHandler(ctx, mux, marshaler, w, r, err)
}

0 comments on commit 605ed44

Please sign in to comment.