Fix error handling to display them correctly without crashing
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import TableEntry from "../models/TableEntry";
|
||||
import RequestError from "../models/RequestError";
|
||||
|
||||
|
||||
export default class UserStatsService {
|
||||
|
||||
@@ -13,13 +15,16 @@ export default class UserStatsService {
|
||||
|
||||
async getStats(): Promise<TableEntry[]> {
|
||||
return fetch(this.apiURL, this.requestInit)
|
||||
.then(res => this.checkResponse(res))
|
||||
.then(data => data.json())
|
||||
.then(res => UserStatsService.checkResponse(res))
|
||||
.then(data => data.json());
|
||||
}
|
||||
|
||||
private checkResponse(response: any): any {
|
||||
private static checkResponse(response: any): any {
|
||||
if (!response.ok) {
|
||||
throw Error(response.statusText);
|
||||
console.log(response);
|
||||
let error = new RequestError(response.statusText);
|
||||
error.response = response;
|
||||
throw error;
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user