Update error reporting middleware

This commit is contained in:
Dane Everitt
2020-12-15 21:08:00 -08:00
parent 3a26a5d39d
commit 0ecc166dcd
9 changed files with 100 additions and 114 deletions

View File

@@ -22,23 +22,15 @@ func (m *Middleware) ErrorHandler() gin.HandlerFunc {
if err == nil {
return
}
tracked := TrackedError(err)
tracked := NewTrackedError(err)
// If there is a server in the context for this request pull it out so that we can
// track the error specifically for that server.
if s, ok := c.Get("server"); ok {
tracked = TrackedServerError(err, s.(*server.Server))
tracked = NewServerError(err, s.(*server.Server))
}
// Sometimes requests have already modifed the status by the time this handler is
// called. In those cases, try to attach the error message but don't try to change
// the response status since it has already been set.
if c.Writer.Status() != 200 {
if err.Error() == io.EOF.Error() {
c.JSON(c.Writer.Status(), gin.H{"error": "A JSON formatted body is required for this endpoint."})
} else {
tracked.AbortWithStatus(c.Writer.Status(), c)
}
// This error occurs if you submit invalid JSON data to an endpoint.
if err.Error() == io.EOF.Error() {
c.JSON(c.Writer.Status(), gin.H{"error": "A JSON formatted body is required for this endpoint."})
return
}
tracked.Abort(c)