diff --git a/internal/esp/client/espwsclient.go b/internal/esp/client/espwsclient.go index 2546977..5cd15c9 100644 --- a/internal/esp/client/espwsclient.go +++ b/internal/esp/client/espwsclient.go @@ -204,7 +204,7 @@ func (espWsClient *EspWsClient) handleBulkMessage(encodedMessages *[]string) { var message messagedto.MessageDTO err = json.Unmarshal(*decodedMessage, &message) if err != nil { - log.DefaultLogger.Error(fmt.Sprintf("Cannot unmarshal message: %v", message)) + log.DefaultLogger.Error(fmt.Sprintf("Cannot unmarshal message: %s", *decodedMessage)) return } diff --git a/pkg/plugin/plugin.go b/pkg/plugin/plugin.go index 60cb170..c6ddbba 100644 --- a/pkg/plugin/plugin.go +++ b/pkg/plugin/plugin.go @@ -337,6 +337,22 @@ func (d *SampleDatasource) PublishStream(_ context.Context, _ *backend.PublishSt }, nil } +type callResourceResponseBody struct { + Error *string `json:"error,omitempty"` + Data json.RawMessage `json:"data,omitempty"` +} + +func newSerializedCallResourceResponseErrorBody(errorMessage string) []byte { + errorResponseBody, err := json.Marshal(callResourceResponseBody{ + Error: &errorMessage, + }) + if err != nil { + errorResponseBody = []byte(`{error:"Internal server error"}`) + } + + return errorResponseBody +} + func (d *SampleDatasource) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error { var response = &backend.CallResourceResponse{ Status: http.StatusNotFound, @@ -354,16 +370,25 @@ func (d *SampleDatasource) CallResource(ctx context.Context, req *backend.CallRe discoveryResponse, err = callDiscoveryEndpoint(ctx, d.httpClient, *discoveryServiceUrl) if err != nil { errorMessage := "Unable to obtain ESP server schema information." - response.Body = []byte(errorMessage) + response.Body = newSerializedCallResourceResponseErrorBody(errorMessage) response.Status = http.StatusInternalServerError log.DefaultLogger.Error(errorMessage, "error", err) return sender.Send(response) } - responseBody, err := io.ReadAll(discoveryResponse.Body) + servers, err := io.ReadAll(discoveryResponse.Body) if err != nil { errorMessage := "Unable to read discovery response." - response.Body = []byte(errorMessage) + response.Body = newSerializedCallResourceResponseErrorBody(errorMessage) + response.Status = http.StatusInternalServerError + log.DefaultLogger.Error(errorMessage, "error", err) + return sender.Send(response) + } + + responseBody, err := json.Marshal(callResourceResponseBody{Data: servers}) + if err != nil { + errorMessage := "Unable to serialize discovery response." + response.Body = newSerializedCallResourceResponseErrorBody(errorMessage) response.Status = http.StatusInternalServerError log.DefaultLogger.Error(errorMessage, "error", err) return sender.Send(response) @@ -371,8 +396,6 @@ func (d *SampleDatasource) CallResource(ctx context.Context, req *backend.CallRe response.Status = http.StatusOK response.Body = responseBody - response.Headers["Content-Encoding"] = []string{discoveryResponse.Header.Get("Content-Encoding")} - response.Headers["Content-Type"] = []string{discoveryResponse.Header.Get("Content-Type")} default: break } diff --git a/src/components/QueryEditor.tsx b/src/components/QueryEditor.tsx index 8b10324..64137af 100644 --- a/src/components/QueryEditor.tsx +++ b/src/components/QueryEditor.tsx @@ -22,6 +22,10 @@ import { type Props = QueryEditorProps; +interface ServersResponse { + data: Server[] +} + interface State { isServerDataFetched: boolean; serverOptions: Array>; @@ -63,8 +67,9 @@ export class QueryEditor extends PureComponent { componentDidMount() { this.props.datasource .getResource('servers') - .then(async (serversResponse: Server[]) => { - const [server, project, cq, window, fields] = QueryEditor.deriveSelectionsFromQuery(serversResponse, this.props.query); + .then(async (serversResponse: ServersResponse) => { + const servers = serversResponse.data + const [server, project, cq, window, fields] = QueryEditor.deriveSelectionsFromQuery(servers, this.props.query); await this.setStateWithPromise({ selectedServer: server, selectedProject: project, @@ -72,7 +77,7 @@ export class QueryEditor extends PureComponent { selectedWindow: window, selectedFields: fields }); - await this.updateServers(serversResponse); + await this.updateServers(servers); await this.setStateWithPromise({ isServerDataFetched: true }); }) .catch(console.error);