mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Handle wrapped errors (#29223)
* Chore: 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:
@@ -2,13 +2,14 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (hs *HTTPServer) AdminProvisioningReloadDashboards(c *models.ReqContext) Response {
|
func (hs *HTTPServer) AdminProvisioningReloadDashboards(c *models.ReqContext) Response {
|
||||||
err := hs.ProvisioningService.ProvisionDashboards()
|
err := hs.ProvisioningService.ProvisionDashboards()
|
||||||
if err != nil && err != context.Canceled {
|
if err != nil && !errors.Is(err, context.Canceled) {
|
||||||
return Error(500, "", err)
|
return Error(500, "", err)
|
||||||
}
|
}
|
||||||
return Success("Dashboards config reloaded")
|
return Success("Dashboards config reloaded")
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ func AdminUpdateUserPermissions(c *models.ReqContext, form dtos.AdminUpdateUserP
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
if err == models.ErrLastGrafanaAdmin {
|
if errors.Is(err, models.ErrLastGrafanaAdmin) {
|
||||||
return Error(400, models.ErrLastGrafanaAdmin.Error(), nil)
|
return Error(400, models.ErrLastGrafanaAdmin.Error(), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ func AdminDeleteUser(c *models.ReqContext) Response {
|
|||||||
cmd := models.DeleteUserCommand{UserId: userID}
|
cmd := models.DeleteUserCommand{UserId: userID}
|
||||||
|
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
if err == models.ErrUserNotFound {
|
if errors.Is(err, models.ErrUserNotFound) {
|
||||||
return Error(404, models.ErrUserNotFound.Error(), nil)
|
return Error(404, models.ErrUserNotFound.Error(), nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to delete user", err)
|
return Error(500, "Failed to delete user", err)
|
||||||
@@ -126,13 +126,13 @@ func (hs *HTTPServer) AdminDisableUser(c *models.ReqContext) Response {
|
|||||||
|
|
||||||
// External users shouldn't be disabled from API
|
// External users shouldn't be disabled from API
|
||||||
authInfoQuery := &models.GetAuthInfoQuery{UserId: userID}
|
authInfoQuery := &models.GetAuthInfoQuery{UserId: userID}
|
||||||
if err := bus.Dispatch(authInfoQuery); err != models.ErrUserNotFound {
|
if err := bus.Dispatch(authInfoQuery); !errors.Is(err, models.ErrUserNotFound) {
|
||||||
return Error(500, "Could not disable external user", nil)
|
return Error(500, "Could not disable external user", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
disableCmd := models.DisableUserCommand{UserId: userID, IsDisabled: true}
|
disableCmd := models.DisableUserCommand{UserId: userID, IsDisabled: true}
|
||||||
if err := bus.Dispatch(&disableCmd); err != nil {
|
if err := bus.Dispatch(&disableCmd); err != nil {
|
||||||
if err == models.ErrUserNotFound {
|
if errors.Is(err, models.ErrUserNotFound) {
|
||||||
return Error(404, models.ErrUserNotFound.Error(), nil)
|
return Error(404, models.ErrUserNotFound.Error(), nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to disable user", err)
|
return Error(500, "Failed to disable user", err)
|
||||||
@@ -152,13 +152,13 @@ func AdminEnableUser(c *models.ReqContext) Response {
|
|||||||
|
|
||||||
// External users shouldn't be disabled from API
|
// External users shouldn't be disabled from API
|
||||||
authInfoQuery := &models.GetAuthInfoQuery{UserId: userID}
|
authInfoQuery := &models.GetAuthInfoQuery{UserId: userID}
|
||||||
if err := bus.Dispatch(authInfoQuery); err != models.ErrUserNotFound {
|
if err := bus.Dispatch(authInfoQuery); !errors.Is(err, models.ErrUserNotFound) {
|
||||||
return Error(500, "Could not enable external user", nil)
|
return Error(500, "Could not enable external user", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
disableCmd := models.DisableUserCommand{UserId: userID, IsDisabled: false}
|
disableCmd := models.DisableUserCommand{UserId: userID, IsDisabled: false}
|
||||||
if err := bus.Dispatch(&disableCmd); err != nil {
|
if err := bus.Dispatch(&disableCmd); err != nil {
|
||||||
if err == models.ErrUserNotFound {
|
if errors.Is(err, models.ErrUserNotFound) {
|
||||||
return Error(404, models.ErrUserNotFound.Error(), nil)
|
return Error(404, models.ErrUserNotFound.Error(), nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to enable user", err)
|
return Error(500, "Failed to enable user", err)
|
||||||
|
|||||||
@@ -140,10 +140,11 @@ func AlertTest(c *models.ReqContext, dto dtos.AlertTestCommand) Response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&backendCmd); err != nil {
|
if err := bus.Dispatch(&backendCmd); err != nil {
|
||||||
if validationErr, ok := err.(alerting.ValidationError); ok {
|
var validationErr alerting.ValidationError
|
||||||
|
if errors.As(err, &validationErr) {
|
||||||
return Error(422, validationErr.Error(), nil)
|
return Error(422, validationErr.Error(), nil)
|
||||||
}
|
}
|
||||||
if err == models.ErrDataSourceAccessDenied {
|
if errors.Is(err, models.ErrDataSourceAccessDenied) {
|
||||||
return Error(403, "Access denied to datasource", err)
|
return Error(403, "Access denied to datasource", err)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to test rule", err)
|
return Error(500, "Failed to test rule", err)
|
||||||
@@ -292,7 +293,7 @@ func UpdateAlertNotification(c *models.ReqContext, cmd models.UpdateAlertNotific
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
if err == models.ErrAlertNotificationNotFound {
|
if errors.Is(err, models.ErrAlertNotificationNotFound) {
|
||||||
return Error(404, err.Error(), err)
|
return Error(404, err.Error(), err)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to update alert notification", err)
|
return Error(500, "Failed to update alert notification", err)
|
||||||
@@ -320,7 +321,7 @@ func UpdateAlertNotificationByUID(c *models.ReqContext, cmd models.UpdateAlertNo
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
if err == models.ErrAlertNotificationNotFound {
|
if errors.Is(err, models.ErrAlertNotificationNotFound) {
|
||||||
return Error(404, err.Error(), nil)
|
return Error(404, err.Error(), nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to update alert notification", err)
|
return Error(500, "Failed to update alert notification", err)
|
||||||
@@ -393,7 +394,7 @@ func DeleteAlertNotification(c *models.ReqContext) Response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
if err == models.ErrAlertNotificationNotFound {
|
if errors.Is(err, models.ErrAlertNotificationNotFound) {
|
||||||
return Error(404, err.Error(), nil)
|
return Error(404, err.Error(), nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to delete alert notification", err)
|
return Error(500, "Failed to delete alert notification", err)
|
||||||
@@ -409,7 +410,7 @@ func DeleteAlertNotificationByUID(c *models.ReqContext) Response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
if err == models.ErrAlertNotificationNotFound {
|
if errors.Is(err, models.ErrAlertNotificationNotFound) {
|
||||||
return Error(404, err.Error(), nil)
|
return Error(404, err.Error(), nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to delete alert notification", err)
|
return Error(500, "Failed to delete alert notification", err)
|
||||||
@@ -433,7 +434,7 @@ func NotificationTest(c *models.ReqContext, dto dtos.NotificationTestCommand) Re
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(cmd); err != nil {
|
if err := bus.Dispatch(cmd); err != nil {
|
||||||
if err == models.ErrSmtpNotEnabled {
|
if errors.Is(err, models.ErrSmtpNotEnabled) {
|
||||||
return Error(412, err.Error(), err)
|
return Error(412, err.Error(), err)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to send alert notifications", err)
|
return Error(500, "Failed to send alert notifications", err)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/dtos"
|
"github.com/grafana/grafana/pkg/api/dtos"
|
||||||
@@ -70,10 +71,10 @@ func (hs *HTTPServer) AddAPIKey(c *models.ReqContext, cmd models.AddApiKeyComman
|
|||||||
cmd.Key = newKeyInfo.HashedKey
|
cmd.Key = newKeyInfo.HashedKey
|
||||||
|
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
if err == models.ErrInvalidApiKeyExpiration {
|
if errors.Is(err, models.ErrInvalidApiKeyExpiration) {
|
||||||
return Error(400, err.Error(), nil)
|
return Error(400, err.Error(), nil)
|
||||||
}
|
}
|
||||||
if err == models.ErrDuplicateApiKey {
|
if errors.Is(err, models.ErrDuplicateApiKey) {
|
||||||
return Error(409, err.Error(), nil)
|
return Error(409, err.Error(), nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to add API Key", err)
|
return Error(500, "Failed to add API Key", err)
|
||||||
|
|||||||
@@ -498,7 +498,7 @@ func CalculateDashboardDiff(c *models.ReqContext, apiOptions dtos.CalculateDiffO
|
|||||||
|
|
||||||
result, err := dashdiffs.CalculateDiff(&options)
|
result, err := dashdiffs.CalculateDiff(&options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == models.ErrDashboardVersionNotFound {
|
if errors.Is(err, models.ErrDashboardVersionNotFound) {
|
||||||
return Error(404, "Dashboard version not found", err)
|
return Error(404, "Dashboard version not found", err)
|
||||||
}
|
}
|
||||||
return Error(500, "Unable to compute diff", err)
|
return Error(500, "Unable to compute diff", err)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/dtos"
|
"github.com/grafana/grafana/pkg/api/dtos"
|
||||||
@@ -77,8 +78,7 @@ func UpdateDashboardPermissions(c *models.ReqContext, apiCmd dtos.UpdateDashboar
|
|||||||
|
|
||||||
if okToUpdate, err := g.CheckPermissionBeforeUpdate(models.PERMISSION_ADMIN, cmd.Items); err != nil || !okToUpdate {
|
if okToUpdate, err := g.CheckPermissionBeforeUpdate(models.PERMISSION_ADMIN, cmd.Items); err != nil || !okToUpdate {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == guardian.ErrGuardianPermissionExists ||
|
if errors.Is(err, guardian.ErrGuardianPermissionExists) || errors.Is(err, guardian.ErrGuardianOverride) {
|
||||||
err == guardian.ErrGuardianOverride {
|
|
||||||
return Error(400, err.Error(), err)
|
return Error(400, err.Error(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,7 +89,8 @@ func UpdateDashboardPermissions(c *models.ReqContext, apiCmd dtos.UpdateDashboar
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
if err == models.ErrDashboardAclInfoMissing || err == models.ErrDashboardPermissionDashboardEmpty {
|
if errors.Is(err, models.ErrDashboardAclInfoMissing) ||
|
||||||
|
errors.Is(err, models.ErrDashboardPermissionDashboardEmpty) {
|
||||||
return Error(409, err.Error(), err)
|
return Error(409, err.Error(), err)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to create permission", err)
|
return Error(500, "Failed to create permission", err)
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ func (hs *HTTPServer) ProxyDataSourceRequest(c *models.ReqContext) {
|
|||||||
dsID := c.ParamsInt64(":id")
|
dsID := c.ParamsInt64(":id")
|
||||||
ds, err := hs.DatasourceCache.GetDatasource(dsID, c.SignedInUser, c.SkipCache)
|
ds, err := hs.DatasourceCache.GetDatasource(dsID, c.SignedInUser, c.SkipCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == models.ErrDataSourceAccessDenied {
|
if errors.Is(err, models.ErrDataSourceAccessDenied) {
|
||||||
c.JsonApiErr(403, "Access denied to datasource", err)
|
c.JsonApiErr(403, "Access denied to datasource", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
@@ -64,7 +65,7 @@ func GetDataSourceById(c *models.ReqContext) Response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&query); err != nil {
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
if err == models.ErrDataSourceNotFound {
|
if errors.Is(err, models.ErrDataSourceNotFound) {
|
||||||
return Error(404, "Data source not found", nil)
|
return Error(404, "Data source not found", nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to query datasources", err)
|
return Error(500, "Failed to query datasources", err)
|
||||||
@@ -111,7 +112,7 @@ func DeleteDataSourceByName(c *models.ReqContext) Response {
|
|||||||
|
|
||||||
getCmd := &models.GetDataSourceByNameQuery{Name: name, OrgId: c.OrgId}
|
getCmd := &models.GetDataSourceByNameQuery{Name: name, OrgId: c.OrgId}
|
||||||
if err := bus.Dispatch(getCmd); err != nil {
|
if err := bus.Dispatch(getCmd); err != nil {
|
||||||
if err == models.ErrDataSourceNotFound {
|
if errors.Is(err, models.ErrDataSourceNotFound) {
|
||||||
return Error(404, "Data source not found", nil)
|
return Error(404, "Data source not found", nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to delete datasource", err)
|
return Error(500, "Failed to delete datasource", err)
|
||||||
@@ -153,7 +154,7 @@ func AddDataSource(c *models.ReqContext, cmd models.AddDataSourceCommand) Respon
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
if err == models.ErrDataSourceNameExists || err == models.ErrDataSourceUidExists {
|
if errors.Is(err, models.ErrDataSourceNameExists) || errors.Is(err, models.ErrDataSourceUidExists) {
|
||||||
return Error(409, err.Error(), err)
|
return Error(409, err.Error(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,7 +185,7 @@ func UpdateDataSource(c *models.ReqContext, cmd models.UpdateDataSourceCommand)
|
|||||||
|
|
||||||
err = bus.Dispatch(&cmd)
|
err = bus.Dispatch(&cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == models.ErrDataSourceUpdatingOldVersion {
|
if errors.Is(err, models.ErrDataSourceUpdatingOldVersion) {
|
||||||
return Error(500, "Failed to update datasource. Reload new version and try again", err)
|
return Error(500, "Failed to update datasource. Reload new version and try again", err)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to update datasource", err)
|
return Error(500, "Failed to update datasource", err)
|
||||||
@@ -196,7 +197,7 @@ func UpdateDataSource(c *models.ReqContext, cmd models.UpdateDataSourceCommand)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&query); err != nil {
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
if err == models.ErrDataSourceNotFound {
|
if errors.Is(err, models.ErrDataSourceNotFound) {
|
||||||
return Error(404, "Data source not found", nil)
|
return Error(404, "Data source not found", nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to query datasources", err)
|
return Error(500, "Failed to query datasources", err)
|
||||||
@@ -254,7 +255,7 @@ func GetDataSourceByName(c *models.ReqContext) Response {
|
|||||||
query := models.GetDataSourceByNameQuery{Name: c.Params(":name"), OrgId: c.OrgId}
|
query := models.GetDataSourceByNameQuery{Name: c.Params(":name"), OrgId: c.OrgId}
|
||||||
|
|
||||||
if err := bus.Dispatch(&query); err != nil {
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
if err == models.ErrDataSourceNotFound {
|
if errors.Is(err, models.ErrDataSourceNotFound) {
|
||||||
return Error(404, "Data source not found", nil)
|
return Error(404, "Data source not found", nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to query datasources", err)
|
return Error(500, "Failed to query datasources", err)
|
||||||
@@ -269,7 +270,7 @@ func GetDataSourceIdByName(c *models.ReqContext) Response {
|
|||||||
query := models.GetDataSourceByNameQuery{Name: c.Params(":name"), OrgId: c.OrgId}
|
query := models.GetDataSourceByNameQuery{Name: c.Params(":name"), OrgId: c.OrgId}
|
||||||
|
|
||||||
if err := bus.Dispatch(&query); err != nil {
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
if err == models.ErrDataSourceNotFound {
|
if errors.Is(err, models.ErrDataSourceNotFound) {
|
||||||
return Error(404, "Data source not found", nil)
|
return Error(404, "Data source not found", nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to query datasources", err)
|
return Error(500, "Failed to query datasources", err)
|
||||||
@@ -288,7 +289,7 @@ func (hs *HTTPServer) CallDatasourceResource(c *models.ReqContext) {
|
|||||||
datasourceID := c.ParamsInt64(":id")
|
datasourceID := c.ParamsInt64(":id")
|
||||||
ds, err := hs.DatasourceCache.GetDatasource(datasourceID, c.SignedInUser, c.SkipCache)
|
ds, err := hs.DatasourceCache.GetDatasource(datasourceID, c.SignedInUser, c.SkipCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == models.ErrDataSourceAccessDenied {
|
if errors.Is(err, models.ErrDataSourceAccessDenied) {
|
||||||
c.JsonApiErr(403, "Access denied to datasource", err)
|
c.JsonApiErr(403, "Access denied to datasource", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -355,7 +356,7 @@ func (hs *HTTPServer) CheckDatasourceHealth(c *models.ReqContext) Response {
|
|||||||
|
|
||||||
ds, err := hs.DatasourceCache.GetDatasource(datasourceID, c.SignedInUser, c.SkipCache)
|
ds, err := hs.DatasourceCache.GetDatasource(datasourceID, c.SignedInUser, c.SkipCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == models.ErrDataSourceAccessDenied {
|
if errors.Is(err, models.ErrDataSourceAccessDenied) {
|
||||||
return Error(403, "Access denied to datasource", err)
|
return Error(403, "Access denied to datasource", err)
|
||||||
}
|
}
|
||||||
return Error(500, "Unable to load datasource metadata", err)
|
return Error(500, "Unable to load datasource metadata", err)
|
||||||
|
|||||||
@@ -134,24 +134,24 @@ func toFolderError(err error) Response {
|
|||||||
return Error(dashboardErr.StatusCode, err.Error(), err)
|
return Error(dashboardErr.StatusCode, err.Error(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == models.ErrFolderTitleEmpty ||
|
if errors.Is(err, models.ErrFolderTitleEmpty) ||
|
||||||
err == models.ErrFolderSameNameExists ||
|
errors.Is(err, models.ErrFolderSameNameExists) ||
|
||||||
err == models.ErrFolderWithSameUIDExists ||
|
errors.Is(err, models.ErrFolderWithSameUIDExists) ||
|
||||||
err == models.ErrDashboardTypeMismatch ||
|
errors.Is(err, models.ErrDashboardTypeMismatch) ||
|
||||||
err == models.ErrDashboardInvalidUid ||
|
errors.Is(err, models.ErrDashboardInvalidUid) ||
|
||||||
err == models.ErrDashboardUidTooLong {
|
errors.Is(err, models.ErrDashboardUidTooLong) {
|
||||||
return Error(400, err.Error(), nil)
|
return Error(400, err.Error(), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == models.ErrFolderAccessDenied {
|
if errors.Is(err, models.ErrFolderAccessDenied) {
|
||||||
return Error(403, "Access denied", err)
|
return Error(403, "Access denied", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == models.ErrFolderNotFound {
|
if errors.Is(err, models.ErrFolderNotFound) {
|
||||||
return JSON(404, util.DynMap{"status": "not-found", "message": models.ErrFolderNotFound.Error()})
|
return JSON(404, util.DynMap{"status": "not-found", "message": models.ErrFolderNotFound.Error()})
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == models.ErrFolderVersionMismatch {
|
if errors.Is(err, models.ErrFolderVersionMismatch) {
|
||||||
return JSON(412, util.DynMap{"status": "version-mismatch", "message": models.ErrFolderVersionMismatch.Error()})
|
return JSON(412, util.DynMap{"status": "version-mismatch", "message": models.ErrFolderVersionMismatch.Error()})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/dtos"
|
"github.com/grafana/grafana/pkg/api/dtos"
|
||||||
@@ -88,8 +89,8 @@ func UpdateFolderPermissions(c *models.ReqContext, apiCmd dtos.UpdateDashboardAc
|
|||||||
|
|
||||||
if okToUpdate, err := g.CheckPermissionBeforeUpdate(models.PERMISSION_ADMIN, cmd.Items); err != nil || !okToUpdate {
|
if okToUpdate, err := g.CheckPermissionBeforeUpdate(models.PERMISSION_ADMIN, cmd.Items); err != nil || !okToUpdate {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == guardian.ErrGuardianPermissionExists ||
|
if errors.Is(err, guardian.ErrGuardianPermissionExists) ||
|
||||||
err == guardian.ErrGuardianOverride {
|
errors.Is(err, guardian.ErrGuardianOverride) {
|
||||||
return Error(400, err.Error(), err)
|
return Error(400, err.Error(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,14 +101,14 @@ func UpdateFolderPermissions(c *models.ReqContext, apiCmd dtos.UpdateDashboardAc
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
if err == models.ErrDashboardAclInfoMissing {
|
if errors.Is(err, models.ErrDashboardAclInfoMissing) {
|
||||||
err = models.ErrFolderAclInfoMissing
|
err = models.ErrFolderAclInfoMissing
|
||||||
}
|
}
|
||||||
if err == models.ErrDashboardPermissionDashboardEmpty {
|
if errors.Is(err, models.ErrDashboardPermissionDashboardEmpty) {
|
||||||
err = models.ErrFolderPermissionFolderEmpty
|
err = models.ErrFolderPermissionFolderEmpty
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == models.ErrFolderAclInfoMissing || err == models.ErrFolderPermissionFolderEmpty {
|
if errors.Is(err, models.ErrFolderAclInfoMissing) || errors.Is(err, models.ErrFolderPermissionFolderEmpty) {
|
||||||
return Error(409, err.Error(), err)
|
return Error(409, err.Error(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
@@ -31,7 +32,7 @@ func getFSDataSources(c *models.ReqContext, enabledPlugins *plugins.EnabledPlugi
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&dsFilterQuery); err != nil {
|
if err := bus.Dispatch(&dsFilterQuery); err != nil {
|
||||||
if err != bus.ErrHandlerNotFound {
|
if !errors.Is(err, bus.ErrHandlerNotFound) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package api
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -134,7 +135,7 @@ func (hs *HTTPServer) Run(ctx context.Context) error {
|
|||||||
switch setting.Protocol {
|
switch setting.Protocol {
|
||||||
case setting.HTTPScheme, setting.SocketScheme:
|
case setting.HTTPScheme, setting.SocketScheme:
|
||||||
if err := hs.httpSrv.Serve(listener); err != nil {
|
if err := hs.httpSrv.Serve(listener); err != nil {
|
||||||
if err == http.ErrServerClosed {
|
if errors.Is(err, http.ErrServerClosed) {
|
||||||
hs.log.Debug("server was shutdown gracefully")
|
hs.log.Debug("server was shutdown gracefully")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -142,7 +143,7 @@ func (hs *HTTPServer) Run(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
case setting.HTTP2Scheme, setting.HTTPSScheme:
|
case setting.HTTP2Scheme, setting.HTTPSScheme:
|
||||||
if err := hs.httpSrv.ServeTLS(listener, setting.CertFile, setting.KeyFile); err != nil {
|
if err := hs.httpSrv.ServeTLS(listener, setting.CertFile, setting.KeyFile); err != nil {
|
||||||
if err == http.ErrServerClosed {
|
if errors.Is(err, http.ErrServerClosed) {
|
||||||
hs.log.Debug("server was shutdown gracefully")
|
hs.log.Debug("server was shutdown gracefully")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -167,7 +168,7 @@ func (hs *HTTPServer) PostSyncUserWithLDAP(c *models.ReqContext) Response {
|
|||||||
query := models.GetUserByIdQuery{Id: userId}
|
query := models.GetUserByIdQuery{Id: userId}
|
||||||
|
|
||||||
if err := bus.Dispatch(&query); err != nil { // validate the userId exists
|
if err := bus.Dispatch(&query); err != nil { // validate the userId exists
|
||||||
if err == models.ErrUserNotFound {
|
if errors.Is(err, models.ErrUserNotFound) {
|
||||||
return Error(404, models.ErrUserNotFound.Error(), nil)
|
return Error(404, models.ErrUserNotFound.Error(), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,7 +178,7 @@ func (hs *HTTPServer) PostSyncUserWithLDAP(c *models.ReqContext) Response {
|
|||||||
authModuleQuery := &models.GetAuthInfoQuery{UserId: query.Result.Id, AuthModule: models.AuthModuleLDAP}
|
authModuleQuery := &models.GetAuthInfoQuery{UserId: query.Result.Id, AuthModule: models.AuthModuleLDAP}
|
||||||
|
|
||||||
if err := bus.Dispatch(authModuleQuery); err != nil { // validate the userId comes from LDAP
|
if err := bus.Dispatch(authModuleQuery); err != nil { // validate the userId comes from LDAP
|
||||||
if err == models.ErrUserNotFound {
|
if errors.Is(err, models.ErrUserNotFound) {
|
||||||
return Error(404, models.ErrUserNotFound.Error(), nil)
|
return Error(404, models.ErrUserNotFound.Error(), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,7 +189,7 @@ func (hs *HTTPServer) PostSyncUserWithLDAP(c *models.ReqContext) Response {
|
|||||||
user, _, err := ldapServer.User(query.Result.Login)
|
user, _, err := ldapServer.User(query.Result.Login)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == multildap.ErrDidNotFindUser { // User was not in the LDAP server - we need to take action:
|
if errors.Is(err, multildap.ErrDidNotFindUser) { // User was not in the LDAP server - we need to take action:
|
||||||
if setting.AdminUser == query.Result.Login { // User is *the* Grafana Admin. We cannot disable it.
|
if setting.AdminUser == query.Result.Login { // User is *the* Grafana Admin. We cannot disable it.
|
||||||
errMsg := fmt.Sprintf(`Refusing to sync grafana super admin "%s" - it would be disabled`, query.Result.Login)
|
errMsg := fmt.Sprintf(`Refusing to sync grafana super admin "%s" - it would be disabled`, query.Result.Login)
|
||||||
logger.Error(errMsg)
|
logger.Error(errMsg)
|
||||||
@@ -197,7 +198,6 @@ func (hs *HTTPServer) PostSyncUserWithLDAP(c *models.ReqContext) Response {
|
|||||||
|
|
||||||
// Since the user was not in the LDAP server. Let's disable it.
|
// Since the user was not in the LDAP server. Let's disable it.
|
||||||
err := login.DisableExternalUser(query.Result.Login)
|
err := login.DisableExternalUser(query.Result.Login)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Error(http.StatusInternalServerError, "Failed to disable the user", err)
|
return Error(http.StatusInternalServerError, "Failed to disable the user", err)
|
||||||
}
|
}
|
||||||
@@ -314,7 +314,7 @@ func (hs *HTTPServer) GetUserFromLDAP(c *models.ReqContext) Response {
|
|||||||
cmd := &models.GetTeamsForLDAPGroupCommand{Groups: user.Groups}
|
cmd := &models.GetTeamsForLDAPGroupCommand{Groups: user.Groups}
|
||||||
err = bus.Dispatch(cmd)
|
err = bus.Dispatch(cmd)
|
||||||
|
|
||||||
if err != bus.ErrHandlerNotFound && err != nil {
|
if err != nil && !errors.Is(err, bus.ErrHandlerNotFound) {
|
||||||
return Error(http.StatusBadRequest, "Unable to find the teams for this user", err)
|
return Error(http.StatusBadRequest, "Unable to find the teams for this user", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -201,13 +201,14 @@ func (hs *HTTPServer) LoginPost(c *models.ReqContext, cmd dtos.LoginCommand) Res
|
|||||||
authModule = authQuery.AuthModule
|
authModule = authQuery.AuthModule
|
||||||
if err != nil {
|
if err != nil {
|
||||||
response = Error(401, "Invalid username or password", err)
|
response = Error(401, "Invalid username or password", err)
|
||||||
if err == login.ErrInvalidCredentials || err == login.ErrTooManyLoginAttempts || err == models.ErrUserNotFound {
|
if errors.Is(err, login.ErrInvalidCredentials) || errors.Is(err, login.ErrTooManyLoginAttempts) || errors.Is(err,
|
||||||
|
models.ErrUserNotFound) {
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do not expose disabled status,
|
// Do not expose disabled status,
|
||||||
// just show incorrect user credentials error (see #17947)
|
// just show incorrect user credentials error (see #17947)
|
||||||
if err == login.ErrUserDisabled {
|
if errors.Is(err, login.ErrUserDisabled) {
|
||||||
hs.log.Warn("User is disabled", "user", cmd.User)
|
hs.log.Warn("User is disabled", "user", cmd.User)
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
@@ -263,7 +264,8 @@ func (hs *HTTPServer) Logout(c *models.ReqContext) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := hs.AuthTokenService.RevokeToken(c.Req.Context(), c.UserToken); err != nil && err != models.ErrUserTokenNotFound {
|
err := hs.AuthTokenService.RevokeToken(c.Req.Context(), c.UserToken)
|
||||||
|
if err != nil && !errors.Is(err, models.ErrUserTokenNotFound) {
|
||||||
hs.log.Error("failed to revoke auth token", "error", err)
|
hs.log.Error("failed to revoke auth token", "error", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -146,7 +146,8 @@ func (hs *HTTPServer) OAuthLogin(ctx *models.ReqContext) {
|
|||||||
// get user info
|
// get user info
|
||||||
userInfo, err := connect.UserInfo(client, token)
|
userInfo, err := connect.UserInfo(client, token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if sErr, ok := err.(*social.Error); ok {
|
var sErr *social.Error
|
||||||
|
if errors.As(err, &sErr) {
|
||||||
hs.handleOAuthLoginErrorWithRedirect(ctx, loginInfo, sErr)
|
hs.handleOAuthLoginErrorWithRedirect(ctx, loginInfo, sErr)
|
||||||
} else {
|
} else {
|
||||||
hs.handleOAuthLoginError(ctx, loginInfo, LoginError{
|
hs.handleOAuthLoginError(ctx, loginInfo, LoginError{
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ func (hs *HTTPServer) QueryMetrics(c *models.ReqContext, reqDto dtos.MetricReque
|
|||||||
|
|
||||||
ds, err := hs.DatasourceCache.GetDatasource(datasourceId, c.SignedInUser, c.SkipCache)
|
ds, err := hs.DatasourceCache.GetDatasource(datasourceId, c.SignedInUser, c.SkipCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == models.ErrDataSourceAccessDenied {
|
if errors.Is(err, models.ErrDataSourceAccessDenied) {
|
||||||
return Error(403, "Access denied to datasource", err)
|
return Error(403, "Access denied to datasource", err)
|
||||||
}
|
}
|
||||||
return Error(500, "Unable to load datasource meta data", err)
|
return Error(500, "Unable to load datasource meta data", err)
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/dtos"
|
"github.com/grafana/grafana/pkg/api/dtos"
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/infra/metrics"
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
||||||
@@ -23,7 +25,7 @@ func GetOrgByID(c *models.ReqContext) Response {
|
|||||||
func GetOrgByName(c *models.ReqContext) Response {
|
func GetOrgByName(c *models.ReqContext) Response {
|
||||||
query := models.GetOrgByNameQuery{Name: c.Params(":name")}
|
query := models.GetOrgByNameQuery{Name: c.Params(":name")}
|
||||||
if err := bus.Dispatch(&query); err != nil {
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
if err == models.ErrOrgNotFound {
|
if errors.Is(err, models.ErrOrgNotFound) {
|
||||||
return Error(404, "Organization not found", err)
|
return Error(404, "Organization not found", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,7 +52,7 @@ func getOrgHelper(orgID int64) Response {
|
|||||||
query := models.GetOrgByIdQuery{Id: orgID}
|
query := models.GetOrgByIdQuery{Id: orgID}
|
||||||
|
|
||||||
if err := bus.Dispatch(&query); err != nil {
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
if err == models.ErrOrgNotFound {
|
if errors.Is(err, models.ErrOrgNotFound) {
|
||||||
return Error(404, "Organization not found", err)
|
return Error(404, "Organization not found", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,7 +84,7 @@ func CreateOrg(c *models.ReqContext, cmd models.CreateOrgCommand) Response {
|
|||||||
|
|
||||||
cmd.UserId = c.UserId
|
cmd.UserId = c.UserId
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
if err == models.ErrOrgNameTaken {
|
if errors.Is(err, models.ErrOrgNameTaken) {
|
||||||
return Error(409, "Organization name taken", err)
|
return Error(409, "Organization name taken", err)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to create organization", err)
|
return Error(500, "Failed to create organization", err)
|
||||||
@@ -109,7 +111,7 @@ func UpdateOrg(c *models.ReqContext, form dtos.UpdateOrgForm) Response {
|
|||||||
func updateOrgHelper(form dtos.UpdateOrgForm, orgID int64) Response {
|
func updateOrgHelper(form dtos.UpdateOrgForm, orgID int64) Response {
|
||||||
cmd := models.UpdateOrgCommand{Name: form.Name, OrgId: orgID}
|
cmd := models.UpdateOrgCommand{Name: form.Name, OrgId: orgID}
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
if err == models.ErrOrgNameTaken {
|
if errors.Is(err, models.ErrOrgNameTaken) {
|
||||||
return Error(400, "Organization name taken", err)
|
return Error(400, "Organization name taken", err)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to update organization", err)
|
return Error(500, "Failed to update organization", err)
|
||||||
@@ -151,7 +153,7 @@ func updateOrgAddressHelper(form dtos.UpdateOrgAddressForm, orgID int64) Respons
|
|||||||
// GET /api/orgs/:orgId
|
// GET /api/orgs/:orgId
|
||||||
func DeleteOrgByID(c *models.ReqContext) Response {
|
func DeleteOrgByID(c *models.ReqContext) Response {
|
||||||
if err := bus.Dispatch(&models.DeleteOrgCommand{Id: c.ParamsInt64(":orgId")}); err != nil {
|
if err := bus.Dispatch(&models.DeleteOrgCommand{Id: c.ParamsInt64(":orgId")}); err != nil {
|
||||||
if err == models.ErrOrgNotFound {
|
if errors.Is(err, models.ErrOrgNotFound) {
|
||||||
return Error(404, "Failed to delete organization. ID not found", nil)
|
return Error(404, "Failed to delete organization. ID not found", nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to update organization", err)
|
return Error(500, "Failed to update organization", err)
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ func AddOrgInvite(c *models.ReqContext, inviteDto dtos.AddInviteForm) Response {
|
|||||||
// first try get existing user
|
// first try get existing user
|
||||||
userQuery := models.GetUserByLoginQuery{LoginOrEmail: inviteDto.LoginOrEmail}
|
userQuery := models.GetUserByLoginQuery{LoginOrEmail: inviteDto.LoginOrEmail}
|
||||||
if err := bus.Dispatch(&userQuery); err != nil {
|
if err := bus.Dispatch(&userQuery); err != nil {
|
||||||
if err != models.ErrUserNotFound {
|
if !errors.Is(err, models.ErrUserNotFound) {
|
||||||
return Error(500, "Failed to query db for existing user check", err)
|
return Error(500, "Failed to query db for existing user check", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -79,7 +79,7 @@ func AddOrgInvite(c *models.ReqContext, inviteDto dtos.AddInviteForm) Response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&emailCmd); err != nil {
|
if err := bus.Dispatch(&emailCmd); err != nil {
|
||||||
if err == models.ErrSmtpNotEnabled {
|
if errors.Is(err, models.ErrSmtpNotEnabled) {
|
||||||
return Error(412, err.Error(), err)
|
return Error(412, err.Error(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,7 +101,7 @@ func inviteExistingUserToOrg(c *models.ReqContext, user *models.User, inviteDto
|
|||||||
// user exists, add org role
|
// user exists, add org role
|
||||||
createOrgUserCmd := models.AddOrgUserCommand{OrgId: c.OrgId, UserId: user.Id, Role: inviteDto.Role}
|
createOrgUserCmd := models.AddOrgUserCommand{OrgId: c.OrgId, UserId: user.Id, Role: inviteDto.Role}
|
||||||
if err := bus.Dispatch(&createOrgUserCmd); err != nil {
|
if err := bus.Dispatch(&createOrgUserCmd); err != nil {
|
||||||
if err == models.ErrOrgUserAlreadyAdded {
|
if errors.Is(err, models.ErrOrgUserAlreadyAdded) {
|
||||||
return Error(412, fmt.Sprintf("User %s is already added to organization", inviteDto.LoginOrEmail), err)
|
return Error(412, fmt.Sprintf("User %s is already added to organization", inviteDto.LoginOrEmail), err)
|
||||||
}
|
}
|
||||||
return Error(500, "Error while trying to create org user", err)
|
return Error(500, "Error while trying to create org user", err)
|
||||||
@@ -143,7 +143,7 @@ func RevokeInvite(c *models.ReqContext) Response {
|
|||||||
func GetInviteInfoByCode(c *models.ReqContext) Response {
|
func GetInviteInfoByCode(c *models.ReqContext) Response {
|
||||||
query := models.GetTempUserByCodeQuery{Code: c.Params(":code")}
|
query := models.GetTempUserByCodeQuery{Code: c.Params(":code")}
|
||||||
if err := bus.Dispatch(&query); err != nil {
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
if err == models.ErrTempUserNotFound {
|
if errors.Is(err, models.ErrTempUserNotFound) {
|
||||||
return Error(404, "Invite not found", nil)
|
return Error(404, "Invite not found", nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to get invite", err)
|
return Error(500, "Failed to get invite", err)
|
||||||
@@ -166,7 +166,7 @@ func (hs *HTTPServer) CompleteInvite(c *models.ReqContext, completeInvite dtos.C
|
|||||||
query := models.GetTempUserByCodeQuery{Code: completeInvite.InviteCode}
|
query := models.GetTempUserByCodeQuery{Code: completeInvite.InviteCode}
|
||||||
|
|
||||||
if err := bus.Dispatch(&query); err != nil {
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
if err == models.ErrTempUserNotFound {
|
if errors.Is(err, models.ErrTempUserNotFound) {
|
||||||
return Error(404, "Invite not found", nil)
|
return Error(404, "Invite not found", nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to get invite", err)
|
return Error(500, "Failed to get invite", err)
|
||||||
@@ -234,7 +234,7 @@ func applyUserInvite(user *models.User, invite *models.TempUserDTO, setActive bo
|
|||||||
// add to org
|
// add to org
|
||||||
addOrgUserCmd := models.AddOrgUserCommand{OrgId: invite.OrgId, UserId: user.Id, Role: invite.Role}
|
addOrgUserCmd := models.AddOrgUserCommand{OrgId: invite.OrgId, UserId: user.Id, Role: invite.Role}
|
||||||
if err := bus.Dispatch(&addOrgUserCmd); err != nil {
|
if err := bus.Dispatch(&addOrgUserCmd); err != nil {
|
||||||
if err != models.ErrOrgUserAlreadyAdded {
|
if !errors.Is(err, models.ErrOrgUserAlreadyAdded) {
|
||||||
return false, Error(500, "Error while trying to create org user", err)
|
return false, Error(500, "Error while trying to create org user", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/dtos"
|
"github.com/grafana/grafana/pkg/api/dtos"
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
@@ -35,7 +37,7 @@ func addOrgUserHelper(cmd models.AddOrgUserCommand) Response {
|
|||||||
cmd.UserId = userToAdd.Id
|
cmd.UserId = userToAdd.Id
|
||||||
|
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
if err == models.ErrOrgUserAlreadyAdded {
|
if errors.Is(err, models.ErrOrgUserAlreadyAdded) {
|
||||||
return JSON(409, util.DynMap{
|
return JSON(409, util.DynMap{
|
||||||
"message": "User is already member of this organization",
|
"message": "User is already member of this organization",
|
||||||
"userId": cmd.UserId,
|
"userId": cmd.UserId,
|
||||||
@@ -159,7 +161,7 @@ func updateOrgUserHelper(cmd models.UpdateOrgUserCommand) Response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
if err == models.ErrLastOrgAdmin {
|
if errors.Is(err, models.ErrLastOrgAdmin) {
|
||||||
return Error(400, "Cannot change role so that there is no organization admin left", nil)
|
return Error(400, "Cannot change role so that there is no organization admin left", nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed update org user", err)
|
return Error(500, "Failed update org user", err)
|
||||||
@@ -187,7 +189,7 @@ func RemoveOrgUser(c *models.ReqContext) Response {
|
|||||||
|
|
||||||
func removeOrgUserHelper(cmd *models.RemoveOrgUserCommand) Response {
|
func removeOrgUserHelper(cmd *models.RemoveOrgUserCommand) Response {
|
||||||
if err := bus.Dispatch(cmd); err != nil {
|
if err := bus.Dispatch(cmd); err != nil {
|
||||||
if err == models.ErrLastOrgAdmin {
|
if errors.Is(err, models.ErrLastOrgAdmin) {
|
||||||
return Error(400, "Cannot remove last organization admin", nil)
|
return Error(400, "Cannot remove last organization admin", nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to remove user from organization", err)
|
return Error(500, "Failed to remove user from organization", err)
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/dtos"
|
"github.com/grafana/grafana/pkg/api/dtos"
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
@@ -35,7 +37,7 @@ func ResetPassword(c *models.ReqContext, form dtos.ResetUserPasswordForm) Respon
|
|||||||
query := models.ValidateResetPasswordCodeQuery{Code: form.Code}
|
query := models.ValidateResetPasswordCodeQuery{Code: form.Code}
|
||||||
|
|
||||||
if err := bus.Dispatch(&query); err != nil {
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
if err == models.ErrInvalidEmailCode {
|
if errors.Is(err, models.ErrInvalidEmailCode) {
|
||||||
return Error(400, "Invalid or expired reset password code", nil)
|
return Error(400, "Invalid or expired reset password code", nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Unknown error validating email code", err)
|
return Error(500, "Unknown error validating email code", err)
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ func (hs *HTTPServer) getPluginContext(pluginID string, user *models.SignedInUse
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
// models.ErrPluginSettingNotFound is expected if there's no row found for plugin setting in database (if non-app plugin).
|
// models.ErrPluginSettingNotFound is expected if there's no row found for plugin setting in database (if non-app plugin).
|
||||||
// If it's not this expected error something is wrong with cache or database and we return the error to the client.
|
// If it's not this expected error something is wrong with cache or database and we return the error to the client.
|
||||||
if err != models.ErrPluginSettingNotFound {
|
if !errors.Is(err, models.ErrPluginSettingNotFound) {
|
||||||
return pc, errutil.Wrap("Failed to get plugin settings", err)
|
return pc, errutil.Wrap("Failed to get plugin settings", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -166,7 +166,7 @@ func GetPluginSettingByID(c *models.ReqContext) Response {
|
|||||||
|
|
||||||
query := models.GetPluginSettingByIdQuery{PluginId: pluginID, OrgId: c.OrgId}
|
query := models.GetPluginSettingByIdQuery{PluginId: pluginID, OrgId: c.OrgId}
|
||||||
if err := bus.Dispatch(&query); err != nil {
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
if err != models.ErrPluginSettingNotFound {
|
if !errors.Is(err, models.ErrPluginSettingNotFound) {
|
||||||
return Error(500, "Failed to get login settings", nil)
|
return Error(500, "Failed to get login settings", nil)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -200,8 +200,9 @@ func GetPluginDashboards(c *models.ReqContext) Response {
|
|||||||
|
|
||||||
list, err := plugins.GetPluginDashboards(c.OrgId, pluginID)
|
list, err := plugins.GetPluginDashboards(c.OrgId, pluginID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if notfound, ok := err.(plugins.PluginNotFoundError); ok {
|
var notFound plugins.PluginNotFoundError
|
||||||
return Error(404, notfound.Error(), nil)
|
if errors.As(err, ¬Found) {
|
||||||
|
return Error(404, notFound.Error(), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
return Error(500, "Failed to get plugin dashboards", err)
|
return Error(500, "Failed to get plugin dashboards", err)
|
||||||
@@ -216,8 +217,9 @@ func GetPluginMarkdown(c *models.ReqContext) Response {
|
|||||||
|
|
||||||
content, err := plugins.GetPluginMarkdown(pluginID, name)
|
content, err := plugins.GetPluginMarkdown(pluginID, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if notfound, ok := err.(plugins.PluginNotFoundError); ok {
|
var notFound plugins.PluginNotFoundError
|
||||||
return Error(404, notfound.Error(), nil)
|
if errors.As(err, ¬Found) {
|
||||||
|
return Error(404, notFound.Error(), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
return Error(500, "Could not get markdown file", err)
|
return Error(500, "Could not get markdown file", err)
|
||||||
@@ -291,7 +293,7 @@ func (hs *HTTPServer) CheckHealth(c *models.ReqContext) Response {
|
|||||||
|
|
||||||
pCtx, err := hs.getPluginContext(pluginID, c.SignedInUser)
|
pCtx, err := hs.getPluginContext(pluginID, c.SignedInUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == ErrPluginNotFound {
|
if errors.Is(err, ErrPluginNotFound) {
|
||||||
return Error(404, "Plugin not found", nil)
|
return Error(404, "Plugin not found", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -334,7 +336,7 @@ func (hs *HTTPServer) CallResource(c *models.ReqContext) {
|
|||||||
|
|
||||||
pCtx, err := hs.getPluginContext(pluginID, c.SignedInUser)
|
pCtx, err := hs.getPluginContext(pluginID, c.SignedInUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == ErrPluginNotFound {
|
if errors.Is(err, ErrPluginNotFound) {
|
||||||
c.JsonApiErr(404, "Plugin not found", nil)
|
c.JsonApiErr(404, "Plugin not found", nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -66,13 +67,12 @@ func (hs *HTTPServer) RenderToPng(c *models.ReqContext) {
|
|||||||
DeviceScaleFactor: scale,
|
DeviceScaleFactor: scale,
|
||||||
Headers: headers,
|
Headers: headers,
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
if err != nil && err == rendering.ErrTimeout {
|
if errors.Is(err, rendering.ErrTimeout) {
|
||||||
c.Handle(500, err.Error(), err)
|
c.Handle(500, err.Error(), err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if errors.Is(err, rendering.ErrPhantomJSNotInstalled) {
|
||||||
if err != nil && err == rendering.ErrPhantomJSNotInstalled {
|
|
||||||
if strings.HasPrefix(runtime.GOARCH, "arm") {
|
if strings.HasPrefix(runtime.GOARCH, "arm") {
|
||||||
c.Handle(500, "Rendering failed - PhantomJS isn't included in arm build per default", err)
|
c.Handle(500, "Rendering failed - PhantomJS isn't included in arm build per default", err)
|
||||||
} else {
|
} else {
|
||||||
@@ -81,7 +81,6 @@ func (hs *HTTPServer) RenderToPng(c *models.ReqContext) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
c.Handle(500, "Rendering failed.", err)
|
c.Handle(500, "Rendering failed.", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ func verifyUserSignUpEmail(email string, code string) (bool, Response) {
|
|||||||
query := models.GetTempUserByCodeQuery{Code: code}
|
query := models.GetTempUserByCodeQuery{Code: code}
|
||||||
|
|
||||||
if err := bus.Dispatch(&query); err != nil {
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
if err == models.ErrTempUserNotFound {
|
if errors.Is(err, models.ErrTempUserNotFound) {
|
||||||
return false, Error(404, "Invalid email verification code", nil)
|
return false, Error(404, "Invalid email verification code", nil)
|
||||||
}
|
}
|
||||||
return false, Error(500, "Failed to read temp user", err)
|
return false, Error(500, "Failed to read temp user", err)
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/dtos"
|
"github.com/grafana/grafana/pkg/api/dtos"
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
@@ -17,7 +19,7 @@ func (hs *HTTPServer) CreateTeam(c *models.ReqContext, cmd models.CreateTeamComm
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := hs.Bus.Dispatch(&cmd); err != nil {
|
if err := hs.Bus.Dispatch(&cmd); err != nil {
|
||||||
if err == models.ErrTeamNameTaken {
|
if errors.Is(err, models.ErrTeamNameTaken) {
|
||||||
return Error(409, "Team name taken", err)
|
return Error(409, "Team name taken", err)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to create Team", err)
|
return Error(500, "Failed to create Team", err)
|
||||||
@@ -59,7 +61,7 @@ func (hs *HTTPServer) UpdateTeam(c *models.ReqContext, cmd models.UpdateTeamComm
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := hs.Bus.Dispatch(&cmd); err != nil {
|
if err := hs.Bus.Dispatch(&cmd); err != nil {
|
||||||
if err == models.ErrTeamNameTaken {
|
if errors.Is(err, models.ErrTeamNameTaken) {
|
||||||
return Error(400, "Team name taken", err)
|
return Error(400, "Team name taken", err)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to update Team", err)
|
return Error(500, "Failed to update Team", err)
|
||||||
@@ -79,7 +81,7 @@ func (hs *HTTPServer) DeleteTeamByID(c *models.ReqContext) Response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := hs.Bus.Dispatch(&models.DeleteTeamCommand{OrgId: orgId, Id: teamId}); err != nil {
|
if err := hs.Bus.Dispatch(&models.DeleteTeamCommand{OrgId: orgId, Id: teamId}); err != nil {
|
||||||
if err == models.ErrTeamNotFound {
|
if errors.Is(err, models.ErrTeamNotFound) {
|
||||||
return Error(404, "Failed to delete Team. ID not found", nil)
|
return Error(404, "Failed to delete Team. ID not found", nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to delete Team", err)
|
return Error(500, "Failed to delete Team", err)
|
||||||
@@ -131,7 +133,7 @@ func GetTeamByID(c *models.ReqContext) Response {
|
|||||||
query := models.GetTeamByIdQuery{OrgId: c.OrgId, Id: c.ParamsInt64(":teamId")}
|
query := models.GetTeamByIdQuery{OrgId: c.OrgId, Id: c.ParamsInt64(":teamId")}
|
||||||
|
|
||||||
if err := bus.Dispatch(&query); err != nil {
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
if err == models.ErrTeamNotFound {
|
if errors.Is(err, models.ErrTeamNotFound) {
|
||||||
return Error(404, "Team not found", err)
|
return Error(404, "Team not found", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/dtos"
|
"github.com/grafana/grafana/pkg/api/dtos"
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
@@ -39,11 +41,11 @@ func (hs *HTTPServer) AddTeamMember(c *models.ReqContext, cmd models.AddTeamMemb
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := hs.Bus.Dispatch(&cmd); err != nil {
|
if err := hs.Bus.Dispatch(&cmd); err != nil {
|
||||||
if err == models.ErrTeamNotFound {
|
if errors.Is(err, models.ErrTeamNotFound) {
|
||||||
return Error(404, "Team not found", nil)
|
return Error(404, "Team not found", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == models.ErrTeamMemberAlreadyAdded {
|
if errors.Is(err, models.ErrTeamMemberAlreadyAdded) {
|
||||||
return Error(400, "User is already added to this team", nil)
|
return Error(400, "User is already added to this team", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,7 +75,7 @@ func (hs *HTTPServer) UpdateTeamMember(c *models.ReqContext, cmd models.UpdateTe
|
|||||||
cmd.OrgId = orgId
|
cmd.OrgId = orgId
|
||||||
|
|
||||||
if err := hs.Bus.Dispatch(&cmd); err != nil {
|
if err := hs.Bus.Dispatch(&cmd); err != nil {
|
||||||
if err == models.ErrTeamMemberNotFound {
|
if errors.Is(err, models.ErrTeamMemberNotFound) {
|
||||||
return Error(404, "Team member not found.", nil)
|
return Error(404, "Team member not found.", nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to update team member.", err)
|
return Error(500, "Failed to update team member.", err)
|
||||||
@@ -97,11 +99,11 @@ func (hs *HTTPServer) RemoveTeamMember(c *models.ReqContext) Response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := hs.Bus.Dispatch(&models.RemoveTeamMemberCommand{OrgId: orgId, TeamId: teamId, UserId: userId, ProtectLastAdmin: protectLastAdmin}); err != nil {
|
if err := hs.Bus.Dispatch(&models.RemoveTeamMemberCommand{OrgId: orgId, TeamId: teamId, UserId: userId, ProtectLastAdmin: protectLastAdmin}); err != nil {
|
||||||
if err == models.ErrTeamNotFound {
|
if errors.Is(err, models.ErrTeamNotFound) {
|
||||||
return Error(404, "Team not found", nil)
|
return Error(404, "Team not found", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == models.ErrTeamMemberNotFound {
|
if errors.Is(err, models.ErrTeamMemberNotFound) {
|
||||||
return Error(404, "Team member not found", nil)
|
return Error(404, "Team member not found", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/dtos"
|
"github.com/grafana/grafana/pkg/api/dtos"
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
@@ -22,7 +24,7 @@ func getUserUserProfile(userID int64) Response {
|
|||||||
query := models.GetUserProfileQuery{UserId: userID}
|
query := models.GetUserProfileQuery{UserId: userID}
|
||||||
|
|
||||||
if err := bus.Dispatch(&query); err != nil {
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
if err == models.ErrUserNotFound {
|
if errors.Is(err, models.ErrUserNotFound) {
|
||||||
return Error(404, models.ErrUserNotFound.Error(), nil)
|
return Error(404, models.ErrUserNotFound.Error(), nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to get user", err)
|
return Error(500, "Failed to get user", err)
|
||||||
@@ -45,7 +47,7 @@ func getUserUserProfile(userID int64) Response {
|
|||||||
func GetUserByLoginOrEmail(c *models.ReqContext) Response {
|
func GetUserByLoginOrEmail(c *models.ReqContext) Response {
|
||||||
query := models.GetUserByLoginQuery{LoginOrEmail: c.Query("loginOrEmail")}
|
query := models.GetUserByLoginQuery{LoginOrEmail: c.Query("loginOrEmail")}
|
||||||
if err := bus.Dispatch(&query); err != nil {
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
if err == models.ErrUserNotFound {
|
if errors.Is(err, models.ErrUserNotFound) {
|
||||||
return Error(404, models.ErrUserNotFound.Error(), nil)
|
return Error(404, models.ErrUserNotFound.Error(), nil)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to get user", err)
|
return Error(500, "Failed to get user", err)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/dtos"
|
"github.com/grafana/grafana/pkg/api/dtos"
|
||||||
@@ -25,7 +26,7 @@ func (hs *HTTPServer) logoutUserFromAllDevicesInternal(ctx context.Context, user
|
|||||||
userQuery := models.GetUserByIdQuery{Id: userID}
|
userQuery := models.GetUserByIdQuery{Id: userID}
|
||||||
|
|
||||||
if err := bus.Dispatch(&userQuery); err != nil {
|
if err := bus.Dispatch(&userQuery); err != nil {
|
||||||
if err == models.ErrUserNotFound {
|
if errors.Is(err, models.ErrUserNotFound) {
|
||||||
return Error(404, "User not found", err)
|
return Error(404, "User not found", err)
|
||||||
}
|
}
|
||||||
return Error(500, "Could not read user from database", err)
|
return Error(500, "Could not read user from database", err)
|
||||||
@@ -45,7 +46,7 @@ func (hs *HTTPServer) getUserAuthTokensInternal(c *models.ReqContext, userID int
|
|||||||
userQuery := models.GetUserByIdQuery{Id: userID}
|
userQuery := models.GetUserByIdQuery{Id: userID}
|
||||||
|
|
||||||
if err := bus.Dispatch(&userQuery); err != nil {
|
if err := bus.Dispatch(&userQuery); err != nil {
|
||||||
if err == models.ErrUserNotFound {
|
if errors.Is(err, models.ErrUserNotFound) {
|
||||||
return Error(404, "User not found", err)
|
return Error(404, "User not found", err)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to get user", err)
|
return Error(500, "Failed to get user", err)
|
||||||
@@ -112,7 +113,7 @@ func (hs *HTTPServer) revokeUserAuthTokenInternal(c *models.ReqContext, userID i
|
|||||||
userQuery := models.GetUserByIdQuery{Id: userID}
|
userQuery := models.GetUserByIdQuery{Id: userID}
|
||||||
|
|
||||||
if err := bus.Dispatch(&userQuery); err != nil {
|
if err := bus.Dispatch(&userQuery); err != nil {
|
||||||
if err == models.ErrUserNotFound {
|
if errors.Is(err, models.ErrUserNotFound) {
|
||||||
return Error(404, "User not found", err)
|
return Error(404, "User not found", err)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to get user", err)
|
return Error(500, "Failed to get user", err)
|
||||||
@@ -120,7 +121,7 @@ func (hs *HTTPServer) revokeUserAuthTokenInternal(c *models.ReqContext, userID i
|
|||||||
|
|
||||||
token, err := hs.AuthTokenService.GetUserToken(c.Req.Context(), userID, cmd.AuthTokenId)
|
token, err := hs.AuthTokenService.GetUserToken(c.Req.Context(), userID, cmd.AuthTokenId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == models.ErrUserTokenNotFound {
|
if errors.Is(err, models.ErrUserTokenNotFound) {
|
||||||
return Error(404, "User auth token not found", err)
|
return Error(404, "User auth token not found", err)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to get user auth token", err)
|
return Error(500, "Failed to get user auth token", err)
|
||||||
@@ -132,7 +133,7 @@ func (hs *HTTPServer) revokeUserAuthTokenInternal(c *models.ReqContext, userID i
|
|||||||
|
|
||||||
err = hs.AuthTokenService.RevokeToken(c.Req.Context(), token)
|
err = hs.AuthTokenService.RevokeToken(c.Req.Context(), token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == models.ErrUserTokenNotFound {
|
if errors.Is(err, models.ErrUserTokenNotFound) {
|
||||||
return Error(404, "User auth token not found", err)
|
return Error(404, "User auth token not found", err)
|
||||||
}
|
}
|
||||||
return Error(500, "Failed to revoke user auth token", err)
|
return Error(500, "Failed to revoke user auth token", err)
|
||||||
|
|||||||
@@ -134,12 +134,11 @@ func (w *FileLogWriter) lineCounter() (int, error) {
|
|||||||
c, err := r.Read(buf)
|
c, err := r.Read(buf)
|
||||||
count += bytes.Count(buf[:c], []byte{'\n'})
|
count += bytes.Count(buf[:c], []byte{'\n'})
|
||||||
switch {
|
switch {
|
||||||
case err == io.EOF:
|
case errors.Is(err, io.EOF):
|
||||||
if err := r.Close(); err != nil {
|
if err := r.Close(); err != nil {
|
||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
return count, nil
|
return count, nil
|
||||||
|
|
||||||
case err != nil:
|
case err != nil:
|
||||||
return count, err
|
return count, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,8 @@ func AuthenticateUser(query *models.LoginUserQuery) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
err := loginUsingGrafanaDB(query)
|
err := loginUsingGrafanaDB(query)
|
||||||
if err == nil || (err != models.ErrUserNotFound && err != ErrInvalidCredentials && err != ErrUserDisabled) {
|
if err == nil || (!errors.Is(err, models.ErrUserNotFound) && !errors.Is(err, ErrInvalidCredentials) &&
|
||||||
|
!errors.Is(err, ErrUserDisabled)) {
|
||||||
query.AuthModule = "grafana"
|
query.AuthModule = "grafana"
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -47,16 +48,16 @@ func AuthenticateUser(query *models.LoginUserQuery) error {
|
|||||||
ldapEnabled, ldapErr := loginUsingLDAP(query)
|
ldapEnabled, ldapErr := loginUsingLDAP(query)
|
||||||
if ldapEnabled {
|
if ldapEnabled {
|
||||||
query.AuthModule = models.AuthModuleLDAP
|
query.AuthModule = models.AuthModuleLDAP
|
||||||
if ldapErr == nil || ldapErr != ldap.ErrInvalidCredentials {
|
if ldapErr == nil || !errors.Is(ldapErr, ldap.ErrInvalidCredentials) {
|
||||||
return ldapErr
|
return ldapErr
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != ErrUserDisabled || ldapErr != ldap.ErrInvalidCredentials {
|
if !errors.Is(err, ErrUserDisabled) || !errors.Is(ldapErr, ldap.ErrInvalidCredentials) {
|
||||||
err = ldapErr
|
err = ldapErr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == ErrInvalidCredentials || err == ldap.ErrInvalidCredentials {
|
if errors.Is(err, ErrInvalidCredentials) || errors.Is(err, ldap.ErrInvalidCredentials) {
|
||||||
if err := saveInvalidLoginAttempt(query); err != nil {
|
if err := saveInvalidLoginAttempt(query); err != nil {
|
||||||
loginLogger.Error("Failed to save invalid login attempt", "err", err)
|
loginLogger.Error("Failed to save invalid login attempt", "err", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package login
|
package login
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
@@ -38,7 +40,7 @@ var loginUsingLDAP = func(query *models.LoginUserQuery) (bool, error) {
|
|||||||
|
|
||||||
externalUser, err := newLDAP(config.Servers).Login(query)
|
externalUser, err := newLDAP(config.Servers).Login(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == ldap.ErrCouldNotFindUser {
|
if errors.Is(err, ldap.ErrCouldNotFindUser) {
|
||||||
// Ignore the error since user might not be present anyway
|
// Ignore the error since user might not be present anyway
|
||||||
if err := DisableExternalUser(query.Username); err != nil {
|
if err := DisableExternalUser(query.Username); err != nil {
|
||||||
ldapLogger.Debug("Failed to disable external user", "err", err)
|
ldapLogger.Debug("Failed to disable external user", "err", err)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package grpcplugin
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||||
@@ -143,11 +144,11 @@ func (c *clientV2) CallResource(ctx context.Context, req *backend.CallResourceRe
|
|||||||
return backendplugin.ErrMethodNotImplemented
|
return backendplugin.ErrMethodNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == io.EOF {
|
if errors.Is(err, io.EOF) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return errutil.Wrap("Failed to receive call resource response", err)
|
return errutil.Wrap("failed to receive call resource response", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := sender.Send(backend.FromProto().CallResourceResponse(protoResp)); err != nil {
|
if err := sender.Send(backend.FromProto().CallResourceResponse(protoResp)); err != nil {
|
||||||
|
|||||||
@@ -313,7 +313,7 @@ func flushStream(plugin Plugin, stream CallResourceClientResponseStream, w http.
|
|||||||
|
|
||||||
for {
|
for {
|
||||||
resp, err := stream.Recv()
|
resp, err := stream.Recv()
|
||||||
if err == io.EOF {
|
if errors.Is(err, io.EOF) {
|
||||||
if processedStreams == 0 {
|
if processedStreams == 0 {
|
||||||
return errors.New("received empty resource response")
|
return errors.New("received empty resource response")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ func (e duplicatePluginError) Error() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e duplicatePluginError) Is(err error) bool {
|
func (e duplicatePluginError) Is(err error) bool {
|
||||||
|
// nolint:errorlint
|
||||||
_, ok := err.(duplicatePluginError)
|
_, ok := err.(duplicatePluginError)
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -187,7 +187,7 @@ func (s *Server) Run() (err error) {
|
|||||||
// Mark that we are in shutdown mode
|
// Mark that we are in shutdown mode
|
||||||
// So no more services are started
|
// So no more services are started
|
||||||
s.shutdownInProgress = true
|
s.shutdownInProgress = true
|
||||||
if err != context.Canceled {
|
if !errors.Is(err, context.Canceled) {
|
||||||
// Server has crashed.
|
// Server has crashed.
|
||||||
s.log.Error("Stopped "+descriptor.Name, "reason", err)
|
s.log.Error("Stopped "+descriptor.Name, "reason", err)
|
||||||
} else {
|
} else {
|
||||||
@@ -234,7 +234,7 @@ func (s *Server) Shutdown(reason string) {
|
|||||||
func (s *Server) ExitCode(reason error) int {
|
func (s *Server) ExitCode(reason error) int {
|
||||||
code := 1
|
code := 1
|
||||||
|
|
||||||
if reason == context.Canceled && s.shutdownReason != "" {
|
if errors.Is(reason, context.Canceled) && s.shutdownReason != "" {
|
||||||
reason = fmt.Errorf(s.shutdownReason)
|
reason = fmt.Errorf(s.shutdownReason)
|
||||||
code = 0
|
code = 0
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user