codec
codec

Reputation: 8836

How to stop execution of next handler if there is an error in previous handler gin

I have chain of router handlers defined

apis.POST(/hello, authHandler("username") , myfuncHandler)

WHow can I force stop calling myfuncHandler if authHandler has some error. I was trying to use c.Next() to move to next handler if there is no error. But I noticed that even if there is error it moves to next handler execution.

I am using gin as server.

Upvotes: 1

Views: 4026

Answers (1)

Afzal Khan
Afzal Khan

Reputation: 101

Use context.Abort() with return

return will only stop the execution of code for that handler only. So if you are using multiple handlers then code for all other handlers will get executed.

So use context.Abort() with return

func authHandler(username string) {
    err := auth(username)
    if err != nil {
        context.Abort()
        return
    }
}

Upvotes: 4

Related Questions