{"record":{"id":"315f7e72d4be7570","repo":"denoland/deno","slug":"err-socket-bad-type","errorCode":"ERR_SOCKET_BAD_TYPE","errorMessage":"Bad socket type specified. Valid types are: udp4, udp6","messagePattern":"Bad socket type specified\\. Valid types are: udp4, udp6","errorType":"error_code","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/internal/dgram.ts","lineNumber":117,"sourceCode":"    const handle = new UDP();\n\n    handle.lookup = FunctionPrototypeBind(lookup4, handle, lookup);\n\n    return handle;\n  }\n\n  if (type === \"udp6\") {\n    const handle = new UDP();\n\n    handle.lookup = FunctionPrototypeBind(lookup6, handle, lookup);\n    handle.bind = handle.bind6;\n    handle.connect = handle.connect6;\n    handle.send = handle.send6;\n\n    return handle;\n  }\n\n  throw new ERR_SOCKET_BAD_TYPE();\n}\n\nfunction _createSocketHandle(\n  address: string,\n  port: number,\n  addressType: SocketType,\n  fd: number,\n  flags: number,\n) {\n  const handle = newHandle(addressType);\n  let err;\n\n  if (isInt32(fd) && fd > 0) {\n    const type = guessHandleType(fd);\n\n    if (type !== \"UDP\") {\n      err = MapPrototypeGet(codeMap, \"EINVAL\")!;\n    } else {","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/internal/dgram.ts#L99-L135","documentation":"dgram.createSocket() builds its native handle through newHandle(type): only exact lowercase 'udp4' and 'udp6' are accepted; every other value falls through to ERR_SOCKET_BAD_TYPE. Unlike Node, Deno's polyfill does not implement 'unix_dgram', so that otherwise-valid Node type also throws here.","triggerScenarios":"dgram.createSocket('unix_dgram') (unix datagram socket, unsupported); createSocket('UDP4') or 'udp4 ' (case/whitespace mismatch); a type variable that is undefined or came from unvalidated config.","commonSituations":"Porting Node services that log to a local unix datagram socket (syslog-style) to Deno; config-driven socket types with casing mismatches; feature flags selecting socket families.","solutions":["Use exactly 'udp4' or 'udp6'.","Replace unix datagram IPC with Deno.connect({ transport: 'unix' }) streams or a loopback UDP socket.","Normalize and whitelist config-driven types before createSocket (lowercase, trim, allowlist)."],"exampleFix":"// before\nconst sock = dgram.createSocket(config.sockType); // 'unix_dgram'\n\n// after\nconst sock = dgram.createSocket(config.sockType === 'udp6' ? 'udp6' : 'udp4');","handlingStrategy":"validation","validationCode":"const SOCKET_TYPES = new Set(['udp4', 'udp6']);\nconst type = String(config.socketType ?? '').toLowerCase().trim();\nif (!SOCKET_TYPES.has(type)) {\n  throw new RangeError('socket type must be udp4 or udp6, got ' + config.socketType);\n}\nconst sock = dgram.createSocket(type);","typeGuard":"const isUdpType = (t) => t === 'udp4' || t === 'udp6';","tryCatchPattern":"try {\n  sock = dgram.createSocket(type);\n} catch (err) {\n  if (err?.code === 'ERR_SOCKET_BAD_TYPE') {\n    sock = dgram.createSocket('udp4'); // safe default after logging the config error\n  } else {\n    throw err;\n  }\n}","preventionTips":["Whitelist socket types from config; normalize case and trim before createSocket.","Do not port Node unix_dgram code to Deno node:dgram - redesign to unix streams or loopback UDP.","Fail fast on unknown socket types at startup, not mid-request."],"tags":["dgram","udp","create-socket","socket-type","node-compat"],"backgroundTag":"dgram-socket-type","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}