AlexStocks commented on code in PR #397: URL: https://github.com/apache/dubbo-go-hessian2/pull/397#discussion_r3526307989
########## 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 { Review Comment: [P1] 这个兼容分支会丢失旧字符串异常里的原始 Java 类名。当前 `Error()` 输出格式是 `java exception: <class> - <message>`,但这里仅去掉前缀后就固定把 `ExceptionClass` 写成 `java.lang.Exception`。也就是说,历史字符串异常一旦经过 `ToGenericException()`,类名信息就不可逆地丢失了。这里至少需要识别 `"<class> - <message>"` 这种已有格式并拆回两个字段。 ########## 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 ( Review Comment: [P1] 这个新增文件当前没有通过 `gofmt`。我本地跑 `gofmt -l generic_exception.go` 会直接报出这个文件,原因就是这里拆成了两个 import block。按仓库 Go 规范应整理成单个 import block。 ########## 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. Review Comment: [P1] 这个 PR 新增了 `GenericException` / `ToGenericException` 的核心行为,但没有配套单测覆盖 `DubboGenericException`、普通 `Throwabler`、以及 legacy string(尤其是 `java exception: <class> - <message>`)这三条分支。当前兼容逻辑已经涉及字符串解析和信息归一化,没有测试的话很容易在后续重构里悄悄回归。 -- 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]
