-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix:httpcode #1252
fix:httpcode #1252
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
package gfsperrors | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"sort" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
|
@@ -15,7 +17,8 @@ const ( | |
|
||
// Error implements the error interface for compatibility with built-in error. | ||
func (m *GfSpError) Error() string { | ||
return m.String() | ||
str, _ := json.Marshal(m) | ||
return string(str) | ||
} | ||
|
||
// SetError sets the Description field by Error(), it is use for change the | ||
|
@@ -43,6 +46,26 @@ func init() { | |
}) | ||
} | ||
|
||
func GetGfSpErr(err error) *GfSpError { | ||
if err == nil { | ||
return nil | ||
} | ||
if gfspErr, ok := err.(*GfSpError); ok { | ||
return gfspErr | ||
} | ||
i := strings.Index(err.Error(), "desc = ") | ||
if i == -1 || len(err.Error())-1 < i+6 { | ||
return nil | ||
} | ||
errInfo := err.Error()[i+6:] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The result after the rpc call is "rpc error: code = Unknown desc = xxx" We only focus on xxx |
||
var gfspErr GfSpError | ||
unmarshalErr := json.Unmarshal([]byte(errInfo), &gfspErr) | ||
if unmarshalErr == nil && gfspErr.HttpStatusCode != 0 { | ||
return &gfspErr | ||
} | ||
return nil | ||
} | ||
|
||
// MakeGfSpError returns an GfSpError from the build-in error interface. It is | ||
// difficult to predefine all errors. For undefined errors, there needs to be a | ||
// way to capture them and return them to the client according to the GfSpError | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if the passed-in err is already a GfSpError?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added type assertions