Skip to content

Commit d9494cb

Browse files
committed
improved error handling
1 parent 344be33 commit d9494cb

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

setup.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -922,11 +922,17 @@ export async function dataImportWithoutDocker(
922922
importData: () => Promise<void>,
923923
): Promise<void> {
924924
if (!process.env.MONGO_DB_URL) {
925-
console.log("Couldn't find mongodb url");
926-
return;
925+
throw new Error("MongoDB URL is not configured. Please run setup first.");
927926
}
928927

929-
const isDbEmpty = await checkDb(process.env.MONGO_DB_URL);
928+
let isDbEmpty: boolean;
929+
try {
930+
isDbEmpty = await checkDb(process.env.MONGO_DB_URL);
931+
} catch (error) {
932+
throw new Error(
933+
`Failed to check database: ${error instanceof Error ? error.message : String(error)}`,
934+
);
935+
}
930936
if (!isDbEmpty) {
931937
const { shouldOverwriteData } = await inquirer.prompt({
932938
type: "confirm",

tests/setup/dataImportFlow.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,44 @@ describe("Data Importation Without Docker", () => {
198198
expect(importDefaultDataMock).not.toBeCalled();
199199
expect(importDataMock).not.toBeCalled();
200200
});
201+
202+
it("should handle database connection failure gracefully", async () => {
203+
const checkDbMock = vi
204+
.fn()
205+
.mockImplementation(async (): Promise<boolean> => {
206+
return false;
207+
});
208+
const wipeExistingDataMock = vi
209+
.fn()
210+
.mockImplementation(async (): Promise<void> => {
211+
return Promise.resolve();
212+
});
213+
const importDataMock = vi
214+
.fn()
215+
.mockImplementation(async (): Promise<void> => {
216+
return Promise.resolve();
217+
});
218+
const importDefaultDataMock = vi
219+
.fn()
220+
.mockImplementation(async (): Promise<void> => {
221+
return Promise.resolve();
222+
});
223+
const errorMessage = "Database connection failed";
224+
checkDbMock.mockRejectedValueOnce(new Error(errorMessage));
225+
226+
await expect(
227+
dataImportWithoutDocker(
228+
checkDbMock,
229+
wipeExistingDataMock,
230+
importDefaultDataMock,
231+
importDataMock,
232+
),
233+
).rejects.toThrow(errorMessage);
234+
235+
expect(wipeExistingDataMock).not.toBeCalled();
236+
expect(importDefaultDataMock).not.toBeCalled();
237+
expect(importDataMock).not.toBeCalled();
238+
});
201239
});
202240

203241
describe("Data Importation With Docker", () => {

0 commit comments

Comments
 (0)