{"record":{"id":"73be1f3a42517a6f","repo":"bytebase/bytebase","slug":"failed-to-write-statement","errorCode":null,"errorMessage":"failed to write statement","messagePattern":"failed to write statement","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/api/v1/sql_service.go","lineNumber":1176,"sourceCode":"\n// exportResultToZip exports a single query result to the ZIP archive.\n// It writes both the SQL statement and the formatted result data.\nfunc exportResultToZip(\n\tctx context.Context,\n\tzipw *zip.Writer,\n\tstores *store.Store,\n\tinstance *store.InstanceMessage,\n\tdatabase *store.DatabaseMessage,\n\tresult *v1pb.QueryResult,\n\trequest *v1pb.ExportRequest,\n\tstatementNumber int,\n) error {\n\tbaseFilename := fmt.Sprintf(\"%s/%s/statement-%d\", database.InstanceID, database.DatabaseName, statementNumber)\n\n\t// Write statement file\n\tstatementFilename := fmt.Sprintf(\"%s.sql\", baseFilename)\n\tif err := export.WriteZipEntry(zipw, statementFilename, []byte(result.Statement), request.GetPassword()); err != nil {\n\t\treturn errors.Wrap(err, \"failed to write statement\")\n\t}\n\n\t// Write result file by streaming directly to ZIP\n\tresultExt := strings.ToLower(request.Format.String())\n\tresultFilename := fmt.Sprintf(\"%s.result.%s\", baseFilename, resultExt)\n\tif err := formatExportToZip(ctx, zipw, resultFilename, stores, instance, database, result, request); err != nil {\n\t\treturn errors.Wrap(err, \"failed to write formatted result\")\n\t}\n\n\treturn nil\n}\n\n// formatExportToZip formats query results and writes them directly to a ZIP entry.\n// This function streams the formatted data to minimize memory usage.\nfunc formatExportToZip(\n\tctx context.Context,\n\tzipw *zip.Writer,\n\tfilename string,","sourceCodeStart":1158,"sourceCodeEnd":1194,"githubUrl":"https://github.com/bytebase/bytebase/blob/1870550677fe08f0d2a78c07acd27541464eb945/backend/api/v1/sql_service.go#L1158-L1194","documentation":"exportResultToZip writes each statement's SQL text as a '<instance>/<db>/statement-N.sql' zip entry via export.WriteZipEntry; any failure there is wrapped with this message. The statement file could not be serialized, encrypted, or written into the archive.","triggerScenarios":"export.WriteZipEntry fails for the statement file: zip entry creation error, compression failure, or password-based encryption failure when request.GetPassword() is set.","commonSituations":"Password-protected exports with an invalid/empty password config, statements containing invalid UTF-8 in odd encodings, or zip writer corruption from earlier failed writes.","solutions":["Inspect the wrapped inner error for the concrete zip/crypto failure","If using a password, verify it is set correctly and the encryption mode is supported","Retry the export; if persistent, test without a password to isolate encryption as the cause","Ensure result.Statement is valid content before export"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if (request.password && request.password.length < 1) throw new Error('invalid export password')","typeGuard":null,"tryCatchPattern":"try { await client.export(request) } catch (e) { if (e.message.includes('failed to write statement')) { /* check password/encryption config, retry without password to isolate */ } }","preventionTips":["Verify export passwords are valid before calling","Test exports without password to isolate encryption issues","Keep statements in valid encodings"],"tags":["export","zip","go"],"backgroundTag":"file-write-failed","analyzedSha":"1870550677fe08f0d2a78c07acd27541464eb945","analyzedAt":"2026-09-06T21:16:13.665Z","contentChangedAt":"2026-09-06T21:16:13.665Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}