grafana/pkg/services/ngalert/api/errors.go
Yuriy Tseretyan 718620c197
Alerting: Update forking request handlers to use the same errors (#52965)
* generalize error handling in forking request handlers
* remove MatchesBackend and change test to test Can
* add 404 to route specs
* change backendTypeByUID to getDatasourceByUID of expected type
* use common errors in api testing
* handle 401 in errorToResponse
* replace backend type error with "unexpected datasource type"
* update swagger spec
2022-08-02 09:33:59 -04:00

39 lines
1.0 KiB
Go

package api
import (
"errors"
"fmt"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/services/datasources"
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
)
var (
errUnexpectedDatasourceType = errors.New("unexpected datasource type")
)
func unexpectedDatasourceTypeError(actual string, expected string) error {
return fmt.Errorf("%w '%s', expected %s", errUnexpectedDatasourceType, actual, expected)
}
func backendTypeDoesNotMatchPayloadTypeError(backendType apimodels.Backend, payloadType string) error {
return fmt.Errorf("unexpected backend type (%s) for payload type (%s)",
backendType.String(),
payloadType,
)
}
func errorToResponse(err error) response.Response {
if errors.Is(err, datasources.ErrDataSourceNotFound) {
return ErrResp(404, err, "")
}
if errors.Is(err, errUnexpectedDatasourceType) {
return ErrResp(400, err, "")
}
if errors.Is(err, ErrAuthorization) {
return ErrResp(401, err, "")
}
return ErrResp(500, err, "")
}