{"id":"dcebbadd3f8bcf40","repo":"socketio/socket.io","slug":"invalid-format","errorCode":null,"errorMessage":"invalid format","messagePattern":"invalid format","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"examples/basic-websocket-client/src/index.js","lineNumber":244,"sourceCode":"    output += JSON.stringify(packet.data);\n  }\n\n  return output;\n}\n\nfunction decode(data) {\n  let i = 1; // skip \"4\" prefix\n\n  const packet = {\n    type: parseInt(data.charAt(i++), 10),\n  };\n\n  if (data.charAt(i)) {\n    packet.data = JSON.parse(data.substring(i));\n  }\n\n  if (!isPacketValid(packet)) {\n    throw new Error(\"invalid format\");\n  }\n\n  return packet;\n}\n\nfunction isPacketValid(packet) {\n  switch (packet.type) {\n    case SIOPacketType.CONNECT:\n      return typeof packet.data === \"object\";\n    case SIOPacketType.DISCONNECT:\n      return packet.data === undefined;\n    case SIOPacketType.EVENT: {\n      const args = packet.data;\n      return (\n        Array.isArray(args) && args.length > 0 && typeof args[0] === \"string\"\n      );\n    }\n    default:","sourceCodeStart":226,"sourceCodeEnd":262,"githubUrl":"https://github.com/socketio/socket.io/blob/ae7fb46e08c5ed964b4a1ea8b1703e816511598e/examples/basic-websocket-client/src/index.js#L226-L262","documentation":"Thrown by the decode() helper in the basic-websocket-client example when an incoming Socket.IO packet fails validation in isPacketValid(). The decoder parses the type byte and JSON data, then checks the packet shape against the expected per-type contract (CONNECT must carry an object, DISCONNECT must have no data, EVENT must be a non-empty array with a string first element). A failure means the frame did not match any valid Socket.IO packet structure.","triggerScenarios":"Calling decode() on a raw string that is malformed: a CONNECT (type 0) whose JSON body is not an object, a DISCONNECT (type 1) that carries trailing data, or an EVENT (type 2) whose payload is missing or whose first array element is not a string. Any type byte outside 0/1/2 also fails because isPacketValid returns false in the default branch.","commonSituations":"Manually decoding frames with a custom/naive parser in the example client, or pointing the example client at a server/endpoint that sends a non-standard or Engine.IO-only frame (e.g. an ERROR packet type 4 which this minimal client does not model).","solutions":["Inspect the raw frame being passed to decode() and confirm its type byte is one of 0 (CONNECT), 1 (DISCONNECT), or 2 (EVENT).","If the server legitimately sends other packet types (ACK=3, ERROR=4, BINARY_EVENT=5), extend isPacketValid/SIOPacketType to model them.","Wrap the decode() call in try/catch and skip/log frames that are not understood rather than crashing the client."],"exampleFix":"// before\nconst packet = decode(raw);\nhandle(packet);\n\n// after\nlet packet;\ntry {\n  packet = decode(raw);\n} catch (e) {\n  console.warn('dropping malformed frame', raw, e.message);\n  return;\n}\nhandle(packet);","handlingStrategy":"try-catch","validationCode":"function safeType(t){ return [0,1,2].includes(t); }\nfunction looksValid(raw){\n  const t = parseInt(raw.charAt(1),10);\n  if(!safeType(t)) return false;\n  // rough shape checks matching isPacketValid\n  return true;\n}","typeGuard":"function isPacketShape(p){\n  switch(p.type){\n    case 0: return typeof p.data === 'object' && p.data !== null;\n    case 1: return p.data === undefined;\n    case 2: return Array.isArray(p.data) && p.data.length>0 && typeof p.data[0]==='string';\n    default: return false;\n  }\n}","tryCatchPattern":"let packet;\ntry { packet = decode(raw); }\ncatch (e) { console.warn('dropping frame', raw, e.message); return; }\nhandle(packet);","preventionTips":["Validate the type byte and JSON shape before trusting a decoded frame.","Extend isPacketValid to model all packet types your client handles.","Always wrap example/custom parser decode() in try/catch at the call site."],"tags":["parser","websocket","example","validation"],"analyzedSha":"ae7fb46e08c5ed964b4a1ea8b1703e816511598e","analyzedAt":"2026-08-03T19:08:14.127Z","schemaVersion":2}