Skip to content

Error handling #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions tools/datasources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tools

import (
"context"
"errors"
"fmt"
"strings"

Expand All @@ -12,6 +13,8 @@ import (
mcpgrafana "github.com/grafana/mcp-grafana"
)

var ErrDatasourceNotFound = errors.New("datasource not found")

type ListDatasourcesParams struct {
Type string `json:"type,omitempty" jsonschema:"description=The type of datasources to search for. For example\\, 'prometheus'\\, 'loki'\\, 'tempo'\\, etc..."`
}
Expand Down Expand Up @@ -81,9 +84,8 @@ func getDatasourceByUID(ctx context.Context, args GetDatasourceByUIDParams) (*mo
c := mcpgrafana.GrafanaClientFromContext(ctx)
datasource, err := c.Datasources.GetDataSourceByUID(args.UID)
if err != nil {
// Check if it's a 404 Not Found Error
if strings.Contains(err.Error(), "404") {
return nil, fmt.Errorf("datasource with UID '%s' not found. Please check if the datasource exists and is accessible", args.UID)
return &models.DataSource{}, fmt.Errorf("%w: datasource UID '%s' does not exist or you don't have access. Use list_datasources to see available datasources", ErrDatasourceNotFound, args.UID)
}
return nil, fmt.Errorf("get datasource by uid %s: %w", args.UID, err)
}
Expand All @@ -107,6 +109,9 @@ func getDatasourceByName(ctx context.Context, args GetDatasourceByNameParams) (*
c := mcpgrafana.GrafanaClientFromContext(ctx)
datasource, err := c.Datasources.GetDataSourceByName(args.Name)
if err != nil {
if strings.Contains(err.Error(), "404") {
return &models.DataSource{}, fmt.Errorf("%w: datasource with name '%s' not found; check if the datasource exists in Grafana and you have permission to access it", ErrDatasourceNotFound, args.Name)
}
return nil, fmt.Errorf("get datasource by name %s: %w", args.Name, err)
}
return datasource.Payload, nil
Expand Down
10 changes: 10 additions & 0 deletions tools/datasources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,14 @@ func TestDatasourcesTools(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "Prometheus", result.Name)
})

t.Run("get datasource by name - not found", func(t *testing.T) {
ctx := newTestContext()
result, err := getDatasourceByName(ctx, GetDatasourceByNameParams{
Name: "non-existent-datasource",
})
require.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, "", result.Name)
})
}
Loading