{"record":{"id":"58504ebfc3a8135a","repo":"toeverything/AFFiNE","slug":"bad-segment-mode","errorCode":null,"errorMessage":"Bad segment: ${mode}","messagePattern":"Bad segment: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"blocksuite/affine/blocks/surface/src/utils/path-data-parser/parser.ts","lineNumber":116,"sourceCode":"      for (let i = index; i < index + paramsCount; i++) {\n        const numbeToken = tokens[i];\n        if (isType(numbeToken, NUMBER)) {\n          params[params.length] = +numbeToken.text;\n        } else {\n          throw new Error(\n            'Param not a number: ' + mode + ',' + numbeToken.text\n          );\n        }\n      }\n      if (typeof PARAMS[mode] === 'number') {\n        const segment: Segment = { key: mode, data: params };\n        segments.push(segment);\n        index += paramsCount;\n        token = tokens[index];\n        if (mode === 'M') mode = 'L';\n        if (mode === 'm') mode = 'l';\n      } else {\n        throw new Error('Bad segment: ' + mode);\n      }\n    } else {\n      throw new Error('Path data ended short');\n    }\n  }\n  return segments;\n}\n\nexport function serialize(segments: Segment[]): string {\n  const tokens: (string | number)[] = [];\n  for (const { key, data } of segments) {\n    tokens.push(key);\n    switch (key) {\n      case 'C':\n      case 'c':\n        tokens.push(\n          data[0],\n          `${data[1]},`,","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/toeverything/AFFiNE/blob/26c515e050211269e911f7d9cfe162a26c83ed98/blocksuite/affine/blocks/surface/src/utils/path-data-parser/parser.ts#L98-L134","documentation":"Thrown by parsePath after collecting parameters when typeof PARAMS[mode] !== 'number'. PARAMS maps each SVG command letter to its parameter count; if 'mode' is a token text not present in PARAMS (an unsupported/unrecognised command letter), the lookup is undefined and the segment is rejected as 'Bad segment'. This guards against unknown SVG path commands the parser does not support.","triggerScenarios":"Calling parsePath with a path string containing a command letter outside the supported set {A,a,C,c,H,h,L,l,M,m,Q,q,S,s,T,t,V,v,Z,z}. For example an 'F', 'X', 'R', or any stray letter treated as a command. The tokenizer accepts single letters as COMMAND tokens, so a stray letter that is not a valid SVG path command reaches this branch.","commonSituations":"Malformed or hand-authored path strings; exporter bugs emitting non-standard commands; corrupt surface/shape data; copy-paste of non-SVG text into a path field.","solutions":["Pre-validate the path string: allow only the SVG path command letters in PARAMS; reject or strip others before parsePath.","Use parsePath's return-early behaviours: note parsePath auto-prepends 'M0,0' for a missing moveto, so ensure the string starts with M/m after any cleanup.","Catch the error and degrade gracefully (default empty path, skip the element)."],"exampleFix":"// before\nparsePath(rawPath); // rawPath contains 'X' command -> 'Bad segment: X'\n\n// after\nconst VALID = /^[AaCcHhLlMmQqSsTtVvZz0-9.,\\-+eE \\t\\r\\n]+$/;\nconst safe = VALID.test(rawPath) ? rawPath : 'M0,0';\nparsePath(safe);","handlingStrategy":"validation","validationCode":"const VALID_PATH = /^[AaCcHhLlMmQqSsTtVvZz0-9.,\\-+eE \\t\\r\\n]+$/;\nconst safe = VALID_PATH.test(rawPath) ? rawPath : 'M0,0';\nparsePath(safe);","typeGuard":"const ALLOWED_CMDS = new Set('AaCcHhLlMmQqSsTtVvZz');\nfunction isKnownCommand(letter) { return ALLOWED_CMDS.has(letter); }","tryCatchPattern":"let segments;\ntry { segments = parsePath(rawPath); }\ncatch (e) {\n  segments = parsePath('M0,0');\n}","preventionTips":["Restrict path strings to the SVG command set supported by PARAMS.","Reject non-SVG letters in path data at the input boundary.","Catch Bad-segment errors and fall back to a default path."],"tags":["svg","path-parser","parsing","surface"],"backgroundTag":null,"analyzedSha":"26c515e050211269e911f7d9cfe162a26c83ed98","analyzedAt":"2026-08-12T13:15:16.447Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}