{"record":{"id":"885d6f7dcd550c86","repo":"XTLS/Xray-core","slug":"failed-to-check-code","errorCode":null,"errorMessage":"failed to check code ","messagePattern":"failed to check code ","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/geodata/geodat_loader.go","lineNumber":23,"sourceCode":"\t\"bytes\"\n\t\"io\"\n\t\"runtime\"\n\t\"strings\"\n\n\t\"github.com/xtls/xray-core/common/errors\"\n\t\"github.com/xtls/xray-core/common/platform/filesystem\"\n\n\t\"google.golang.org/protobuf/proto\"\n)\n\nfunc checkFile(file, code string) error {\n\tr, err := filesystem.OpenAsset(file)\n\tif err != nil {\n\t\treturn errors.New(\"failed to open \", file).Base(err)\n\t}\n\tdefer r.Close()\n\tif _, err := find(r, []byte(code), false); err != nil {\n\t\treturn errors.New(\"failed to check code \", code, \" from \", file).Base(err)\n\t}\n\treturn nil\n}\n\nfunc loadFile(file, code string) ([]byte, error) {\n\truntime.GC() // peak mem\n\tr, err := filesystem.OpenAsset(file)\n\tif err != nil {\n\t\treturn nil, errors.New(\"failed to open \", file).Base(err)\n\t}\n\tdefer r.Close()\n\tbs, err := find(r, []byte(code), true)\n\tif err != nil {\n\t\treturn nil, errors.New(\"failed to load code \", code, \" from \", file).Base(err)\n\t}\n\treturn bs, nil\n}\n","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/XTLS/Xray-core/blob/7d214f8b094f75322fa3990f8aadad1c912f24f5/common/geodata/geodat_loader.go#L5-L41","documentation":"Wrapped error from checkFile when the dat file opened fine but the find() scanner could not locate the requested code header (e.g. 'cn', 'google') while scanning the protobuf stream in verify mode (readBody=false). The base error carries the underlying cause — typically io.ErrUnexpectedEOF/EOF from a truncated or structurally invalid dat file, or the code simply not existing in that file.","triggerScenarios":"checkFile('geosite.dat', code) where code is absent (typo like 'China' instead of 'cn', or a code only present in an extended dat) or where the file is truncated so the header never matches.","commonSituations":"Routing rules using a geosite/geoip code that the shipped dat does not contain (regional codes, 'geolocation-!cn' variants on minimal dat files); partially downloaded dat files; custom condensed dat files stripped of many codes.","solutions":["Correct the code in the routing rule to one present in your dat file (check the geosite code list for your dat source).","Replace the dat file with the full official release (v2fly) and retry — truncation also produces this.","If using custom dat files, regenerate them including the codes your config references."],"exampleFix":"// before (config)\n{\"type\": \"field\", \"outboundTag\": \"direct\", \"domain\": [\"geosite:China\"]}\n\n// after\n{\"type\": \"field\", \"outboundTag\": \"direct\", \"domain\": [\"geosite:cn\"]}","handlingStrategy":"validation","validationCode":"// Startup smoke check for every geosite/geoip code your config uses:\nfor _, code := range neededCodes {\n    if err := checkFile(\"geosite.dat\", code); err != nil { /* fix code or dat before serving */ }\n}","typeGuard":null,"tryCatchPattern":"if err := checkFile(file, code); err != nil {\n    if strings.Contains(err.Error(), \"failed to check code\") { /* code missing/truncated: fix dat or code */ }\n}","preventionTips":["Use canonical lowercase codes (cn, google, geolocation-!cn)","Use full official dat releases, not stripped custom ones"],"tags":["geodata","routing","config","xray"],"backgroundTag":null,"analyzedSha":"7d214f8b094f75322fa3990f8aadad1c912f24f5","analyzedAt":"2026-08-15T14:26:24.325Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}