Skip to content
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

Hook to log errors if status code > 399 #419

Open
ORESoftware opened this issue Feb 12, 2020 · 1 comment
Open

Hook to log errors if status code > 399 #419

ORESoftware opened this issue Feb 12, 2020 · 1 comment

Comments

@ORESoftware
Copy link

ORESoftware commented Feb 12, 2020

I have this:

If my server handles a request and writes the response code as being greater than 399, I want to log an error trace:

    	bm.Use(func(res http.ResponseWriter, req *http.Request, c martini.Context, log *logging.Logger, statter statsd.Statter) {
    
    		start := time.Now()
    
    		rw := res.(martini.ResponseWriter)
    
    		c.Next()  // do all the middleware handler stuff
    
    		if res.(martini.ResponseWriter).Status() > 399 {
    			log.Warning("%v", "print response here")  // how to read response here
    		}
    
    })

is there a way to read the response, given the status code? If we send an error back to the client, how can I intercept the error message and log it?

for example, if we use:

func (ctr *TagsController) DeleteTags(c *common.Context, user *common.User, org *common.Organization, req *http.Request, params martini.Params) (int, interface{}) {

	tagName, hasTag := params["tag"]

	if !hasTag {
                 var XXX = errors.New("missing tag: no tag param to update")
		return 422, ctr.ErrorResponse(422, XXX)
	}

}

I would want to log XXX from the final martini middleware, since the status code is greater than 399. How to do this?

Furthermore, it would be nice to send in private/public logging - the client message and the error message. Using Node.js express we could do this:

app.use((req,res,next) => {
    next(new Error('oh shit'));
});

app.use((err,req,res,next) => {
   log.error(err);   // the stack trace is logged
   res.send(err.message);  // the client only sees the message, not the stack trace
});

is there a way to send the client a different message, but still hook into the error object so we can log it easily instead of having to log it manually everywhere?

@ORESoftware
Copy link
Author

ORESoftware commented Feb 12, 2020

The only solution that I know of is to do something like this:

func logAndEcho(...args interface{}) string {
   log.Info(args)
   return args;
}


func (ctr *TagsController) DeleteTags(c *common.Context, user *common.User, org *common.Organization, req *http.Request, params martini.Params) (int, interface{}) {

	tagName, hasTag := params["tag"]

	if !hasTag {
		return 422, ctr.ErrorResponse(422, logAndEcho(errors.New("missing tag: no tag param to update")))
	}

}

using the above, we can log it and send it back in the same line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant