-
Notifications
You must be signed in to change notification settings - Fork 11
/
grpc.go
42 lines (34 loc) · 1.34 KB
/
grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*This file is part of kuberpult.
Kuberpult is free software: you can redistribute it and/or modify
it under the terms of the Expat(MIT) License as published by
the Free Software Foundation.
Kuberpult is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MIT License for more details.
You should have received a copy of the MIT License
along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
Copyright 2023 freiheit.com*/
package handler
import (
"context"
"net/http"
"github.com/freiheit-com/kuberpult/pkg/logger"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func handleGRPCError(ctx context.Context, w http.ResponseWriter, err error) {
s, _ := status.FromError(err)
switch s.Code() {
case codes.InvalidArgument:
http.Error(w, s.Message(), http.StatusBadRequest)
case codes.FailedPrecondition:
// This is a bit of a shortcut.
// We probably do not want to return NotFound for any failed precondition.
// For now, this is only used when deleting locks that are non-existent.
http.Error(w, s.Message(), http.StatusNotFound)
default:
logger.FromContext(ctx).Error(s.Message())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}