{"id":"0139863d678e32e8","repo":"socketio/socket.io","slug":"unknown-event-name-eventname","errorCode":null,"errorMessage":"unknown event name: ${eventName}","messagePattern":"unknown event name: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"examples/custom-parsers/src/custom-parser.js","lineNumber":68,"sourceCode":"  }\n  pack (packet) {\n    let eventName = packet.data[0];\n    let flatPacket = {\n      data: packet.data[1],\n      nsp: packet.nsp\n    };\n    switch (eventName) {\n      case 'string':\n        flatPacket._id = 1;\n        return stringSchema.encode(flatPacket);\n      case 'numeric':\n        flatPacket._id = 2;\n        return numericSchema.encode(flatPacket);\n      case 'binary':\n        flatPacket._id = 3;\n        return binarySchema.encode(flatPacket);\n      default:\n        throw new Error('unknown event name: ' + eventName);\n    }\n  }\n}\n\nclass Decoder extends Emitter {\n  add (obj) {\n    if (typeof obj === 'string') {\n      this.parseJSON(obj);\n    } else {\n      this.parseBinary(obj);\n    }\n  }\n  parseJSON (obj) {\n    try {\n      let decoded = JSON.parse(obj);\n      this.emit('decoded', decoded);\n    } catch (e) {\n      this.emit('decoded', errorPacket);","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/socketio/socket.io/blob/ae7fb46e08c5ed964b4a1ea8b1703e816511598e/examples/custom-parsers/src/custom-parser.js#L50-L86","documentation":"Thrown by the custom-parser example's Encoder.pack() when the first element of packet.data (the event name) does not match any of the three hard-coded schema names ('string', 'numeric', 'binary'). This example uses schemapack to map specific event names to specific binary schemas, so any event name outside that whitelist has no encoder.","triggerScenarios":"Calling the custom Encoder with an EVENT packet whose data[0] is anything other than the literal strings 'string', 'numeric', or 'binary' (e.g. socket.emit('hello') through this parser).","commonSituations":"Adapting the custom-parser example to a real app but forgetting to add new schema branches for the app's actual event names; or emitting a reserved/system event through the custom parser.","solutions":["Add a case branch in pack() for each event name your app emits, mapping it to a schemapack schema.","If you want a generic fallback, add a default branch that JSON-stringifies the packet instead of throwing.","Restrict your application's emit() calls to the event names defined in the switch statement."],"exampleFix":"// before\nswitch (eventName) {\n  case 'string': ... // no default -> throws\n}\n\n// after\nswitch (eventName) {\n  case 'string': ... \n  case 'numeric': ... \n  case 'binary': ... \n  case 'hello':\n    flatPacket._id = 4;\n    return helloSchema.encode(flatPacket);\n  default:\n    throw new Error('unknown event name: ' + eventName);\n}","handlingStrategy":"validation","validationCode":"const KNOWN = new Set(['string','numeric','binary']);\nfunction canEncode(eventName){ return KNOWN.has(eventName); }","typeGuard":"function isKnownEvent(name){ return ['string','numeric','binary'].includes(name); }","tryCatchPattern":"if(!isKnownEvent(packet.data[0])){ throw new Error('unknown event name: '+packet.data[0]); }","preventionTips":["Register a schema branch in pack() for every event name your app emits.","Add a generic JSON fallback default branch instead of throwing.","Keep an allow-list of event names and validate before emitting."],"tags":["parser","example","validation","binary"],"analyzedSha":"ae7fb46e08c5ed964b4a1ea8b1703e816511598e","analyzedAt":"2026-08-03T19:08:14.127Z","schemaVersion":2}