Skip to content

Commit

Permalink
improve logging and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
amlmtl committed Nov 13, 2023
1 parent fcf77c5 commit 0bf4748
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion internal/esp/client/espwsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
33 changes: 28 additions & 5 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -354,25 +370,32 @@ 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)
}

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
}
Expand Down
11 changes: 8 additions & 3 deletions src/components/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import {

type Props = QueryEditorProps<DataSource, EspQuery, EspDataSourceOptions>;

interface ServersResponse {
data: Server[]
}

interface State {
isServerDataFetched: boolean;
serverOptions: Array<SelectableEspObject<Server>>;
Expand Down Expand Up @@ -63,16 +67,17 @@ export class QueryEditor extends PureComponent<Props> {
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,
selectedCq: cq,
selectedWindow: window,
selectedFields: fields
});
await this.updateServers(serversResponse);
await this.updateServers(servers);
await this.setStateWithPromise({ isServerDataFetched: true });
})
.catch(console.error);
Expand Down

0 comments on commit 0bf4748

Please sign in to comment.