Skip to content

Commit

Permalink
Update Compliance Tests support
Browse files Browse the repository at this point in the history
  • Loading branch information
lbeckman314 committed Jan 9, 2024
1 parent 7eeb1df commit 8acf51f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/compliance-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ name: Compliance Test

on:
push:
branches:
- master

jobs:
build:
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ name: Go

on:
push:
branches:
- master

jobs:

lint:
name: lint
runs-on: ubuntu-latest
Expand Down
39 changes: 38 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ package server
import (
"net"
"net/http"
"strings"

"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"

"github.com/golang/gddo/httputil"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
Expand Down Expand Up @@ -54,6 +56,40 @@ func newDebugInterceptor(log *logger.Logger) grpc.UnaryServerInterceptor {
}
}

// customErrorHandler is a custom error handler for the gRPC gateway
// Returns '400' for invalid backend parameters and '500' for all other errors
// Required for Compliance Tests
func customErrorHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error) {
const fallback = `{"error": "failed to process the request"}`

st, ok := status.FromError(err)
if !ok {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fallback))
return
}

// Map specific gRPC error codes to HTTP status codes
if (strings.Contains(st.Message(), "backend parameters not supported")) {
w.WriteHeader(http.StatusBadRequest)
} else {
w.WriteHeader(http.StatusInternalServerError)
}

// Write the error message
jErr := JSONError{Error: st.Message()}
jErrBytes, mErr := marshaler.Marshal(jErr)
if mErr != nil {
w.Write([]byte(fallback))
return
}
w.Write(jErrBytes)
}

type JSONError struct {
Error string `json:"error"`
}

// Serve starts the server and does not block. This will open TCP ports
// for both RPC and HTTP.
func (s *Server) Serve(pctx context.Context) error {
Expand Down Expand Up @@ -84,7 +120,8 @@ func (s *Server) Serve(pctx context.Context) error {
mux := http.NewServeMux()

marsh := NewMarshaler()
grpcMux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, marsh))
grpcMux := runtime.NewServeMux(
runtime.WithMarshalerOption(runtime.MIMEWildcard, marsh), runtime.WithErrorHandler(customErrorHandler))

// m := protojson.MarshalOptions{
// Indent: " ",
Expand Down

0 comments on commit 8acf51f

Please sign in to comment.