gofr-dev/gofr · error

data length exceeds array capacity

Error message

data length exceeds array capacity

What it means

errDataLengthExceeded is returned by setSliceOrArrayValue when the number of multipart values being bound exceeds the capacity of a fixed-size array field in the target struct. Arrays in Go have fixed capacity, so extra values cannot fit.

Source

Thrown at pkg/gofr/http/multipart_file_bind.go:15

package http

import (
	"errors"
	"io"
	"mime/multipart"
	"reflect"
	"strconv"

	"gofr.dev/pkg/gofr/file"
)

var (
	errUnsupportedInterfaceType = errors.New("unsupported interface value type")
	errDataLengthExceeded       = errors.New("data length exceeds array capacity")
	errUnsupportedKind          = errors.New("unsupported kind")
	errSettingValueFailure      = errors.New("error setting value at index")
	errNotAStruct               = errors.New("provided value is not a struct")
	errUnexportedField          = errors.New("cannot set field; it might be unexported")
	errUnsupportedFieldType     = errors.New("unsupported type for field")
	errFieldsNotSet             = errors.New("no fields were set")
)

type formData struct {
	fields map[string][]string
	files  map[string][]*multipart.FileHeader
}

func (uf *formData) mapStruct(val reflect.Value, field *reflect.StructField) (bool, error) {
	vKind := val.Kind()

	if field == nil {
		// Check if val is not a struct

View on GitHub (pinned to 187eb24962)

Solutions

  1. Change the field to a slice ([]string) instead of a fixed-size array to accept any number of values
  2. Increase the array size to accommodate the maximum expected number of values
  3. Enforce the limit client-side or validate the incoming count before binding

Example fix

// before
type Upload struct { Files [3]string }
// after
type Upload struct { Files []string }
Defensive patterns

Strategy: validation

Validate before calling

if len(r.MultipartForm.Value[field]) > arrayCap { return fmt.Errorf("field %s accepts at most %d values", field, arrayCap) }

Type guard

func fitsArray(vals []string, cap int) bool { return len(vals) <= cap }

Try / catch

if err := bind.Struct(&upload); err != nil { http.Error(w, "too many values for field", http.StatusBadRequest); return }

Prevention

When it happens

Trigger: Binding multiple multipart form/file entries into a field declared as a fixed-size array (e.g. [3]string) while the request carries more than 3 values.

Common situations: Client sends more form fields/files than the server's array-based DTO allows; contract drift between client and server where the server shrank the array size.

Related errors


AI-assisted analysis of gofr-dev/gofr@187eb24962 (2026-09-01). Data as JSON: /api/errors/f33f4d3d22242041. Report an issue: GitHub.