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

fix: Apps not throwing errors correctly
This commit is contained in:
Nicolas Meienberger 2022-07-25 16:56:53 +00:00 committed by GitHub
commit e3ccd7129c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 5 deletions

View file

@ -78,6 +78,20 @@ describe('Install app', () => {
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 () => {
await expect(AppsService.installApp(app1.id, {})).rejects.toThrowError('Variable TEST_FIELD is required');
});

View file

@ -46,7 +46,7 @@ const startApp = async (appName: string): Promise<App> => {
await App.update({ id: appName }, { status: AppStatusEnum.RUNNING });
} catch (e) {
await App.update({ id: appName }, { status: AppStatusEnum.STOPPED });
console.log(e);
throw e;
}
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();
// 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 });
@ -125,9 +130,15 @@ const stopApp = async (id: string): Promise<App> => {
// Run script
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;
return app;
@ -148,7 +159,8 @@ const uninstallApp = async (id: string): Promise<App> => {
try {
await runAppScript(['uninstall', id]);
} catch (e) {
console.log(e);
await App.update({ id }, { status: AppStatusEnum.STOPPED });
throw e;
}
await App.delete({ id });