use some status information to handle errors better

This commit is contained in:
Sven Czarnian
2022-11-05 00:10:27 +01:00
parent 47c09f2fdd
commit df0a6ade57
6 changed files with 107 additions and 24 deletions

View File

@@ -1,12 +1,21 @@
import axios from 'axios';
import { Configuration } from './configuration';
import { Session } from './session';
import { AirportOverview } from '../types';
import {
AirportOverviewBackend,
AirportOverview,
BackendReturnStatus,
} from '../types';
export class Airport {
static async all(): Promise<AirportOverview[]> {
static async all(): Promise<AirportOverviewBackend> {
const token = Session.bearerToken();
if (!token) return [];
if (!token) {
return {
status: BackendReturnStatus.Unauthorized,
airports: [],
};
}
return axios
.get<AirportOverview[]>(`${Configuration.resourceServer}/airport/all`, {
@@ -14,7 +23,17 @@ export class Airport {
Authorization: `Bearer ${token}`,
},
})
.then((response) => response.data)
.catch(() => []);
.then((response) => {
return {
status: BackendReturnStatus.Ok,
airports: response.data,
};
})
.catch((err) => {
return {
status: err.status === 401 ? BackendReturnStatus.Unauthorized : BackendReturnStatus.Failure,
airports: [],
};
});
}
}