-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Make sure to call after hook on error #1479
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,83 +48,63 @@ func (hs *hooks) AddHook(hook Hook) { | |
func (hs hooks) process( | ||
ctx context.Context, cmd Cmder, fn func(context.Context, Cmder) error, | ||
) error { | ||
ctx, err := hs.beforeProcess(ctx, cmd) | ||
if err != nil { | ||
cmd.SetErr(err) | ||
return err | ||
if len(hs.hooks) == 0 { | ||
return fn(ctx, cmd) | ||
} | ||
|
||
cmdErr := fn(ctx, cmd) | ||
var hookIndex int | ||
var retErr error | ||
|
||
if err := hs.afterProcess(ctx, cmd); err != nil { | ||
cmd.SetErr(err) | ||
return err | ||
for ; hookIndex < len(hs.hooks) && retErr == nil; hookIndex++ { | ||
ctx, retErr = hs.hooks[hookIndex].BeforeProcess(ctx, cmd) | ||
if retErr != nil { | ||
cmd.SetErr(retErr) | ||
} | ||
} | ||
|
||
return cmdErr | ||
} | ||
|
||
func (hs hooks) beforeProcess(ctx context.Context, cmd Cmder) (context.Context, error) { | ||
for _, h := range hs.hooks { | ||
var err error | ||
ctx, err = h.BeforeProcess(ctx, cmd) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if retErr == nil { | ||
retErr = fn(ctx, cmd) | ||
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. if retErr != nil -> cmd.SetErr(retErr) 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. cm.SetErr occurs in fn? 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. Yes, cmd.SetErr is not needed here - |
||
} | ||
return ctx, nil | ||
} | ||
|
||
func (hs hooks) afterProcess(ctx context.Context, cmd Cmder) error { | ||
var firstErr error | ||
for i := len(hs.hooks) - 1; i >= 0; i-- { | ||
h := hs.hooks[i] | ||
if err := h.AfterProcess(ctx, cmd); err != nil && firstErr == nil { | ||
firstErr = err | ||
for hookIndex--; hookIndex >= 0; hookIndex-- { | ||
if err := hs.hooks[hookIndex].AfterProcess(ctx, cmd); err != nil { | ||
retErr = err | ||
cmd.SetErr(retErr) | ||
} | ||
} | ||
return firstErr | ||
|
||
return retErr | ||
} | ||
|
||
func (hs hooks) processPipeline( | ||
ctx context.Context, cmds []Cmder, fn func(context.Context, []Cmder) error, | ||
) error { | ||
ctx, err := hs.beforeProcessPipeline(ctx, cmds) | ||
if err != nil { | ||
setCmdsErr(cmds, err) | ||
return err | ||
if len(hs.hooks) == 0 { | ||
return fn(ctx, cmds) | ||
} | ||
|
||
cmdsErr := fn(ctx, cmds) | ||
var hookIndex int | ||
var retErr error | ||
|
||
if err := hs.afterProcessPipeline(ctx, cmds); err != nil { | ||
setCmdsErr(cmds, err) | ||
return err | ||
for ; hookIndex < len(hs.hooks) && retErr == nil; hookIndex++ { | ||
ctx, retErr = hs.hooks[hookIndex].BeforeProcessPipeline(ctx, cmds) | ||
if retErr != nil { | ||
setCmdsErr(cmds, retErr) | ||
} | ||
} | ||
|
||
return cmdsErr | ||
} | ||
|
||
func (hs hooks) beforeProcessPipeline(ctx context.Context, cmds []Cmder) (context.Context, error) { | ||
for _, h := range hs.hooks { | ||
var err error | ||
ctx, err = h.BeforeProcessPipeline(ctx, cmds) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if retErr == nil { | ||
retErr = fn(ctx, cmds) | ||
} | ||
return ctx, nil | ||
} | ||
|
||
func (hs hooks) afterProcessPipeline(ctx context.Context, cmds []Cmder) error { | ||
var firstErr error | ||
for i := len(hs.hooks) - 1; i >= 0; i-- { | ||
h := hs.hooks[i] | ||
if err := h.AfterProcessPipeline(ctx, cmds); err != nil && firstErr == nil { | ||
firstErr = err | ||
for hookIndex--; hookIndex >= 0; hookIndex-- { | ||
if err := hs.hooks[hookIndex].AfterProcessPipeline(ctx, cmds); err != nil { | ||
retErr = err | ||
setCmdsErr(cmds, retErr) | ||
} | ||
} | ||
return firstErr | ||
|
||
return retErr | ||
} | ||
|
||
func (hs hooks) processTxPipeline( | ||
|
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.
break will not trigger idx++
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.
Right, but why that is a problem? I call AfterProcess for all functions except the last one that returned the error.