Skip to content

Commit

Permalink
Return appropriate status type plugin request status handler func (#1036
Browse files Browse the repository at this point in the history
)

* check for inner data resp err

* update test

* update

* fixup

* check for cancelled

* fix err messages

* update cancelled err check
  • Loading branch information
wbrowne authored Jul 23, 2024
1 parent c770830 commit c5aa822
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion backend/adapter_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func logWrapper(next handlerWrapperFunc) handlerWrapperFunc {

ctxLogger := Logger.FromContext(ctx)
logFunc := ctxLogger.Debug
if status > RequestStatusOK {
if status > RequestStatusCancelled {
logFunc = ctxLogger.Error
}

Expand Down
30 changes: 29 additions & 1 deletion backend/data_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,58 @@ func (a *dataSDKAdapter) QueryData(ctx context.Context, req *pluginv2.QueryDataR
return RequestStatusFromError(innerErr), innerErr
}

if isCancelledError(innerErr) {
return RequestStatusCancelled, nil
}

// Set downstream status source in the context if there's at least one response with downstream status source,
// and if there's no plugin error
var hasPluginError bool
var hasDownstreamError bool
var hasCancelledError bool
for _, r := range resp.Responses {
if r.Error == nil {
continue
}

if isCancelledError(r.Error) {
hasCancelledError = true
}
if r.ErrorSource == ErrorSourceDownstream {
hasDownstreamError = true
} else {
hasPluginError = true
}
}

if hasCancelledError {
if err := WithDownstreamErrorSource(ctx); err != nil {
return RequestStatusError, fmt.Errorf("failed to set downstream status source: %w", errors.Join(innerErr, err))
}
return RequestStatusCancelled, nil
}

// A plugin error has higher priority than a downstream error,
// so set to downstream only if there's no plugin error
if hasDownstreamError && !hasPluginError {
if err := WithDownstreamErrorSource(ctx); err != nil {
return RequestStatusError, fmt.Errorf("failed to set downstream status source: %w", errors.Join(innerErr, err))
}
return RequestStatusError, nil
}

if hasPluginError {
if err := WithErrorSource(ctx, ErrorSourcePlugin); err != nil {
return RequestStatusError, fmt.Errorf("failed to set plugin status source: %w", errors.Join(innerErr, err))
}
return RequestStatusError, nil
}

if innerErr != nil {
return RequestStatusFromError(innerErr), innerErr
}

return RequestStatusFromError(innerErr), innerErr
return RequestStatusOK, nil
})
if err != nil {
return nil, err
Expand Down
6 changes: 5 additions & 1 deletion backend/request_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func RequestStatusFromError(err error) RequestStatus {
status := RequestStatusOK
if err != nil {
status = RequestStatusError
if errors.Is(err, context.Canceled) || grpcstatus.Code(err) == grpccodes.Canceled {
if isCancelledError(err) {
status = RequestStatusCancelled
}
}
Expand Down Expand Up @@ -101,3 +101,7 @@ func RequestStatusFromProtoQueryDataResponse(res *pluginv2.QueryDataResponse, er

return status
}

func isCancelledError(err error) bool {
return errors.Is(err, context.Canceled) || grpcstatus.Code(err) == grpccodes.Canceled
}

0 comments on commit c5aa822

Please sign in to comment.