generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: better unwrapping of java exception (#74)
* feat: better unwrapping of java exception * srv-side exception processing * modify FromError * fix typo problem * add basic Exception type * modify NewException returned value * introduct java.lang.Exception * gofumpt format * add docs * modify indentation of docs
- Loading branch information
1 parent
c3b7d6b
commit 708aa3f
Showing
9 changed files
with
345 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2023 CloudWeGo Authors | ||
* | ||
* 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 exception | ||
|
||
import ( | ||
"github.com/apache/dubbo-go-hessian2/java_exception" | ||
) | ||
|
||
type Throwabler = java_exception.Throwabler | ||
|
||
type Exception = java_exception.Exception | ||
|
||
func NewException(detailMessage string) *Exception { | ||
return java_exception.NewException(detailMessage) | ||
} | ||
|
||
// FromError extracts Throwabler from passed err. | ||
// | ||
// - If err is nil, it returns nil and false | ||
// | ||
// - If err implements Unwrap(), it would unwrap err until getting the real cause. | ||
// Then it would check cause whether implementing Throwabler. If yes, it returns | ||
// Throwabler and true. | ||
// | ||
// If not, it checks err whether implementing Throwabler directly. If yes, | ||
// it returns Throwabler and true. | ||
func FromError(err error) (Throwabler, bool) { | ||
if err == nil { | ||
return nil, false | ||
} | ||
for { | ||
if wrapper, ok := err.(interface{ Unwrap() error }); ok { | ||
err = wrapper.Unwrap() | ||
} else { | ||
break | ||
} | ||
} | ||
if exception, ok := err.(Throwabler); ok { | ||
return exception, true | ||
} | ||
return nil, false | ||
} |
Oops, something went wrong.