{"record":{"id":"f5952944b5e7982f","repo":"grpc-ecosystem/grpc-gateway","slug":"invalid-pattern","errorCode":null,"errorMessage":"invalid pattern","messagePattern":"invalid pattern","errorType":"http","errorClass":"ErrInvalidPattern","httpStatus":500,"severity":"error","filePath":"runtime/pattern.go","lineNumber":17,"sourceCode":"package runtime\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"github.com/grpc-ecosystem/grpc-gateway/v2/utilities\"\n\t\"google.golang.org/grpc/grpclog\"\n)\n\nvar (\n\t// ErrNotMatch indicates that the given HTTP request path does not match to the pattern.\n\tErrNotMatch = errors.New(\"not match to the path pattern\")\n\t// ErrInvalidPattern indicates that the given definition of Pattern is not valid.\n\tErrInvalidPattern = errors.New(\"invalid pattern\")\n)\n\ntype MalformedSequenceError string\n\nfunc (e MalformedSequenceError) Error() string {\n\treturn \"malformed path escape \" + strconv.Quote(string(e))\n}\n\ntype op struct {\n\tcode    utilities.OpCode\n\toperand int\n}\n\n// Pattern is a template pattern of http request paths defined in\n// https://github.com/googleapis/googleapis/blob/master/google/api/http.proto\ntype Pattern struct {\n\t// ops is a list of operations\n\tops []op","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/grpc-ecosystem/grpc-gateway/blob/a58a4436a376a4bcc7d8f10c4d4f919a8438bba9/runtime/pattern.go#L1-L35","documentation":"ErrInvalidPattern is a sentinel error returned by NewPattern when the compiled pattern definition passed in is structurally invalid: an unsupported opcodes version, or an odd number of opcode entries. The pattern cannot be constructed safely, so the library fails fast at startup rather than misbehaving at request time.","triggerScenarios":"Calling runtime.NewPattern(version, ops, pool, verb) where version != 1 (pattern.go:56) or len(ops) is odd (pattern.go:62); typically from generated code or custom pattern construction.","commonSituations":"Hand-editing or post-processing protoc-gen-grpc-gateway generated *_pb.gw.go files and corrupting the ops/pool arrays; mismatched generated code versions where the opcode encoding version differs; custom gateways building patterns manually.","solutions":["Regenerate the *_pb.gw.go files with a protoc-gen-grpc-gateway version matching your protoc plugins and proto annotations (run protoc again, don't hand-edit ops/pool)","Ensure the version argument is 1 and the ops slice contains an even number of entries when constructing patterns manually","Pin grpc-gateway v2 consistently across go.mod and code generation tooling"],"exampleFix":"// before (hand-edited generated code)\np := runtime.MustPattern(runtime.NewPattern(2, []int{0, 0, 1}, []string{\"v1\"}, \"\"))\n// after\n// regenerate *_pb.gw.go; or construct correctly:\np := runtime.MustPattern(runtime.NewPattern(1, []int{utilities.OpLitPush, 0}, []string{\"v1\"}, \"\"))","handlingStrategy":"validation","validationCode":"if version != 1 {\n    return fmt.Errorf(\"unsupported pattern version %d\", version)\n}\nif len(ops)%2 != 0 {\n    return fmt.Errorf(\"ops slice must have even length, got %d\", len(ops))\n}","typeGuard":"func isValidPatternArgs(version int32, ops []int) bool {\n    return version == 1 && len(ops)%2 == 0\n}","tryCatchPattern":"p, err := runtime.NewPattern(1, ops, pool, verb)\nif errors.Is(err, runtime.ErrInvalidPattern) {\n    grpclog.Fatalf(\"bad pattern definition (regenerate *_pb.gw.go): %v\", err)\n}","preventionTips":["Never hand-edit the ops/pool arrays in generated *_pb.gw.go files — regenerate instead","Keep protoc-gen-grpc-gateway, protoc, and the grpc-gateway go module versions in sync","Wrap NewPattern in runtime.MustPattern only after tests confirm generated code integrity"],"tags":["go","grpc-gateway","code-generation","pattern"],"backgroundTag":"invalid-pattern-definition","analyzedSha":"a58a4436a376a4bcc7d8f10c4d4f919a8438bba9","analyzedAt":"2026-09-02T10:28:31.537Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}