Copilot commented on code in PR #397:
URL: https://github.com/apache/dubbo-go-hessian2/pull/397#discussion_r3526013723


##########
generic_exception.go:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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 hessian
+
+import (
+       "strings"
+)
+
+import (
+       "github.com/apache/dubbo-go-hessian2/java_exception"
+)
+
+// GenericException keeps Java exception class and message.
+type GenericException struct {
+       ExceptionClass   string
+       ExceptionMessage string
+}
+
+// Error returns a readable error string.
+func (e GenericException) Error() string {
+       if e.ExceptionClass == "" {
+               return e.ExceptionMessage
+       }
+       if e.ExceptionMessage == "" {
+               return e.ExceptionClass
+       }
+       return "java exception: " + e.ExceptionClass + " - " + 
e.ExceptionMessage
+}
+
+// ToGenericException converts decoded exception to GenericException when 
possible.
+func ToGenericException(expt any) (*GenericException, bool) {
+       switch v := expt.(type) {
+       case *GenericException:
+               return v, true
+       case GenericException:
+               return &v, true
+       case *java_exception.DubboGenericException:
+               return &GenericException{ExceptionClass: v.ExceptionClass, 
ExceptionMessage: v.ExceptionMessage}, true
+       case java_exception.DubboGenericException:
+               return &GenericException{ExceptionClass: v.ExceptionClass, 
ExceptionMessage: v.ExceptionMessage}, true
+       case java_exception.Throwabler:
+               return &GenericException{ExceptionClass: v.JavaClassName(), 
ExceptionMessage: v.Error()}, true
+       case string:
+               return parseLegacyException(v), true
+       }
+       return nil, false
+}

Review Comment:
   `ToGenericException` can panic if callers pass an interface holding a 
typed-nil pointer (e.g., `(*java_exception.DubboGenericException)(nil)` or a 
nil `Throwabler`). In those cases the type switch matches and the code 
dereferences/calls methods on `nil`. Consider treating typed-nil as "not 
convertible" (or otherwise returning a non-nil `*GenericException`) so the 
function never panics when it returns `ok=true`.



##########
generic_exception.go:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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 hessian
+
+import (
+       "strings"
+)
+
+import (
+       "github.com/apache/dubbo-go-hessian2/java_exception"
+)
+
+// GenericException keeps Java exception class and message.
+type GenericException struct {
+       ExceptionClass   string
+       ExceptionMessage string
+}
+
+// Error returns a readable error string.
+func (e GenericException) Error() string {
+       if e.ExceptionClass == "" {
+               return e.ExceptionMessage
+       }
+       if e.ExceptionMessage == "" {
+               return e.ExceptionClass
+       }
+       return "java exception: " + e.ExceptionClass + " - " + 
e.ExceptionMessage
+}
+
+// ToGenericException converts decoded exception to GenericException when 
possible.
+func ToGenericException(expt any) (*GenericException, bool) {
+       switch v := expt.(type) {
+       case *GenericException:
+               return v, true
+       case GenericException:
+               return &v, true
+       case *java_exception.DubboGenericException:
+               return &GenericException{ExceptionClass: v.ExceptionClass, 
ExceptionMessage: v.ExceptionMessage}, true
+       case java_exception.DubboGenericException:
+               return &GenericException{ExceptionClass: v.ExceptionClass, 
ExceptionMessage: v.ExceptionMessage}, true
+       case java_exception.Throwabler:
+               return &GenericException{ExceptionClass: v.JavaClassName(), 
ExceptionMessage: v.Error()}, true
+       case string:
+               return parseLegacyException(v), true
+       }
+       return nil, false
+}
+
+func parseLegacyException(exStr string) *GenericException {
+       const prefix = "java exception:"
+       msg := strings.TrimSpace(exStr)
+       if strings.HasPrefix(msg, prefix) {
+               msg = strings.TrimSpace(strings.TrimPrefix(msg, prefix))
+       }
+       return &GenericException{ExceptionClass: "java.lang.Exception", 
ExceptionMessage: msg}
+}

Review Comment:
   `parseLegacyException` strips the `java exception:` prefix but never parses 
the "<class> - <message>" format produced by `GenericException.Error()`. That 
means passing an error string like `java exception: com.foo.Bar - boom` into 
`ToGenericException` loses the exception class. Consider parsing the delimiter 
when present and falling back to the legacy default only when the class/message 
cannot be extracted.



##########
generic_exception.go:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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 hessian
+
+import (
+       "strings"
+)
+
+import (
+       "github.com/apache/dubbo-go-hessian2/java_exception"
+)
+
+// GenericException keeps Java exception class and message.
+type GenericException struct {
+       ExceptionClass   string
+       ExceptionMessage string
+}
+
+// Error returns a readable error string.
+func (e GenericException) Error() string {
+       if e.ExceptionClass == "" {
+               return e.ExceptionMessage
+       }
+       if e.ExceptionMessage == "" {
+               return e.ExceptionClass
+       }
+       return "java exception: " + e.ExceptionClass + " - " + 
e.ExceptionMessage
+}
+
+// ToGenericException converts decoded exception to GenericException when 
possible.
+func ToGenericException(expt any) (*GenericException, bool) {
+       switch v := expt.(type) {
+       case *GenericException:
+               return v, true
+       case GenericException:
+               return &v, true
+       case *java_exception.DubboGenericException:
+               return &GenericException{ExceptionClass: v.ExceptionClass, 
ExceptionMessage: v.ExceptionMessage}, true
+       case java_exception.DubboGenericException:
+               return &GenericException{ExceptionClass: v.ExceptionClass, 
ExceptionMessage: v.ExceptionMessage}, true
+       case java_exception.Throwabler:
+               return &GenericException{ExceptionClass: v.JavaClassName(), 
ExceptionMessage: v.Error()}, true
+       case string:
+               return parseLegacyException(v), true
+       }
+       return nil, false

Review Comment:
   This change introduces a new exported conversion helper with several 
non-trivial branches (typed values, pointers, `Throwabler`, legacy string 
parsing). The repository has extensive decoder/exception tests already, but 
there are no tests covering `ToGenericException`/`parseLegacyException` 
behavior (including nil-safety and parsing). Adding a small 
`generic_exception_test.go` would help prevent regressions.



-- 
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