Skip to content

Commit 248a208

Browse files
committed
Allow to retrieve bindHook errors
The original error returned by bindHook is currently lost. I have use cases where I need to access this error details in order to return specific error messages to end users. This commit adds the ability to retrieve the bindHook error from the BindError struct.
1 parent c4f8b2f commit 248a208

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

tonic/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func Handler(h interface{}, status int, options ...func(*Route)) gin.HandlerFunc
7373
input := reflect.New(in)
7474
// Bind the body with the hook.
7575
if err := bindHook(c, input.Interface()); err != nil {
76-
handleError(c, BindError{message: err.Error(), typ: in})
76+
handleError(c, BindError{message: err.Error(), typ: in, bindHookErr: err})
7777
return
7878
}
7979
// Bind query-parameters.

tonic/tonic.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ func Tags(tags []string) func(*Route) {
230230
// to bind parameters, to differentiate from errors returned
231231
// by the handlers.
232232
type BindError struct {
233+
bindHookErr error
233234
validationErr error
234235
message string
235236
typ reflect.Type
@@ -258,6 +259,11 @@ func (be BindError) ValidationErrors() validator.ValidationErrors {
258259
return nil
259260
}
260261

262+
// BindHookError returns the error from the bind process.
263+
func (be BindError) BindHookError() error {
264+
return be.bindHookErr
265+
}
266+
261267
// An extractorFunc extracts data from a gin context according to
262268
// parameters specified in a field tag.
263269
type extractor func(*gin.Context, string) (string, []string, error)

0 commit comments

Comments
 (0)