Log errors from rows.Close (#920)

* Log errors from rows.Close

* fixed imports

* Added contextual messages

* fixed review changes
This commit is contained in:
Prateek Sachan 2020-03-18 15:47:18 +05:30 committed by GitHub
parent c2bd0b97b3
commit c019ad7086
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
43 changed files with 118 additions and 69 deletions

View file

@ -15,13 +15,17 @@
package common
import (
"context"
"fmt"
"io"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/matrix-org/util"
"github.com/matrix-org/dendrite/common/config"
"github.com/matrix-org/dugong"
"github.com/sirupsen/logrus"
@ -156,3 +160,17 @@ func setupFileHook(hook config.LogrusHook, level logrus.Level, componentName str
),
})
}
//CloseAndLogIfError Closes io.Closer and logs the error if any
func CloseAndLogIfError(ctx context.Context, closer io.Closer, message string) {
if closer == nil {
return
}
err := closer.Close()
if ctx == nil {
ctx = context.TODO()
}
if err != nil {
util.GetLogger(ctx).WithError(err).Error(message)
}
}