|
| 1 | +package backend |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | +) |
| 8 | + |
| 9 | +// NewErrorSourceMiddleware returns a new backend.HandlerMiddleware that sets the error source in the |
| 10 | +// context.Context, based on returned errors or query data response errors. |
| 11 | +// If at least one query data response has a "downstream" error source and there isn't one with a "plugin" error source, |
| 12 | +// the error source in the context is set to "downstream". |
| 13 | +func NewErrorSourceMiddleware() HandlerMiddleware { |
| 14 | + return HandlerMiddlewareFunc(func(next Handler) Handler { |
| 15 | + return &ErrorSourceMiddleware{ |
| 16 | + BaseHandler: NewBaseHandler(next), |
| 17 | + } |
| 18 | + }) |
| 19 | +} |
| 20 | + |
| 21 | +type ErrorSourceMiddleware struct { |
| 22 | + BaseHandler |
| 23 | +} |
| 24 | + |
| 25 | +func (m *ErrorSourceMiddleware) handleDownstreamError(ctx context.Context, err error) error { |
| 26 | + if err == nil { |
| 27 | + return nil |
| 28 | + } |
| 29 | + |
| 30 | + if IsDownstreamError(err) { |
| 31 | + if innerErr := WithDownstreamErrorSource(ctx); innerErr != nil { |
| 32 | + return fmt.Errorf("failed to set downstream error source: %w", errors.Join(innerErr, err)) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return err |
| 37 | +} |
| 38 | + |
| 39 | +func (m *ErrorSourceMiddleware) QueryData(ctx context.Context, req *QueryDataRequest) (*QueryDataResponse, error) { |
| 40 | + resp, err := m.BaseHandler.QueryData(ctx, req) |
| 41 | + // we want to always process the error here first before checking anything else |
| 42 | + // because we want the opportunity to set the error source in the context. |
| 43 | + err = m.handleDownstreamError(ctx, err) |
| 44 | + |
| 45 | + // no point in continue if we have an error or no response to process, so we return early. |
| 46 | + if err != nil || resp == nil || len(resp.Responses) == 0 { |
| 47 | + return resp, err |
| 48 | + } |
| 49 | + |
| 50 | + // Set downstream error source in the context if there's at least one response with downstream error source, |
| 51 | + // and if there's no plugin error |
| 52 | + var hasPluginError bool |
| 53 | + var hasDownstreamError bool |
| 54 | + for refID, r := range resp.Responses { |
| 55 | + if r.Error == nil { |
| 56 | + continue |
| 57 | + } |
| 58 | + |
| 59 | + if !r.ErrorSource.IsValid() { |
| 60 | + // if the error is a downstream error, set error source to downstream, otherwise plugin. |
| 61 | + if IsDownstreamError(r.Error) { |
| 62 | + r.ErrorSource = ErrorSourceDownstream |
| 63 | + } else { |
| 64 | + r.ErrorSource = ErrorSourcePlugin |
| 65 | + } |
| 66 | + resp.Responses[refID] = r |
| 67 | + } |
| 68 | + |
| 69 | + if !r.Status.IsValid() { |
| 70 | + r.Status = statusFromError(r.Error) |
| 71 | + resp.Responses[refID] = r |
| 72 | + } |
| 73 | + |
| 74 | + if r.ErrorSource == ErrorSourceDownstream { |
| 75 | + hasDownstreamError = true |
| 76 | + } else { |
| 77 | + hasPluginError = true |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // A plugin error has higher priority than a downstream error, |
| 82 | + // so set to downstream only if there's no plugin error |
| 83 | + if hasDownstreamError && !hasPluginError { |
| 84 | + if err := WithDownstreamErrorSource(ctx); err != nil { |
| 85 | + return resp, fmt.Errorf("failed to set downstream status source: %w", err) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return resp, err |
| 90 | +} |
| 91 | + |
| 92 | +func (m *ErrorSourceMiddleware) CallResource(ctx context.Context, req *CallResourceRequest, sender CallResourceResponseSender) error { |
| 93 | + err := m.BaseHandler.CallResource(ctx, req, sender) |
| 94 | + return m.handleDownstreamError(ctx, err) |
| 95 | +} |
| 96 | + |
| 97 | +func (m *ErrorSourceMiddleware) CheckHealth(ctx context.Context, req *CheckHealthRequest) (*CheckHealthResult, error) { |
| 98 | + resp, err := m.BaseHandler.CheckHealth(ctx, req) |
| 99 | + return resp, m.handleDownstreamError(ctx, err) |
| 100 | +} |
| 101 | + |
| 102 | +func (m *ErrorSourceMiddleware) CollectMetrics(ctx context.Context, req *CollectMetricsRequest) (*CollectMetricsResult, error) { |
| 103 | + resp, err := m.BaseHandler.CollectMetrics(ctx, req) |
| 104 | + return resp, m.handleDownstreamError(ctx, err) |
| 105 | +} |
| 106 | + |
| 107 | +func (m *ErrorSourceMiddleware) SubscribeStream(ctx context.Context, req *SubscribeStreamRequest) (*SubscribeStreamResponse, error) { |
| 108 | + resp, err := m.BaseHandler.SubscribeStream(ctx, req) |
| 109 | + return resp, m.handleDownstreamError(ctx, err) |
| 110 | +} |
| 111 | + |
| 112 | +func (m *ErrorSourceMiddleware) PublishStream(ctx context.Context, req *PublishStreamRequest) (*PublishStreamResponse, error) { |
| 113 | + resp, err := m.BaseHandler.PublishStream(ctx, req) |
| 114 | + return resp, m.handleDownstreamError(ctx, err) |
| 115 | +} |
| 116 | + |
| 117 | +func (m *ErrorSourceMiddleware) RunStream(ctx context.Context, req *RunStreamRequest, sender *StreamSender) error { |
| 118 | + err := m.BaseHandler.RunStream(ctx, req, sender) |
| 119 | + return m.handleDownstreamError(ctx, err) |
| 120 | +} |
| 121 | + |
| 122 | +func (m *ErrorSourceMiddleware) ValidateAdmission(ctx context.Context, req *AdmissionRequest) (*ValidationResponse, error) { |
| 123 | + resp, err := m.BaseHandler.ValidateAdmission(ctx, req) |
| 124 | + return resp, m.handleDownstreamError(ctx, err) |
| 125 | +} |
| 126 | + |
| 127 | +func (m *ErrorSourceMiddleware) MutateAdmission(ctx context.Context, req *AdmissionRequest) (*MutationResponse, error) { |
| 128 | + resp, err := m.BaseHandler.MutateAdmission(ctx, req) |
| 129 | + return resp, m.handleDownstreamError(ctx, err) |
| 130 | +} |
| 131 | + |
| 132 | +func (m *ErrorSourceMiddleware) ConvertObjects(ctx context.Context, req *ConversionRequest) (*ConversionResponse, error) { |
| 133 | + resp, err := m.BaseHandler.ConvertObjects(ctx, req) |
| 134 | + return resp, m.handleDownstreamError(ctx, err) |
| 135 | +} |
0 commit comments