Copilot commented on code in PR #782:
URL: https://github.com/apache/dubbo-go-pixiu/pull/782#discussion_r2458699834


##########
pkg/filter/http/grpcproxy/grpc.go:
##########
@@ -267,21 +274,40 @@ func (f *Filter) Decode(c *http.HttpContext) 
filter.FilterStatus {
 
        resp, err := Invoke(ctx, stub, mthDesc, grpcReq, grpc.Header(&md), 
grpc.Trailer(&t))
        // judge err is server side error or not
-       if st, ok := status.FromError(err); !ok || isServerError(st) {
-               if isServerTimeout(st) {
-                       logger.Errorf("%s err {failed to invoke grpc service 
provider because timeout, err:%s}", loggerHeader, err.Error())
-                       c.SendLocalReply(stdHttp.StatusGatewayTimeout, 
[]byte(fmt.Sprintf("%s", err)))
+       if st, ok := status.FromError(err); ok {
+               // Handle client-side gRPC errors (e.g., InvalidArgument)
+               if st.Code() != codes.OK && !isServerError(st) {
+                       logger.Errorf("%s err {gRPC client error, code: %s, 
msg: %s}", loggerHeader, st.Code(), st.Message())
+                       errResp := http.BadGateway.WithError(fmt.Errorf("gRPC 
client error: %w", err))

Review Comment:
   Client-side gRPC errors (like `InvalidArgument`, `NotFound`) should return 
400-level status codes (e.g., `BadRequest`) rather than `BadGateway` (502), 
which typically indicates upstream service errors. This would provide clearer 
error classification to API consumers.
   ```suggestion
                        httpStatus := grpcCodeToHTTPStatus(st.Code())
                        errResp := httpStatus.WithError(fmt.Errorf("gRPC client 
error: %w", err))
   ```



##########
pkg/filter/http/httpproxy/routerfilter.go:
##########
@@ -152,10 +151,12 @@ func (f *Filter) Decode(hc *contexthttp.HttpContext) 
filter.FilterStatus {
                var urlErr *url.Error
                ok := errors.As(err, &urlErr)
                if ok && urlErr.Timeout() {
-                       hc.SendLocalReply(http.StatusGatewayTimeout, 
[]byte(err.Error()))
+                       errResp := 
contexthttp.GatewayTimeout.WithError(fmt.Errorf("upstream timeout: %w", err))
+                       hc.SendLocalReply(errResp.Status, errResp.ToJSON())
                        return filter.Stop
                }
-               hc.SendLocalReply(http.StatusServiceUnavailable, 
[]byte(err.Error()))
+               errResp := 
contexthttp.BadGateway.WithError(fmt.Errorf("upstream service error: %w", err))

Review Comment:
   The error message 'upstream service error' is generic and doesn't 
differentiate from the ServiceUnavailable case. Consider using a more specific 
message like 'upstream connection failed' to clarify that this error occurs 
when the upstream is reachable but returns an error response.
   ```suggestion
                errResp := 
contexthttp.BadGateway.WithError(fmt.Errorf("upstream connection failed: %w", 
err))
   ```



##########
pkg/context/http/erroresponse.go:
##########
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package http

Review Comment:
   Corrected spelling of 'erroresponse.go' to 'errorresponse.go'



##########
pkg/filter/http/grpcproxy/grpc.go:
##########
@@ -252,7 +258,8 @@ func (f *Filter) Decode(c *http.HttpContext) 
filter.FilterStatus {
        err = jsonToProtoMsg(c.Request.Body, grpcReq)
        if err != nil && !errors.Is(err, io.EOF) {
                logger.Errorf("%s err {failed to convert json to proto msg, 
%s}", loggerHeader, err.Error())
-               c.SendLocalReply(stdHttp.StatusInternalServerError, 
[]byte(fmt.Sprintf("%s", err)))
+               errResp := http.BadGateway.WithError(fmt.Errorf("protocol 
conversion error: %w", err))

Review Comment:
   Protocol conversion errors during request processing (JSON to proto) should 
return `BadRequest` (400) instead of `BadGateway` (502), as this indicates a 
client input problem rather than an upstream service issue.
   ```suggestion
                errResp := http.BadRequest.WithError(fmt.Errorf("protocol 
conversion error: %w", err))
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to