Skip to content

Commit

Permalink
improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Aad1tya27 committed Jan 13, 2025
1 parent 344be33 commit d9494cb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
12 changes: 9 additions & 3 deletions setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -922,11 +922,17 @@ export async function dataImportWithoutDocker(
importData: () => Promise<void>,
): Promise<void> {
if (!process.env.MONGO_DB_URL) {
console.log("Couldn't find mongodb url");
return;
throw new Error("MongoDB URL is not configured. Please run setup first.");
}

const isDbEmpty = await checkDb(process.env.MONGO_DB_URL);
let isDbEmpty: boolean;
try {
isDbEmpty = await checkDb(process.env.MONGO_DB_URL);
} catch (error) {
throw new Error(
`Failed to check database: ${error instanceof Error ? error.message : String(error)}`,
);
}
if (!isDbEmpty) {
const { shouldOverwriteData } = await inquirer.prompt({
type: "confirm",
Expand Down
38 changes: 38 additions & 0 deletions tests/setup/dataImportFlow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,44 @@ describe("Data Importation Without Docker", () => {
expect(importDefaultDataMock).not.toBeCalled();
expect(importDataMock).not.toBeCalled();
});

it("should handle database connection failure gracefully", async () => {
const checkDbMock = vi
.fn()
.mockImplementation(async (): Promise<boolean> => {
return false;
});
const wipeExistingDataMock = vi
.fn()
.mockImplementation(async (): Promise<void> => {
return Promise.resolve();
});
const importDataMock = vi
.fn()
.mockImplementation(async (): Promise<void> => {
return Promise.resolve();
});
const importDefaultDataMock = vi
.fn()
.mockImplementation(async (): Promise<void> => {
return Promise.resolve();
});
const errorMessage = "Database connection failed";
checkDbMock.mockRejectedValueOnce(new Error(errorMessage));

await expect(
dataImportWithoutDocker(
checkDbMock,
wipeExistingDataMock,
importDefaultDataMock,
importDataMock,
),
).rejects.toThrow(errorMessage);

expect(wipeExistingDataMock).not.toBeCalled();
expect(importDefaultDataMock).not.toBeCalled();
expect(importDataMock).not.toBeCalled();
});
});

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

0 comments on commit d9494cb

Please sign in to comment.