Преглед на файлове

Merge pull request #136 from meienberger/fix/app-errors

fix: Apps not throwing errors correctly
Nicolas Meienberger преди 2 години
родител
ревизия
e3ccd7129c
променени са 2 файла, в които са добавени 31 реда и са изтрити 5 реда
  1. 14 0
      packages/system-api/src/modules/apps/__tests__/apps.service.test.ts
  2. 17 5
      packages/system-api/src/modules/apps/apps.service.ts

+ 14 - 0
packages/system-api/src/modules/apps/__tests__/apps.service.test.ts

@@ -78,6 +78,20 @@ describe('Install app', () => {
     spy.mockRestore();
     spy.mockRestore();
   });
   });
 
 
+  it('Should delete app if install script fails', async () => {
+    const spy = jest.spyOn(childProcess, 'execFile');
+    spy.mockImplementation(() => {
+      throw new Error('Test error');
+    });
+
+    await expect(AppsService.installApp(app1.id, { TEST_FIELD: 'test' })).rejects.toThrow('Test error');
+
+    const app = await App.findOne({ where: { id: app1.id } });
+
+    expect(app).toBeNull();
+    spy.mockRestore();
+  });
+
   it('Should throw if required form fields are missing', async () => {
   it('Should throw if required form fields are missing', async () => {
     await expect(AppsService.installApp(app1.id, {})).rejects.toThrowError('Variable TEST_FIELD is required');
     await expect(AppsService.installApp(app1.id, {})).rejects.toThrowError('Variable TEST_FIELD is required');
   });
   });

+ 17 - 5
packages/system-api/src/modules/apps/apps.service.ts

@@ -46,7 +46,7 @@ const startApp = async (appName: string): Promise<App> => {
     await App.update({ id: appName }, { status: AppStatusEnum.RUNNING });
     await App.update({ id: appName }, { status: AppStatusEnum.RUNNING });
   } catch (e) {
   } catch (e) {
     await App.update({ id: appName }, { status: AppStatusEnum.STOPPED });
     await App.update({ id: appName }, { status: AppStatusEnum.STOPPED });
-    console.log(e);
+    throw e;
   }
   }
 
 
   app = (await App.findOne({ where: { id: appName } })) as App;
   app = (await App.findOne({ where: { id: appName } })) as App;
@@ -75,7 +75,12 @@ const installApp = async (id: string, form: Record<string, string>): Promise<App
     app = await App.create({ id, status: AppStatusEnum.INSTALLING, config: form }).save();
     app = await App.create({ id, status: AppStatusEnum.INSTALLING, config: form }).save();
 
 
     // Run script
     // Run script
-    await runAppScript(['install', id]);
+    try {
+      await runAppScript(['install', id]);
+    } catch (e) {
+      await App.delete({ id });
+      throw e;
+    }
   }
   }
 
 
   await App.update({ id }, { status: AppStatusEnum.RUNNING });
   await App.update({ id }, { status: AppStatusEnum.RUNNING });
@@ -125,9 +130,15 @@ const stopApp = async (id: string): Promise<App> => {
 
 
   // Run script
   // Run script
   await App.update({ id }, { status: AppStatusEnum.STOPPING });
   await App.update({ id }, { status: AppStatusEnum.STOPPING });
-  await runAppScript(['stop', id]);
 
 
-  await App.update({ id }, { status: AppStatusEnum.STOPPED });
+  try {
+    await runAppScript(['stop', id]);
+    await App.update({ id }, { status: AppStatusEnum.STOPPED });
+  } catch (e) {
+    await App.update({ id }, { status: AppStatusEnum.RUNNING });
+    throw e;
+  }
+
   app = (await App.findOne({ where: { id } })) as App;
   app = (await App.findOne({ where: { id } })) as App;
 
 
   return app;
   return app;
@@ -148,7 +159,8 @@ const uninstallApp = async (id: string): Promise<App> => {
   try {
   try {
     await runAppScript(['uninstall', id]);
     await runAppScript(['uninstall', id]);
   } catch (e) {
   } catch (e) {
-    console.log(e);
+    await App.update({ id }, { status: AppStatusEnum.STOPPED });
+    throw e;
   }
   }
 
 
   await App.delete({ id });
   await App.delete({ id });