Skip to content

Need help with custom middleware #1820

Answered by aldas
awiz911 asked this question in Q&A
Discussion options

You must be logged in to vote

"status": c.Response().Status, returns 200 after err := next(c) because at that moment error handler as not yet modified response status (response has not been "commited" yet). It will be done for error in global error handler after the end of the handler chain execution.

Check this block how status is written in global error handler.

echo/echo.go

Line 379 in dec96f0

if !c.Response().Committed {

You can check err for type that contains code and extract it before logging

			err := next(c)

			status := c.Response().Status
			
			httpErr := new(echo.HTTPError)
			if errors.As(err, &httpErr) {
				status = httpErr.Code
			}

			fields := map[string]interface{}{
				"latency": 

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@awiz911
Comment options

Answer selected by awiz911
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants