Skip to content

plugins/destination/mongodb/client/write.go: panic(err) instead of error returns when JSON unmarshal fails in transformArr #23172

Description

@praneshnikhar

MongoDB: panic(err) in production code path in write.go

File: plugins/destination/mongodb/client/write.go

Problem: panic(err) is used instead of returning errors when json.Unmarshal fails in transformArr. In a production system, this means any malformed JSON data in a record column will crash the entire process instead of being handled gracefully (e.g., skipping the row, logging an error, or returning the error up the call chain to be retried).

Current code (multi-location):

case *types.JSONArray:
    var val any
    if err := json.Unmarshal([]byte(a.ValueStr(i)), &val); err != nil {
        panic(err)  // crashes the process
    }
case *array.Struct:
    var val any
    if err := json.Unmarshal([]byte(a.ValueStr(i)), &val); err != nil {
        panic(err)  // crashes the process
    }

Additionally, transformArr, transformRecord, and transformRecords all return []any without an error return — so even if the panics were replaced, there was no way to propagate errors. The function signatures needed to be changed to ([]any, error).

Fix: Replace panic(err) with return nil, err and update all callers to propagate the error.

Reproduction

  1. Configure a MongoDB destination plugin.
  2. Set up a source that produces records with an invalid JSON value in a column typed as JSONArray or Struct (e.g., a malformed JSON string that json.Unmarshal can't parse).
  3. Run cloudquery sync — the process crashes with a panic instead of reporting an error and continuing.

Example of a record that triggers this:

Column type: JSONArray
Value: "{invalid json without proper structure"

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions