Skip to content

Commit

Permalink
[bugfix] Fix domains not being unblockable, log internal server error…
Browse files Browse the repository at this point in the history
…s from API (#833)

* log internal server errors from 500 api calls

* don't exec into nil dest

* don't exec into nil dest

* log error in router logger not api errorhandling

* update logging a tad

* linter
  • Loading branch information
tsmethurst authored Sep 17, 2022
1 parent 1149310 commit c1585d5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
4 changes: 4 additions & 0 deletions internal/api/errorhandling.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func genericErrorHandler(c *gin.Context, instanceGet func(ctx context.Context, d
// if something goes wrong during the function, it will recover and just try to serve
// an appropriate application/json content-type error.
func ErrorHandler(c *gin.Context, errWithCode gtserror.WithCode, instanceGet func(ctx context.Context, domain string) (*apimodel.Instance, gtserror.WithCode)) {
// set the error on the gin context so that it can be logged
// in the gin logger middleware (internal/router/logger.go)
c.Error(errWithCode) //nolint:errcheck

// discover if we're allowed to serve a nice html error page,
// or if we should just use a json. Normally we would want to
// check for a returned error, but if an error occurs here we
Expand Down
2 changes: 1 addition & 1 deletion internal/db/bundb/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (d *domainDB) DeleteDomainBlock(ctx context.Context, domain string) db.Erro
_, err := d.conn.NewDelete().
Model((*gtsmodel.DomainBlock)(nil)).
Where("domain = ?", domain).
Exec(ctx, nil)
Exec(ctx)
if err != nil {
return d.conn.ProcessError(err)
}
Expand Down
16 changes: 4 additions & 12 deletions internal/router/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,21 @@ func loggingMiddleware(c *gin.Context) {
fields[4] = kv.Field{"statusCode", code}
fields[5] = kv.Field{"path", path}

var lvl level.LEVEL
// Create log entry with fields
l := log.WithFields(fields...)

// Default is info
lvl = level.INFO
lvl := level.INFO

if code >= 500 {
// This is a server error
lvl = level.ERROR

if len(c.Errors) > 0 {
// Add an error string log field
fields = append(fields, kv.Field{
"error", c.Errors.String(),
})
}
l = l.WithField("error", c.Errors)
}

// Generate a nicer looking bytecount
size := bytesize.Size(c.Writer.Size())

// Create log entry with fields
l := log.WithFields(fields...)

// Finally, write log entry with status text body size
l.Logf(lvl, "%s: wrote %s", http.StatusText(code), size)
}()
Expand Down

0 comments on commit c1585d5

Please sign in to comment.