JuliusBrussee/caveman · error

json splice: invalid field insertion

Error message

json splice: invalid field insertion

What it means

AppendObjectFields validates each FieldInsertion: the name must be non-empty and the value must be valid JSON. A blank key or non-JSON value (e.g. a raw string or Go literal) causes the whole splice to fail after partial buffering.

Solutions

  1. Skip empty-named fields before building the insertion list
  2. Always produce field.Value via json.Marshal of the actual value
  3. Add a pre-call loop validating len(field.Name)>0 && json.Valid(field.Value) with a descriptive error

Example fix

// before: raw value
fields := []jsonsplice.FieldInsertion{{Name: cfg.Key, Value: []byte(cfg.Val)}}
out, err := jsonsplice.AppendObjectFields(body, span, fields...)
// after: marshal value, skip blanks
var fields []jsonsplice.FieldInsertion
for _, kv := range cfg.Fields() {
    if kv.Key == "" { continue }
    v, err := json.Marshal(kv.Value)
    if err != nil { return nil, err }
    fields = append(fields, jsonsplice.FieldInsertion{Name: kv.Key, Value: v})
}
out, err := jsonsplice.AppendObjectFields(body, span, fields...)
Defensive patterns

Strategy: validation

Validate before calling

for i, f := range fields {
    if f.Name == "" || !json.Valid(f.Value) { return fmt.Errorf("field %d invalid: name=%q value=%q", i, f.Name, f.Value) }
}

Try / catch

if err := validateFields(fields); err != nil { return nil, err }
out, err := jsonsplice.AppendObjectFields(body, span, fields...)

Prevention

When it happens

Trigger: field.Name left as "" due to a skipped/conditional config; field.Value built by string concatenation with invalid JSON; passing a bool/int as []byte without marshaling.

Common situations: Missing cache breakpoint config producing an empty field name; hand-writing values like ephemeral without quotes; upstream struct serialization disabled or failing silently.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/377dce447c33a296. Report an issue: GitHub.

Appendix: source

Thrown at proxy/providers/jsonsplice/jsonsplice.go:175

		return nil, fmt.Errorf("json splice: invalid object range")
	}
	if len(fields) == 0 {
		return body, nil
	}

	insertAt := object.End - 1
	for insertAt > object.Start+1 && bytes.ContainsRune([]byte(" \n\r\t"), rune(body[insertAt-1])) {
		insertAt--
	}
	hasFields := insertAt > object.Start+1

	var addition bytes.Buffer
	if hasFields {
		addition.WriteByte(',')
	}
	for i, field := range fields {
		if field.Name == "" || !json.Valid(field.Value) {
			return nil, fmt.Errorf("json splice: invalid field insertion")
		}
		if i > 0 {
			addition.WriteByte(',')
		}
		name, err := json.Marshal(field.Name)
		if err != nil {
			return nil, err
		}
		addition.Write(name)
		addition.WriteByte(':')
		addition.Write(field.Value)
	}

	out := make([]byte, 0, len(body)+addition.Len())
	out = append(out, body[:insertAt]...)
	out = append(out, addition.Bytes()...)
	out = append(out, body[insertAt:]...)
	return out, nil

View on GitHub (pinned to 3ee70a1026)