googleapis/mcp-toolbox · error
failed to unmarshal cluster JSON: %w
Error message
failed to unmarshal cluster JSON: %w
What it means
Thrown by GetCluster when json.Unmarshal fails to decode the marshaled cluster JSON into a map[string]any. Since the input came from protojson.Marshal, this is nearly always a sign of empty/invalid intermediate data rather than malformed cluster content.
Source
Thrown at internal/sources/dataproc/dataproc.go:244
req := &dataprocpb.GetClusterRequest{
ProjectId: s.Project,
Region: s.Region,
ClusterName: clusterName,
}
clusterPb, err := client.GetCluster(ctx, req)
if err != nil {
return nil, fmt.Errorf("failed to get cluster: %w", err)
}
jsonBytes, err := protojson.Marshal(clusterPb)
if err != nil {
return nil, fmt.Errorf("failed to marshal cluster to JSON: %w", err)
}
var result map[string]any
if err := json.Unmarshal(jsonBytes, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal cluster JSON: %w", err)
}
consoleUrl := ClusterConsoleURLFromProto(clusterPb, s.Region)
logsUrl := ClusterLogsURLFromProto(clusterPb, s.Region)
wrappedResult := map[string]any{
"consoleUrl": consoleUrl,
"logsUrl": logsUrl,
"cluster": result,
}
return wrappedResult, nil
}
// ListJobsResponse is the response from the list jobs API.
type ListJobsResponse struct {
Jobs []Job `json:"jobs"`
NextPageToken string `json:"nextPageToken"`View on GitHub (pinned to 8cc6e09de2)
Solutions
- Log/inspect jsonBytes when this fires to confirm what was produced by protojson.Marshal.
- Update google.golang.org/protobuf and the dataproc module to latest versions.
- Replace the two-step protojson->JSON->map conversion with a single protojson.Marshal into a struct if customizing the code.
Defensive patterns
Strategy: try-catch
Validate before calling
null
Type guard
if len(jsonBytes) == 0 || !json.Valid(jsonBytes) {
return errors.New("invalid JSON produced from cluster proto")
} Try / catch
cluster, err := src.GetCluster(ctx, name)
if err != nil && strings.Contains(err.Error(), "unmarshal cluster JSON") {
// internal serialization issue: capture jsonBytes, update deps, report upstream
return err
} Prevention
- Keep protobuf and dataproc module versions current and compatible.
- Don't mutate intermediate JSON between marshal and unmarshal.
- Prefer a single protojson decode step when customizing the code.
When it happens
Trigger: json.Unmarshal(jsonBytes, &result) receiving empty or non-JSON bytes — only realistically possible if the protojson.Marshal step produced invalid output or jsonBytes was mutated.
Common situations: Version-skew bugs in serialization; custom builds with modified code; effectively unreachable in normal operation given the preceding marshal succeeded.
Understand the failure class
Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.
Related errors
- failed to unmarshal job JSON: %w
- failed to marshal result: %w
- operation finished with error but could not marshal error ob
- failed to unmarshal operation bytes: %w
- failed to marshal request: %w
AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05).
Data as JSON: /api/errors/06fe6c25eade480e.
Report an issue: GitHub.