Skip to content

Commit

Permalink
Better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasBomholtz committed Mar 1, 2023
1 parent 9b0bd60 commit 3e1d0de
Show file tree
Hide file tree
Showing 10 changed files with 386 additions and 37 deletions.
10 changes: 0 additions & 10 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,6 @@ export default class Application extends Model implements Application21 {
return result;
}

async get(): Promise<any> {
try {
const response = await HTTP.get(`${this.url}?expand=2&verbose=true`);
return response.data;
} catch (err) {
this.handleException(`Failed to get application: ${this.id}`, err);
}
return {};
}

async getAll(): Promise<Application[]> {
let result: Application[] = [];
try {
Expand Down
6 changes: 6 additions & 0 deletions src/cmd/configure.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Wapp from '../wapp.configure';
import setup from '../util/setup_cli';
import tui from '../util/tui';

const sections = [
{
Expand All @@ -23,6 +24,11 @@ export default async function configure(argv: string[]) {
}

const wapp = new Wapp();
if (!wapp.present()) {
tui.showError('No Wapp found in current folder');
return;
}

await wapp.init();
await wapp.configure();
}
50 changes: 33 additions & 17 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default class Model {
const response = await HTTP.get(`${this.url}?expand=2&verbose=true`);
this.parse(response.data);
return true;
} catch (err) {
} catch (err: any) {
this.handleException(`Failed to fetch ${this.meta.type}`, err);
}

Expand All @@ -111,21 +111,10 @@ export default class Model {
try {
await HTTP.delete(`${this.url}`);
} catch (err: any) {
switch (err.response.data.code) {
case 300020:
// Installation already deleted
break;
case 9900067:
// Already deleted
break;
case 300024:
throw Error('Can not delete application that is published!');
default:
this.handleException(
`Failed to delete ${this.meta.type}: ${this.id}`,
err
);
}
this.handleException(
`Failed to delete ${this.meta.type}: ${this.id}`,
err
);
}
}

Expand Down Expand Up @@ -168,7 +157,34 @@ export default class Model {
if (this.meta.type === 'session') {
tui.showError(msg, err);
} else {
Model.handleException(msg, err);
switch (err.response?.data?.code) {
case 117000000:
// do not print invalid session error
break;
case 300020:
// Installation already deleted
break;
case 9900067:
// Already deleted
break;
case 300098:
tui.showError(`${err.response.data.message}`);
tui.showError(
`Please visit ${Config.host()}/pricing for more information`
);
break;
case 9900147:
tui.showError(`${msg} because it was not found on Wappsto`);
break;
case 300024:
throw Error('Can not delete application that is published!');
case 400006:
tui.showError('You do not have permission to this wapp.');
tui.showError('Try to logout and login with the correct user');
break;
default:
Model.handleException(msg, err);
}
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/util/tui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { yellow, magenta, green, blue, red, white, bold } from 'kleur/colors';
import figlet from 'figlet';
import { clearLine, cursorTo } from 'readline';
import updateNotifier from 'simple-update-notifier';
import config from '../config';
import { VERSION } from './version';

class Tui {
Expand Down Expand Up @@ -182,9 +181,6 @@ class Tui {
if (data) {
if (data.code === 117000000) {
// do not print invalid session error
} else if (data.code === 300098) {
strMsg += `${red(data.message)}\n`;
strMsg += `Please visit ${config.host()}/pricing for more information`;
} else {
strErr = `${JSON.stringify(data)}\n`;
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const VERSION = '2.0.11';
const VERSION = '2.0.12';
export { VERSION };
11 changes: 8 additions & 3 deletions src/wapp.configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ export default class ConfigureWapp extends Wapp {
return;
}

await section('Load application', () => {
return this.application.fetch();
});
if (
!(await section('Load application', () => {
return this.application.fetch();
}))
) {
tui.showError("Can't configure wapp, without valid application");
return;
}

const answers = await section('Wait for user input', () => {
return questions.configureWapp(
Expand Down
5 changes: 5 additions & 0 deletions test/configure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ describe('Configure', () => {
console.log = org;
});

it('can handle missing wapp', async () => {
await configure([]);
expect(mockedAxios.get).toHaveBeenCalledTimes(0);
});

it('can handle change the description', async () => {
mockedAxios.get.mockResolvedValueOnce({
data: applicationResponse,
Expand Down
2 changes: 1 addition & 1 deletion test/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('Delete', () => {
.mockResolvedValueOnce({
data: installationResponse,
});
mockedAxios.delete.mockRejectedValueOnce({
mockedAxios.delete.mockRejectedValue({
data: {},
});

Expand Down
11 changes: 10 additions & 1 deletion test/serve.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import axios from 'axios';
import { setup, teardown } from './util/setup';
import serve from '../src/cmd/serve';

describe('Serve', () => {
let mockedAxios: jest.Mocked<typeof axios>;

beforeEach(async () => {
await setup();
mockedAxios = await setup();
});

afterEach(() => {
Expand All @@ -28,4 +32,9 @@ describe('Serve', () => {

console.log = org;
});

it('can handle missing wapp', async () => {
await serve([]);
expect(mockedAxios.get).toHaveBeenCalledTimes(0);
});
});
Loading

0 comments on commit 3e1d0de

Please sign in to comment.