{"record":{"id":"97c6acf0c0a7467e","repo":"vitessio/vitess","slug":"all-vterrors-states-are-not-mapped-to-mysql-errors","errorCode":null,"errorMessage":"all vterrors states are not mapped to mysql errors","messagePattern":"all vterrors states are not mapped to mysql errors","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"go/mysql/sqlerror/sql_error.go","lineNumber":375,"sourceCode":"}\n\n// ConvertStateToMySQLErrorCode returns MySQL error code for the given vterrors.State\n// If the state is == 0, an empty string is returned\nfunc ConvertStateToMySQLErrorCode(state vterrors.State) string {\n\ts := getStateToMySQLState(state)\n\treturn s.num.ToString()\n}\n\n// ConvertStateToMySQLState returns MySQL state for the given vterrors.State\n// If the state is == 0, an empty string is returned\nfunc ConvertStateToMySQLState(state vterrors.State) string {\n\ts := getStateToMySQLState(state)\n\treturn s.state\n}\n\nfunc init() {\n\tif len(stateToMysqlCode) != int(vterrors.NumOfStates) {\n\t\tpanic(\"all vterrors states are not mapped to mysql errors\")\n\t}\n}\n\nfunc convertToMysqlError(err error) error {\n\terrState := vterrors.ErrState(err)\n\tif errState == vterrors.Undefined {\n\t\treturn err\n\t}\n\tmysqlCode, ok := stateToMysqlCode[errState]\n\tif !ok {\n\t\treturn err\n\t}\n\treturn NewSQLError(mysqlCode.num, mysqlCode.state, err.Error())\n}\n\nvar isGRPCOverflowRE = regexp.MustCompile(`.*?grpc: (received|trying to send) message larger than max \\(\\d+ vs. \\d+\\)`)\n\nfunc demuxResourceExhaustedErrors(msg string) ErrorCode {","sourceCodeStart":357,"sourceCodeEnd":393,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/mysql/sqlerror/sql_error.go#L357-L393","documentation":"The sqlerror package maintains a map from every vtrpcpb.Code state to a MySQL SQLSTATE string. An init()-time panic fires if the number of mapped states doesn't equal vterrors.NumOfStates, i.e. a new vterrors state code was added without a corresponding MySQL state mapping. This is a build-time/program-start invariant check, not a runtime data error.","triggerScenarios":"Starting any binary that imports go/mysql/sqlerror after adding a new vtrpcpb.Code state to the vterrors package without adding it to stateToMysqlCode; upgrading vitess libs where one module version is out of sync.","commonSituations":"Developers adding a new gRPC status code; mismatched vendored module versions where vterrors was bumped but sqlerror's map wasn't (partial dependency upgrade).","solutions":["Add the missing state to stateToMysqlCode in go/mysql/sqlerror/sql_error.go with an appropriate MySQL SQLSTATE.","Run go mod tidy / align all vitess module versions so vterrors and sqlerror come from the same commit.","Rebuild from a clean checkout to rule out stale builds mixing old and new packages."],"exampleFix":"// before (new state added, map unchanged)\nvar stateToMysqlCode = map[vtrpcpb.Code]mysqlState{ ... } // missing new state\n// after\nvar stateToMysqlCode = map[vtrpcpb.Code]mysqlState{\n    ...\n    vtrpcpb.Code_NEW_STATE: {state: \"HY000\", code: 1105}, // add mapping\n}","handlingStrategy":"validation","validationCode":"// startup self-check before shipping:\nif len(stateToMysqlCode) != int(vterrors.NumOfStates) {\n    return fmt.Errorf(\"missing mysql state mapping for a vterrors state\")\n}","typeGuard":null,"tryCatchPattern":"// init panic cannot be recovered at use-site; add a unit test:\nfunc TestAllStatesMapped(t *testing.T) {\n    require.Equal(t, int(vterrors.NumOfStates), len(stateToMysqlCode))\n}","preventionTips":["Add a unit test asserting the map size equals vterrors.NumOfStates.","When adding a vtrpcpb.Code, update stateToMysqlCode in the same PR.","Keep vitess module versions aligned across the dependency graph."],"tags":["mysql","sqlstate","startup-panic","developer-error"],"backgroundTag":"unmapped-error-state","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}