{"record":{"id":"67f4e79f8e777ab0","repo":"grafana/k6","slug":"parsing-file-descriptor-w","errorCode":null,"errorMessage":"parsing file descriptor: %w","messagePattern":"parsing file descriptor: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/element_handle_options.go","lineNumber":233,"sourceCode":"// NewElementHandleSetInputFilesOptions creates a new ElementHandleSetInputFilesOption.\nfunc NewElementHandleSetInputFilesOptions(defaultTimeout time.Duration) *ElementHandleSetInputFilesOptions {\n\treturn &ElementHandleSetInputFilesOptions{\n\t\tElementHandleBaseOptions: *NewElementHandleBaseOptions(defaultTimeout),\n\t}\n}\n\n// addFile to the struct. Input value can only be a file descriptor object.\nfunc (f *Files) addFile(ctx context.Context, file sobek.Value) error {\n\tif common.IsNullish(file) {\n\t\treturn nil\n\t}\n\trt := k6ext.Runtime(ctx)\n\tfileType := file.ExportType()\n\tswitch fileType.Kind() {\n\tcase reflect.Map: // file descriptor object\n\t\tvar parsedFile File\n\t\tif err := rt.ExportTo(file, &parsedFile); err != nil {\n\t\t\treturn fmt.Errorf(\"parsing file descriptor: %w\", err)\n\t\t}\n\t\tf.Payload = append(f.Payload, &parsedFile)\n\tdefault:\n\t\treturn fmt.Errorf(\"invalid parameter type : %s\", fileType.Kind().String())\n\t}\n\n\treturn nil\n}\n\n// Parse parses the Files struct from the given sobek.Value.\nfunc (f *Files) Parse(ctx context.Context, files sobek.Value) error {\n\trt := k6ext.Runtime(ctx)\n\tif common.IsNullish(files) {\n\t\treturn nil\n\t}\n\n\toptsType := files.ExportType()\n\tswitch optsType.Kind() {","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/element_handle_options.go#L215-L251","documentation":"Thrown while parsing the files argument of ElementHandle.setInputFiles. k6 exports each JavaScript file descriptor object into the Go File struct via rt.ExportTo, and the export fails because the object's shape does not match. The expected descriptor is { name: string, mimeType: string, buffer: string } (File struct in element_handle_options.go:92). Fields with mismatched types (e.g. a buffer that is not a string) make the export fail.","triggerScenarios":"setInputFiles([{ name: 'a.txt', mimeType: 'text/plain', buffer: 123 }]) with a non-string buffer; descriptor objects containing nested objects, arrays, or numbers where strings are expected; one malformed element inside an otherwise valid files array.","commonSituations":"Porting Playwright scripts: Playwright accepts { name, mimeType, buffer: Buffer } while this k6 browser version expects buffer as a string; building descriptors dynamically without validating field types; passing binary data unwrapped.","solutions":["Make each descriptor exactly { name: string, mimeType: string, buffer: string }","Encode binary payloads into a string (e.g. base64 via k6/encoding) before putting them in buffer","Log each descriptor before calling setInputFiles to find the malformed one","For arrays, verify every element is a plain object with string-valued fields"],"exampleFix":"// before\nawait el.setInputFiles([{ name: 'f.bin', mimeType: 'application/octet-stream', buffer: 12345 }]);\n\n// after\nimport encoding from 'k6/encoding';\nconst b64 = encoding.b64encode('\\x00\\x01\\x02', 'std');\nawait el.setInputFiles([{ name: 'f.bin', mimeType: 'application/octet-stream', buffer: b64 }]);","handlingStrategy":"validation","validationCode":"function isFileDescriptor(o) {\n  return !!o && typeof o === 'object' && !Array.isArray(o) &&\n    typeof o.name === 'string' && typeof o.mimeType === 'string' &&\n    (o.buffer === undefined || typeof o.buffer === 'string');\n}\nconst files = [{ name: 'a.txt', mimeType: 'text/plain', buffer: 'hello' }];\nif (!files.every(isFileDescriptor)) throw new Error('malformed file descriptor');\nawait el.setInputFiles(files);","typeGuard":"function isFileDescriptor(o) {\n  return !!o && typeof o === 'object' && !Array.isArray(o) &&\n    typeof o.name === 'string' && typeof o.mimeType === 'string' &&\n    (o.buffer === undefined || typeof o.buffer === 'string');\n}","tryCatchPattern":"try {\n  await el.setInputFiles(files);\n} catch (e) {\n  if (/parsing file descriptor/.test(e.message)) {\n    // normalize every descriptor to {name, mimeType, buffer} strings and retry\n  } else throw e;\n}","preventionTips":["Build descriptors from one helper so the shape stays consistent","Encode binary payloads as strings before passing them","Remember k6 wants {name, mimeType, buffer}, not Playwright Buffer objects","Validate array elements before the call, not after the failure"],"tags":["browser","file-upload","set-input-files","validation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}