Skip to content

Commit

Permalink
Format http errors differently
Browse files Browse the repository at this point in the history
  • Loading branch information
gsquared94 committed May 29, 2020
1 parent 1b5eb3d commit 7d97996
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion pkg/skaffold/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package server

import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
Expand All @@ -27,6 +28,7 @@ import (
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/status"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
Expand Down Expand Up @@ -159,7 +161,7 @@ func newGRPCServer(port int) (func() error, error) {
}

func newHTTPServer(port, proxyPort int) (func() error, error) {
mux := runtime.NewServeMux()
mux := runtime.NewServeMux(runtime.WithProtoErrorHandler(errorHandler))
opts := []grpc.DialOption{grpc.WithInsecure()}
err := proto.RegisterSkaffoldServiceHandlerFromEndpoint(context.Background(), mux, fmt.Sprintf("%s:%d", util.Loopback, proxyPort), opts)
if err != nil {
Expand All @@ -176,3 +178,18 @@ func newHTTPServer(port, proxyPort int) (func() error, error) {

return l.Close, nil
}

type errResponse struct {
Err string `json:"error,omitempty"`
}

func errorHandler(ctx context.Context, _ *runtime.ServeMux, marshaler runtime.Marshaler, writer http.ResponseWriter, _ *http.Request, err error) {
writer.Header().Set("Content-type", marshaler.ContentType())
s, _ := status.FromError(err)
writer.WriteHeader(runtime.HTTPStatusFromCode(s.Code()))
if err := json.NewEncoder(writer).Encode(errResponse{
Err: s.Message(),
}); err != nil {
writer.Write([]byte(`{"error": "failed to marshal error message"}`))
}
}

0 comments on commit 7d97996

Please sign in to comment.