I'm testing a Go application using Iris. I want to log every error or exception to my Rollbar account.
For instance, if the endpoint takes too long to respond and there is a timeout, I want to log it. How can I capture errors like that?
Update
I found in the documentation the OnError
method, and I thought I could use it like this:
iris.OnError(iris.StatusServiceUnavailable, func(c *iris.Context) {
c.Write("503")
params := string(c.RequestCtx.Request.Body())
rollbar.Error("error", errors.New("503 Service Unavailable"), &rollbar.Field{Name: "request_body", Data: params})
})
But it is not logging the error to Rollbar.
You'll need to write your own iris middleware.
Should look something like this:
func irisMiddlewareFunc(ctx *iris.Context) {
startedAt := time.Now()
ctx.Next()
timeTaken := time.Since(startedAt)
// now check time taken and log as required
if timeTaken.Seconds() > 2 {
fmt.Println("Taken too long")
}
}
The you use it like this:
iris.UseFunc(irisMiddlewareFunc)
And if you need to handle panics as well, just use recover() as you would usually do to handle panics. See github.com/iris-contrib/middleware/recovery for an example.