mirror of
https://github.com/go-micro/go-micro.git
synced 2026-06-15 19:35:13 +02:00
Errors (#2290)
* add errors.As convert target err to *Error, return false if err don't match *Error * update errors.As to (*Error, bool) * fixing FromError panic issue when err is nil
This commit is contained in:
@@ -4,6 +4,7 @@ package errors
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
@@ -138,9 +139,24 @@ func Equal(err1 error, err2 error) bool {
|
||||
|
||||
// FromError try to convert go error to *Error
|
||||
func FromError(err error) *Error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if verr, ok := err.(*Error); ok && verr != nil {
|
||||
return verr
|
||||
}
|
||||
|
||||
return Parse(err.Error())
|
||||
}
|
||||
|
||||
// As finds the first error in err's chain that matches *Error
|
||||
func As(err error) (*Error, bool) {
|
||||
if err == nil {
|
||||
return nil, false
|
||||
}
|
||||
var merr *Error
|
||||
if errors.As(err, &merr) {
|
||||
return merr, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user