Add error handling

This commit is contained in:
Tim 2021-07-05 04:37:28 +02:00
parent 7ec967a08b
commit 9fbdd98bc6
2 changed files with 21 additions and 5 deletions

View File

@ -41,8 +41,11 @@ export default class PerudoApi {
});
}
private get<T = any>(url: string, getParameters?: Record<string, any>) {
private get<T extends ApiObject>(url: string, getParameters?: Record<string, any>): AxiosPromise<T> {
return axios.get(url).then((response: AxiosResponse<T>) => {
if (response?.data?.errors?.length > 0) {
throw response.data.errors?.join();
}
return response;
})
}

View File

@ -1,5 +1,9 @@
<template>
<div class="home">
<div v-if="error">
Errors: <br>
{{ error }}
</div>
<md-field>
<label>Name</label>
<md-input v-model="name" :readonly="gameJoined"></md-input>
@ -44,6 +48,7 @@ import {ApiObject, GameState, IsStartedObject, JoinCreateGameObject} from "@/obj
})
export default class Home extends Vue {
private gameState: GameState = GameState.Setup;
private error: string | null = null;
private name: string = 'Bobby';
private code: string = '';
@ -66,7 +71,9 @@ export default class Home extends Vue {
this.owner = true;
this.gameState = GameState.Joined;
});
}).catch((reason => {
this.error = reason;
}));
}
private joinGame() {
@ -74,14 +81,18 @@ export default class Home extends Vue {
this.playerId = response.player.id;
this.gameState = GameState.Joined;
})
}).catch((reason => {
this.error = reason;
}));
}
private startGame() {
if (this.playerId) {
PerudoApi.instance.startGame(this.playerId).then((response: ApiObject) => {
this.gameState = GameState.Started;
})
}).catch((reason => {
this.error = reason;
}));
}
}
@ -91,7 +102,9 @@ export default class Home extends Vue {
if (response.started) {
this.gameState = GameState.Started;
}
});
}).catch((reason => {
this.error = reason;
}));
}
}
}