Skip to content

Email for professionals and teams

Revert "fix(import): keep imported messages when the contact update fails (#183)" (#185)

Hieu Nguyen (Jack)1a537fa5996e
diff --git a/src/lib/import/service.ts b/src/lib/import/service.ts
index 1caf466..4162381 100644
--- a/src/lib/import/service.ts
+++ b/src/lib/import/service.ts
@@ -39,7 +39,7 @@ export async function importMessagesToMailbox(
}
} catch (error) {
result.skipped += 1;
- result.errors.push(`${message.filename}: ${describeImportError(error)}`);
+ result.errors.push(`${message.filename}: ${error instanceof Error ? error.message : "Import failed"}`);
}
}
return result;
@@ -118,26 +118,16 @@ async function importMessageToMailbox(
httpMetadata: { contentType: "message/rfc822" },
});
await storeMessageAttachments(env, messageId, parsed.attachments, { validate: false });
+ const contactAddress = placement.direction === "outbound" ? toAddr : fromAddr;
+ await upsertContactFromAddress(env, {
+ userId: input.userId,
+ address: contactAddress,
+ source: placement.direction === "outbound" ? "outbound" : "inbound",
+ });
} catch (error) {
await deleteMessageWithObjects(env, db, messageId, rawR2Key);
throw error;
}
- // The contact is a by-product: the message is already stored, so a failed update must not discard it.
- await upsertContactFromAddress(env, {
- userId: input.userId,
- address: placement.direction === "outbound" ? toAddr : fromAddr,
- source: placement.direction === "outbound" ? "outbound" : "inbound",
- }).catch((error) => console.error("Import contact update failed", input.filename, describeImportError(error)));
-
return true;
}
-
-/**
- * Drizzle wraps driver errors as "Failed query: <sql> params: <values>", which
- * hides the reason and repeats message data; the driver's own message is on `cause`.
- */
-export function describeImportError(error: unknown): string {
- if (!(error instanceof Error)) return "Import failed";
- return error.cause instanceof Error && error.message.startsWith("Failed query") ? error.cause.message : error.message;
-}
diff --git a/tests/import-contact-failure.test.mjs b/tests/import-contact-failure.test.mjs
deleted file mode 100644
index 0983220..0000000
--- a/tests/import-contact-failure.test.mjs
+++ /dev/null
@@ -1,92 +0,0 @@
-import assert from "node:assert/strict";
-import { mkdtempSync, rmSync } from "node:fs";
-import { dirname, join } from "node:path";
-import test, { after } from "node:test";
-import { fileURLToPath, pathToFileURL } from "node:url";
-import { build } from "esbuild";
-
-const root = join(dirname(fileURLToPath(import.meta.url)), "..");
-const outDir = mkdtempSync(join(root, "tests", ".tmp-import-contact-"));
-after(() => rmSync(outDir, { recursive: true, force: true }));
-
-await build({
- entryPoints: {
- service: join(root, "src/lib/import/service.ts"),
- database: join(root, "server/runtime/sqlite-database.ts"),
- migrate: join(root, "server/runtime/migrate.ts"),
- },
- outdir: outDir,
- outExtension: { ".js": ".mjs" },
- bundle: true,
- platform: "node",
- format: "esm",
- target: "node22",
- tsconfig: join(root, "tsconfig.json"),
- external: ["better-sqlite3", "drizzle-orm", "drizzle-orm/*", "postal-mime"],
- logLevel: "silent",
-});
-
-const load = (name) => import(pathToFileURL(join(outDir, `${name}.mjs`)).href);
-const { importMessagesToMailbox, describeImportError } = await load("service");
-const { SqliteDatabase } = await load("database");
-const { applyMigrations } = await load("migrate");
-
-const raw = new TextEncoder().encode([
- "From: Kevin <[email protected]>",
- "To: Iris <[email protected]>",
- "Subject: Offer",
- "Message-ID: <[email protected]>",
- "Date: Tue, 2 Feb 2021 10:00:00 +0000",
- "",
- "Hello",
-].join("\r\n")).buffer;
-
-/** A D1 stand-in whose writes to `contacts` fail, like a transient D1 error under import load. */
-function failingContacts(database) {
- return new Proxy(database, {
- get(target, property) {
- if (property !== "prepare") return Reflect.get(target, property);
- return (sql) => {
- if (/^update "contacts"|^insert into "contacts"/i.test(sql)) {
- return { bind: () => ({ run: async () => { throw new Error("D1_ERROR: Network connection lost."); }, all: async () => { throw new Error("D1_ERROR: Network connection lost."); }, raw: async () => { throw new Error("D1_ERROR: Network connection lost."); } }) };
- }
- return target.prepare(sql);
- };
- },
- });
-}
-
-test("a failed contact update keeps the imported message", async (t) => {
- const database = new SqliteDatabase(join(outDir, "import.sqlite"));
- t.after(() => database.db.close());
- await applyMigrations(database, join(root, "drizzle/migrations"));
- const now = Math.floor(Date.now() / 1000);
- database.db.prepare("INSERT INTO users (id, email, name, password_hash, created_at) VALUES ('u1', '[email protected]', 'Kevin', 'x', ?)").run(now);
- database.db.prepare("INSERT INTO domains (id, user_id, hostname, zone_id, created_at) VALUES ('d1', 'u1', 'example.com', 'zone', ?)").run(now);
- database.db.prepare("INSERT INTO mailboxes (id, user_id, domain_id, local_part, created_at) VALUES ('mb1', 'u1', 'd1', 'kevin', ?)").run(now);
- const stored = [];
- const env = { DB: failingContacts(database), BUCKET: { put: async (key) => stored.push(key), delete: async () => undefined } };
- const errors = [];
- const originalError = console.error;
- console.error = (...args) => errors.push(args.join(" "));
- t.after(() => { console.error = originalError; });
-
- const result = await importMessagesToMailbox(env, {
- userId: "u1",
- mailboxId: "mb1",
- destination: { type: "system", section: "sent" },
- messages: [{ filename: "takeout-1.eml", raw }],
- });
-
- assert.deepEqual(result, { imported: 1, skipped: 0, errors: [] });
- assert.equal(database.db.prepare("SELECT count(*) AS n FROM messages").get().n, 1);
- assert.equal(stored.length, 1);
- assert.match(errors.join("\n"), /Import contact update failed takeout-1\.eml D1_ERROR: Network connection lost/);
-});
-
-test("import errors show the driver's reason instead of the query and its values", () => {
- const wrapped = new Error('Failed query: update "contacts" set "display_name" = ? params: Iris', { cause: new Error("D1_ERROR: Network connection lost.") });
- assert.equal(describeImportError(wrapped), "D1_ERROR: Network connection lost.");
- assert.equal(describeImportError(new Error("Message is too large")), "Message is too large");
- assert.equal(describeImportError("nope"), "Import failed");
-});