ExportXMLWordPrintableJSON

    • Type: Sub-task
    • Resolution: Unresolved
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Go Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Context

      GODRIVER-4172 makes mongo.WriteException a wrapper: processWriteError now wraps the
      error chain it was handed instead of replacing it. However, InsertMany, BulkWrite and ClientBulkWrite build their own error values, either never calling processWriteError, or calling it and then copying fields out of the returned WriteException into a fresh exception value, discarding the chain.

      Note that bulkWrite.execute and clientBulkWrite.execute are internally inconsistent: both return the original err (chain intact) when a command error occurred, but a hand-built exception (chain gone) when only write errors occurred. Users cannot rely on either shape.

      We need to fix those three methods so they also preserve the error chain.

      Definition of done

      • Add Unwrap() error to mongo.BulkWriteException and mongo.ClientBulkWriteException, following the WriteException precedent: an unexported wrapped field plus an unexported wrappedMsgOnly flag so the error message is not duplicated across the chain.
      • Update each site above to wrap rather than replace:
        • InsertMany wraps the error returned by processWriteError.
        • bulkWrite.execute wraps the per-batch errors. Decide and document whether a multi-batch unordered failure joins them (Unwrap() []error) or keeps only one (Unwrap() error); pick one signature per type and apply it consistently.
        • clientBulkWrite.execute wraps err rather than only reading CommandError out of it.
        • modelBatches.processResponse wraps info.Error.
      • Replace the 18 remaining direct type assertions for the bulk exception types with errors.As, using assert.ErrorAs / require.ErrorAs.
      • Re-audit the type-dispatch order in extractErrorDetails (internal/integration/unified/error.go:190) and its near-duplicate in internal/integration/json_helpers_test.go:478 — see Pitfalls.
      • Unit tests asserting, for each affected method, that errors.Unwrap returns the operation's error, that errors.As finds driver.WriteCommandError, and that a sentinel joined into the chain is reachable with errors.Is.

      Pitfalls

      • Dispatch order in the unified test runner is the main hazard. extractErrorDetails tries CommandError, then WriteException, then BulkWriteException, then ClientBulkWriteException, and returns on the first errors.As hit. Wrapping a WriteException inside InsertMany's BulkWriteException makes the WriteException
        case match first, so details.raw becomes the whole server response instead of the per-write-error Raw the BulkWriteException case uses — silently changing errorResponse, errorCode, errorCodeName and errorLabelsContain assertions across the CRUD, retryable-writes and CSOT suites. The same applies to clientBulkWrite.execute: wrapping err puts a CommandError in the chain, which matches the first case. Fix the order (most specific type first) in the same PR, or avoid putting a competing error type in the chain — and add a test pinning whichever invariant you choose. GODRIVER-4172 avoided exactly this by wrapping the raw err instead of wrapErrors(err); see the comment at mongo/errors.go:971.

            Assignee:
            Unassigned
            Reporter:
            Matt Dale
            None
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: