grafana/pkg/services/ngalert/api/errors.go
Yuri Tseretyan 027bd9356f
Alerting: Rule Modify Export APIs (#75322)
* extend RuleStore interface to get namespace by UID
* add new export API endpoints
* implement request handlers
* update authorization and wire handlers to paths
* add folder error matchers to errorToResponse
* add tests for export methods
2023-10-02 11:47:59 -04:00

45 lines
1.3 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")
// errFolderAccess is used as a wrapper to propagate folder related errors and correctly map to the response status
errFolderAccess = errors.New("cannot get folder")
)
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, "")
}
if errors.Is(err, errFolderAccess) {
return toNamespaceErrorResponse(err)
}
return ErrResp(500, err, "")
}