Chore: Enable errorlint linter (#29227)

* Enable errorlint linter
* Handle wrapped errors

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>
This commit is contained in:
Arve Knudsen
2020-11-19 14:47:17 +01:00
committed by GitHub
parent 993adb72e0
commit 9593d57914
34 changed files with 142 additions and 101 deletions

View File

@@ -2,6 +2,7 @@ package authproxy
import (
"encoding/hex"
"errors"
"fmt"
"hash/fnv"
"net"
@@ -180,12 +181,12 @@ func (auth *AuthProxy) Login(logger log.Logger, ignoreCache bool) (int64, *Error
}
if isLDAPEnabled() {
id, e := auth.LoginViaLDAP()
if e != nil {
if e == ldap.ErrInvalidCredentials {
id, err := auth.LoginViaLDAP()
if err != nil {
if errors.Is(err, ldap.ErrInvalidCredentials) {
return 0, newError("proxy authentication required", ldap.ErrInvalidCredentials)
}
return 0, newError("failed to get the user", e)
return 0, newError("failed to get the user", err)
}
return id, nil

View File

@@ -2,6 +2,7 @@ package middleware
import (
"context"
"errors"
"fmt"
"net/url"
"strconv"
@@ -182,7 +183,7 @@ func initContextWithBasicAuth(ctx *models.ReqContext, orgId int64) bool {
"err", err,
)
if err == models.ErrUserNotFound {
if errors.Is(err, models.ErrUserNotFound) {
err = login.ErrInvalidCredentials
}
ctx.JsonApiErr(401, errStringInvalidUsernamePassword, err)
@@ -250,7 +251,7 @@ func rotateEndOfRequestFunc(ctx *models.ReqContext, authTokenService models.User
// if the request is cancelled by the client we should not try
// to rotate the token since the client would not accept any result.
if ctx.Context.Req.Context().Err() == context.Canceled {
if errors.Is(ctx.Context.Req.Context().Err(), context.Canceled) {
return
}

View File

@@ -17,6 +17,7 @@ package middleware
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
@@ -102,7 +103,7 @@ func function(pc uintptr) []byte {
func Recovery() macaron.Handler {
return func(c *macaron.Context) {
defer func() {
if err := recover(); err != nil {
if r := recover(); r != nil {
panicLogger := log.Root
// try to get request logger
if ctx, ok := c.Data["ctx"]; ok {
@@ -110,16 +111,18 @@ func Recovery() macaron.Handler {
panicLogger = ctxTyped.Logger
}
// http.ErrAbortHandler is suppressed by default in the http package
// and used as a signal for aborting requests. Suppresses stacktrace
// since it doesn't add any important information.
if err == http.ErrAbortHandler {
panicLogger.Error("Request error", "error", err)
return
if err, ok := r.(error); ok {
// http.ErrAbortHandler is suppressed by default in the http package
// and used as a signal for aborting requests. Suppresses stacktrace
// since it doesn't add any important information.
if errors.Is(err, http.ErrAbortHandler) {
panicLogger.Error("Request error", "error", err)
return
}
}
stack := stack(3)
panicLogger.Error("Request error", "error", err, "stack", string(stack))
panicLogger.Error("Request error", "error", r, "stack", string(stack))
// if response has already been written, skip.
if c.Written() {
@@ -131,8 +134,8 @@ func Recovery() macaron.Handler {
c.Data["Theme"] = setting.DefaultTheme
if setting.Env == setting.Dev {
if theErr, ok := err.(error); ok {
c.Data["Title"] = theErr.Error()
if err, ok := r.(error); ok {
c.Data["Title"] = err.Error()
}
c.Data["ErrorMsg"] = string(stack)