Parse though error message in backend plugin

How do I in my go code send an error message back to the client browser ?
It is an important part of the experience that an addon can tell the user was is wrong and not just get “Metric request error”

image

From your QueryData method, you should be able to return something like this:

return backend.DataResponse{
    Error: errors.New("An error occurred"),
}

Hi, thank you, but i cannot seem to make this work
( go is such a weird language in some ways )

If i create a backend.DataResponse, it wont let me return it since it does not mach the type backend.QueryDataResponse
and backend.QueryDataResponse does not have an error property
But all the example code use the error return value, so i just don’t understand why doesn’t grafana just pick up the error return value from QueryData ? does it need to be created in a special way or do i need to/can i change the return type of QueryDataResponse to some type that both allows backend.QueryDataResponse and backend.DataResponse ?

Sorry! I realize I replied a little to quickly here. As you’ve noticed, you need to return a backend.QueryDataResponse rather than a backend.DataResponse.

You can see an example in one of my plugins:

So, more accurately, it should be:

	return &backend.QueryDataResponse{
		Responses: responses,
	}, nil

where responses is a map, where the key is the refID of the query, and the value is the DataResponse for that query. Hope that helps!

Thank you so much for taking your time to help.
Spot on, that helped. I should ofc return an array of responses and set the error per response. Thank you so much, you are an life saver ! :slight_smile:

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.