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
- Retry the GetOperations call — this is usually transient or response-corruption related
- Update google-api-go-client and related Google SDK packages to the latest versions
- Log the raw operation response from the API for debugging
- Fall back to reporting the operation's Code and Message fields directly instead of full JSON marshal
- 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
- Keep google-api-go-client versions current so Rpc.Status marshals correctly
- Log raw API responses when operations fail to aid debugging
- Treat this as rare/transient and retry the operation fetch once
- Have a fallback path that surfaces operation name instead of full details
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
- operation finished with error: %s
- could not marshal operation: %w
- failed to unmarshal operation bytes: %w
- failed to unmarshal job JSON: %w
- failed to marshal result: %w
AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05).
Data as JSON: /api/errors/7a786f4e9559e79a.
Report an issue: GitHub.