File tree Expand file tree Collapse file tree 2 files changed +47
-3
lines changed Expand file tree Collapse file tree 2 files changed +47
-3
lines changed Original file line number Diff line number Diff line change @@ -922,11 +922,17 @@ export async function dataImportWithoutDocker(
922
922
importData : ( ) => Promise < void > ,
923
923
) : Promise < void > {
924
924
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." ) ;
927
926
}
928
927
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
+ }
930
936
if ( ! isDbEmpty ) {
931
937
const { shouldOverwriteData } = await inquirer . prompt ( {
932
938
type : "confirm" ,
Original file line number Diff line number Diff line change @@ -198,6 +198,44 @@ describe("Data Importation Without Docker", () => {
198
198
expect ( importDefaultDataMock ) . not . toBeCalled ( ) ;
199
199
expect ( importDataMock ) . not . toBeCalled ( ) ;
200
200
} ) ;
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
+ } ) ;
201
239
} ) ;
202
240
203
241
describe ( "Data Importation With Docker" , ( ) => {
You can’t perform that action at this time.
0 commit comments