googleapis/mcp-toolbox · error

operation finished with error but could not marshal error ob

Error message

operation finished with error but could not marshal error object: %w

What it means

This error is thrown inside GetOperations when a long-running AlloyDB operation has finished with op.Error set, but the Go JSON marshal of that Google Rpc.Status error object itself failed. This is rare — it means the operation failed AND the error details could not be serialized (e.g., a value not JSON-marshalable was present in error details). It signals an internal serialization problem rather than the operation's own error content.

Source

Thrown at internal/sources/alloydbadmin/alloydbadmin.go:349

	}

	service, err := s.getService(ctx, accessToken)
	if err != nil {
		return nil, err
	}

	name := fmt.Sprintf("projects/%s/locations/%s/operations/%s", project, location, operation)

	op, err := service.Projects.Locations.Operations.Get(name).Do()
	if err != nil {
		logger.DebugContext(ctx, fmt.Sprintf("error getting operation: %s, retrying in %v\n", err, delay))
	} else {
		if op.Done {
			if op.Error != nil {
				var errorBytes []byte
				errorBytes, err = json.Marshal(op.Error)
				if err != nil {
					return nil, fmt.Errorf("operation finished with error but could not marshal error object: %w", err)
				}
				return nil, fmt.Errorf("operation finished with error: %s", string(errorBytes))
			}

			var opBytes []byte
			opBytes, err = op.MarshalJSON()
			if err != nil {
				return nil, fmt.Errorf("could not marshal operation: %w", err)
			}

			if op.Response != nil {
				var responseData map[string]any
				if err := json.Unmarshal(op.Response, &responseData); err == nil && responseData != nil {
					if msg, ok := generateAlloyDBConnectionMessage(responseData, connectionMessageTemplate); ok {
						return msg, nil
					}
				}
			}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Retry the GetOperations call — this is usually transient or response-corruption related
  2. Update google-api-go-client and related Google SDK packages to the latest versions
  3. Log the raw operation response from the API for debugging
  4. Fall back to reporting the operation's Code and Message fields directly instead of full JSON marshal
  5. If reproducible, capture the operation name and file an issue with the AlloyDB API team

Example fix

// before
errorBytes, err = json.Marshal(op.Error)
if err != nil {
    return nil, fmt.Errorf("operation finished with error but could not marshal error object: %w", err)
}
// after
errorBytes, err = json.Marshal(op.Error)
if err != nil {
    // fall back to the plain fields instead of failing
    return nil, fmt.Errorf("operation finished with error: code=%d message=%s (marshal failed: %v)", op.Error.Code, op.Error.Message, err)
}
return nil, fmt.Errorf("operation finished with error: %s", string(errorBytes))
Defensive patterns

Strategy: fallback

Type guard

func opErrorMarshalable(op *alloydb.Operation) bool {
    if op == nil || op.Error == nil {
        return false
    }
    _, err := json.Marshal(op.Error)
    return err == nil
}

Try / catch

op, err := s.GetOperations(ctx, project, location, opName, tmpl, delay, accessToken)
if err != nil {
    if strings.Contains(err.Error(), "could not marshal error object") {
        // fallback: refetch raw operation or report op name for manual inspection
        return nil, fmt.Errorf("operation %s failed but error details unavailable; check Cloud Logging", opName)
    }
    return err
}

Prevention

When it happens

Trigger: GetOperations detects op.Done == true and op.Error != nil, calls json.Marshal(op.Error), and the marshal returns an error (e.g., unsupported types inside the Rpc.Status details Any fields).

Common situations: Unexpected or malformed Any payload types returned by the API in operation error details, custom/changed google-api-go-client versions with types the stdlib encoder cannot handle, or corrupted responses.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/7a786f4e9559e79a. Report an issue: GitHub.