From da40c541412b9905f6ae4c0ef01c084ae60f58d8 Mon Sep 17 00:00:00 2001 From: Sammy Kerata Oina <44265300+SammyOina@users.noreply.github.com> Date: Mon, 29 May 2023 17:23:39 +0300 Subject: [PATCH] MF-1783 - Improve HTTP / gRPC codes when attempting to publish with nonexistent thing key / channel id (#1798) * return 404 for non existent thing key or channel Signed-off-by: SammyOina * check for malformed uuid input Signed-off-by: SammyOina * use replace all Signed-off-by: SammyOina * remove uuid check Signed-off-by: SammyOina --------- Signed-off-by: SammyOina --- http/api/transport.go | 5 ++++- things/api/auth/grpc/server.go | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/http/api/transport.go b/http/api/transport.go index b645b181..bb03ed96 100644 --- a/http/api/transport.go +++ b/http/api/transport.go @@ -77,7 +77,7 @@ func parseSubtopic(subtopic string) (string, error) { if err != nil { return "", errMalformedSubtopic } - subtopic = strings.Replace(subtopic, "/", ".", -1) + subtopic = strings.ReplaceAll(subtopic, "/", ".") elems := strings.Split(subtopic, ".") filteredElems := []string{} @@ -171,6 +171,9 @@ func encodeError(_ context.Context, err error, w http.ResponseWriter) { w.WriteHeader(http.StatusForbidden) case codes.Internal: w.WriteHeader(http.StatusInternalServerError) + case codes.NotFound: + err = errors.ErrNotFound + w.WriteHeader(http.StatusNotFound) default: w.WriteHeader(http.StatusInternalServerError) } diff --git a/things/api/auth/grpc/server.go b/things/api/auth/grpc/server.go index 459627b9..12e32ddc 100644 --- a/things/api/auth/grpc/server.go +++ b/things/api/auth/grpc/server.go @@ -137,6 +137,9 @@ func encodeError(err error) error { case errors.ErrNotFound: return status.Error(codes.NotFound, "entity does not exist") default: + if errors.Contains(err, errors.ErrNotFound) || errors.Contains(err, errors.ErrViewEntity) { + return status.Error(codes.NotFound, "entity does not exist") + } return status.Error(codes.Internal, "internal server error") } }