codegen: complete pattern matching with error handling