bytebase/bytebase · error

failed to write statement

Error message

failed to write statement

What it means

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.

Source

Thrown at backend/api/v1/sql_service.go:1176

// exportResultToZip exports a single query result to the ZIP archive.
// It writes both the SQL statement and the formatted result data.
func exportResultToZip(
	ctx context.Context,
	zipw *zip.Writer,
	stores *store.Store,
	instance *store.InstanceMessage,
	database *store.DatabaseMessage,
	result *v1pb.QueryResult,
	request *v1pb.ExportRequest,
	statementNumber int,
) error {
	baseFilename := fmt.Sprintf("%s/%s/statement-%d", database.InstanceID, database.DatabaseName, statementNumber)

	// Write statement file
	statementFilename := fmt.Sprintf("%s.sql", baseFilename)
	if err := export.WriteZipEntry(zipw, statementFilename, []byte(result.Statement), request.GetPassword()); err != nil {
		return errors.Wrap(err, "failed to write statement")
	}

	// Write result file by streaming directly to ZIP
	resultExt := strings.ToLower(request.Format.String())
	resultFilename := fmt.Sprintf("%s.result.%s", baseFilename, resultExt)
	if err := formatExportToZip(ctx, zipw, resultFilename, stores, instance, database, result, request); err != nil {
		return errors.Wrap(err, "failed to write formatted result")
	}

	return nil
}

// formatExportToZip formats query results and writes them directly to a ZIP entry.
// This function streams the formatted data to minimize memory usage.
func formatExportToZip(
	ctx context.Context,
	zipw *zip.Writer,
	filename string,

View on GitHub (pinned to 1870550677)

Solutions

  1. Inspect the wrapped inner error for the concrete zip/crypto failure
  2. If using a password, verify it is set correctly and the encryption mode is supported
  3. Retry the export; if persistent, test without a password to isolate encryption as the cause
  4. Ensure result.Statement is valid content before export
Defensive patterns

Strategy: try-catch

Validate before calling

if (request.password && request.password.length < 1) throw new Error('invalid export password')

Try / catch

try { await client.export(request) } catch (e) { if (e.message.includes('failed to write statement')) { /* check password/encryption config, retry without password to isolate */ } }

Prevention

When it happens

Trigger: export.WriteZipEntry fails for the statement file: zip entry creation error, compression failure, or password-based encryption failure when request.GetPassword() is set.

Common situations: 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.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of bytebase/bytebase@1870550677 (2026-09-06). Data as JSON: /api/errors/73be1f3a42517a6f. Report an issue: GitHub.