feat: mejorar el manejo de errores en la inicialización de datos y asegurar la preservación de fallos originales
This commit is contained in:
parent
b7df37707b
commit
da52788e65
@ -69,3 +69,13 @@ it("reports connection cleanup errors", async () => {
|
||||
connection.close.mockRejectedValue(new Error("synthetic private detail"));
|
||||
await expect(initializeData({ ...options, admin: true })).rejects.toThrow("connection cleanup");
|
||||
});
|
||||
|
||||
it("preserves the original failure when cleanup also fails", async () => {
|
||||
const cause = new Error("Account failed");
|
||||
users.mockRejectedValue(cause);
|
||||
connection.close.mockRejectedValue(new Error("Close failed"));
|
||||
await expect(initializeData({ ...options, admin: true })).rejects.toMatchObject({
|
||||
message: "Data initialization failed during accounts.", cause,
|
||||
});
|
||||
expect(connection.close).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
@ -19,6 +19,7 @@ export const initializeData = async (options: DataInitializationOptions): Promis
|
||||
}
|
||||
let adapter: ReturnType<typeof createSequelizeAdapter> | undefined;
|
||||
let stage = "connection";
|
||||
let failure: Error | undefined;
|
||||
try {
|
||||
// Seed errors and SQL logging must not expose account data or password hashes.
|
||||
adapter = createSequelizeAdapter({ logging: false });
|
||||
@ -45,12 +46,13 @@ export const initializeData = async (options: DataInitializationOptions): Promis
|
||||
}
|
||||
} catch (cause) {
|
||||
// Only this static message is printed by the CLI; the original cause stays private.
|
||||
throw new Error(`Data initialization failed during ${stage}.`, { cause });
|
||||
failure = new Error(`Data initialization failed during ${stage}.`, { cause });
|
||||
} finally {
|
||||
try {
|
||||
await adapter?.connection.close();
|
||||
} catch (cause) {
|
||||
throw new Error("Data initialization failed during connection cleanup.", { cause });
|
||||
failure ??= new Error("Data initialization failed during connection cleanup.", { cause });
|
||||
}
|
||||
}
|
||||
if (failure) throw failure;
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user