mirror of
https://github.com/grafana/grafana.git
synced 2024-11-26 02:40:26 -06:00
718620c197
* 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
39 lines
1.0 KiB
Go
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, "")
|
|
}
|