Skip to content

Commit f8e3779

Browse files
committed
Merge errorResponse into sendErrorResponseAndCleanup
1 parent 9a879e3 commit f8e3779

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

  • core/services/gateway/handlers/confidentialrelay

core/services/gateway/handlers/confidentialrelay/handler.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func (h *handler) removeExpiredRequests(ctx context.Context) {
221221
for _, er := range expiredRequests {
222222
responses := er.copiedResponses()
223223
h.lggr.Debugw("request expired without quorum", "requestID", er.req.ID, "responseCount", len(responses), "required", h.donConfig.F+1)
224-
err := h.sendResponseAndCleanup(ctx, er, h.errorResponse(er.req, api.RequestTimeoutError, fmt.Errorf("request expired: got %d/%d responses", len(responses), h.donConfig.F+1), nil))
224+
err := h.sendErrorResponseAndCleanup(ctx, er, api.RequestTimeoutError, fmt.Errorf("request expired: got %d/%d responses", len(responses), h.donConfig.F+1), nil)
225225
if err != nil {
226226
h.lggr.Errorw("error sending response to user", "requestID", er.req.ID, "error", err)
227227
}
@@ -307,10 +307,10 @@ func (h *handler) HandleNodeMessage(ctx context.Context, resp *jsonrpc.Response[
307307
return nil
308308
case errors.Is(err, errQuorumUnobtainable):
309309
l.Errorw("quorum unobtainable, returning error to user", "error", err)
310-
return h.sendResponseAndCleanup(ctx, ar, h.errorResponse(ar.req, api.FatalError, err, nil))
310+
return h.sendErrorResponseAndCleanup(ctx, ar, api.FatalError, err, nil)
311311
case err != nil:
312312
l.Errorw("unexpected aggregation error", "error", err)
313-
return h.sendResponseAndCleanup(ctx, ar, h.errorResponse(ar.req, api.FatalError, err, nil))
313+
return h.sendErrorResponseAndCleanup(ctx, ar, api.FatalError, err, nil)
314314
}
315315

316316
return h.sendSuccessResponseAndCleanup(ctx, l, ar, aggregatedResp)
@@ -327,7 +327,7 @@ func (h *handler) fanOutToNodes(ctx context.Context, l logger.Logger, ar *active
327327
}
328328

329329
if len(nodeErrors) == len(h.donConfig.Members) && len(nodeErrors) > 0 {
330-
return h.sendResponseAndCleanup(ctx, ar, h.errorResponse(ar.req, api.FatalError, errors.New("failed to forward user request to nodes"), nil))
330+
return h.sendErrorResponseAndCleanup(ctx, ar, api.FatalError, errors.New("failed to forward user request to nodes"), nil)
331331
}
332332

333333
l.Debugw("successfully forwarded request to relay nodes")
@@ -338,7 +338,7 @@ func (h *handler) sendSuccessResponseAndCleanup(ctx context.Context, l logger.Lo
338338
rawResponse, err := jsonrpc.EncodeResponse(resp)
339339
if err != nil {
340340
l.Errorw("failed to encode response", "error", err)
341-
return h.sendResponseAndCleanup(ctx, ar, h.errorResponse(ar.req, api.NodeReponseEncodingError, fmt.Errorf("failed to marshal response: %w", err), nil))
341+
return h.sendErrorResponseAndCleanup(ctx, ar, api.NodeReponseEncodingError, fmt.Errorf("failed to marshal response: %w", err), nil)
342342
}
343343

344344
var errorCode api.ErrorCode
@@ -356,12 +356,10 @@ func (h *handler) sendSuccessResponseAndCleanup(ctx context.Context, l logger.Lo
356356
return h.sendResponseAndCleanup(ctx, ar, successResp)
357357
}
358358

359-
func (h *handler) errorResponse(
360-
req jsonrpc.Request[json.RawMessage],
361-
errorCode api.ErrorCode,
362-
err error,
363-
data []byte,
364-
) gwhandlers.UserCallbackPayload {
359+
// sendErrorResponseAndCleanup builds a sanitized error payload, records
360+
// metrics, sends the response, and removes the request from activeRequests.
361+
func (h *handler) sendErrorResponseAndCleanup(ctx context.Context, ar *activeRequest, errorCode api.ErrorCode, err error, data []byte) error {
362+
req := ar.req
365363
switch errorCode {
366364
case api.FatalError:
367365
case api.NodeReponseEncodingError:
@@ -385,7 +383,7 @@ func (h *handler) errorResponse(
385383
case api.LimitExceededError:
386384
}
387385

388-
return gwhandlers.UserCallbackPayload{
386+
resp := gwhandlers.UserCallbackPayload{
389387
RawResponse: h.codec.EncodeNewErrorResponse(
390388
req.ID,
391389
api.ToJSONRPCErrorCode(errorCode),
@@ -394,6 +392,7 @@ func (h *handler) errorResponse(
394392
),
395393
ErrorCode: errorCode,
396394
}
395+
return h.sendResponseAndCleanup(ctx, ar, resp)
397396
}
398397

399398
// sendResponseAndCleanup sends the response to the user and removes the

0 commit comments

Comments
 (0)