refactor: better errors (#590)

* refactor: improve error messages from server
This commit is contained in:
Carlos Valente
2023-11-14 21:54:56 +01:00
committed by GitHub
parent a41fe8806b
commit 1ca3b9c134
9 changed files with 26 additions and 29 deletions
-1
View File
@@ -11,7 +11,6 @@ export function maybeAxiosError(error: unknown) {
const statusText = (error as AxiosError).response?.statusText ?? '';
let data = (error as AxiosError).response?.data ?? '';
if (typeof data === 'object') {
// TODO: use error instead, when migrated
if ('message' in data) {
data = JSON.stringify(data.message);
} else {
@@ -100,7 +100,7 @@ export const useEventAction = () => {
// @ts-expect-error -- we know that the object is well formed now
await _addEventMutation.mutateAsync(newEvent);
} catch (error) {
logAxiosError('Error fetching data', error);
logAxiosError('Failed adding event', error);
}
},
[_addEventMutation, defaultPublic, queryClient, startTimeIsLastEnd],
@@ -46,7 +46,6 @@ export class DataProvider {
static async clearRundown() {
data.rundown = [];
// @ts-expect-error -- not sure how to type, this is library side
await db.write();
}
@@ -100,7 +99,6 @@ export class DataProvider {
}
static async persist() {
// @ts-expect-error -- not sure how to type, this is library side
await db.write();
}
@@ -149,7 +149,7 @@ export const postAliases = async (req, res) => {
await DataProvider.setAliases(newAliases);
res.status(200).send(newAliases);
} catch (error) {
res.status(400).send(error);
res.status(400).send({ message: error.toString() });
}
};
@@ -172,7 +172,7 @@ export const postUserFields = async (req, res) => {
await DataProvider.setUserFields(newData);
res.status(200).send(newData);
} catch (error) {
res.status(400).send(error);
res.status(400).send({ message: error.toString() });
}
};
@@ -235,7 +235,7 @@ export const postSettings = async (req, res) => {
await DataProvider.setSettings(newData);
res.status(200).send(newData);
} catch (error) {
res.status(400).send(error);
res.status(400).send({ message: error.toString() });
}
};
@@ -270,7 +270,7 @@ export const postViewSettings = async (req, res) => {
await DataProvider.setViewSettings(newData);
res.status(200).send(newData);
} catch (error) {
res.status(400).send(error);
res.status(400).send({ message: error.toString() });
}
};
@@ -298,7 +298,7 @@ export const postOscSubscriptions = async (req, res) => {
res.send(oscSettings).status(200);
} catch (error) {
res.status(400).send(error);
res.status(400).send({ message: error.toString() });
}
};
@@ -319,7 +319,7 @@ export const postOSC = async (req, res) => {
res.send(oscSettings).status(200);
} catch (error) {
res.status(400).send(error);
res.status(400).send({ message: error.toString() });
}
};
@@ -348,7 +348,7 @@ export async function patchPartialProjectFile(req, res) {
}
res.status(200).send();
} catch (error) {
res.status(400).send(error);
res.status(400).send({ message: error.toString() });
}
}
@@ -409,6 +409,6 @@ export const postNew: RequestHandler = async (req, res) => {
await deleteAllEvents();
res.status(201).send(newData);
} catch (error) {
res.status(400).send(error);
res.status(400).send({ message: error.toString() });
}
};
@@ -13,14 +13,14 @@ export const pbStart = async (req, res) => {
const { eventId, eventIndex } = req.query;
if (eventId) {
const success = PlaybackService.startById(eventId);
success ? res.sendStatus(202) : res.status(400).send('Invalid event ID');
success ? res.sendStatus(202) : res.status(400).send({ message: 'Invalid event ID' });
} else if (eventIndex) {
const index = Number(eventIndex);
if (!isNaN(index)) {
const success = PlaybackService.startByIndex(eventIndex - 1);
success ? res.sendStatus(202) : res.status(400).send('Invalid event index');
success ? res.sendStatus(202) : res.status(400).send({ message: 'Invalid event index' });
} else {
res.status(400).send('Invalid event index');
res.status(400).send({ message: 'Invalid event index' });
}
} else {
PlaybackService.start();
@@ -69,17 +69,17 @@ export const pbLoad = async (req, res) => {
const { eventId, eventIndex } = req.query;
if (eventId) {
const success = PlaybackService.loadById(eventId);
success ? res.sendStatus(202) : res.status(400).send('Invalid event ID');
success ? res.sendStatus(202) : res.status(400).send({ message: 'Invalid event ID' });
} else if (eventIndex) {
const index = Number(eventIndex);
if (!isNaN(index)) {
const success = PlaybackService.loadByIndex(eventIndex - 1);
success ? res.sendStatus(202) : res.status(400).send('Invalid event index');
success ? res.sendStatus(202) : res.status(400).send({ message: 'Invalid event index' });
} else {
res.status(400).send('Invalid event index');
res.status(400).send({ message: 'Invalid event index' });
}
} else {
res.status(400).send('No event given');
res.status(400).send({ message: 'No event given' });
}
};
@@ -30,6 +30,6 @@ export const postProject: RequestHandler = async (req, res) => {
const newData = await DataProvider.setProjectData(newEvent);
res.status(200).send(newData);
} catch (error) {
res.status(400).send(error);
res.status(400).send({ message: error.toString() });
}
};
@@ -29,7 +29,7 @@ export const rundownPost: RequestHandler = async (req, res) => {
const newEvent = await addEvent(req.body);
res.status(201).send(newEvent);
} catch (error) {
res.status(400).send(error);
res.status(400).send({ message: error.toString() });
}
};
@@ -44,7 +44,7 @@ export const rundownPut: RequestHandler = async (req, res) => {
const event = await editEvent(req.body);
res.status(200).send(event);
} catch (error) {
res.status(400).send(error);
res.status(400).send({ message: error.toString() });
}
};
@@ -58,7 +58,7 @@ export const rundownReorder: RequestHandler = async (req, res) => {
const event = await reorderEvent(eventId, from, to);
res.status(200).send(event);
} catch (error) {
res.status(400).send(error);
res.status(400).send({ message: error.toString() });
}
};
@@ -72,7 +72,7 @@ export const rundownSwap: RequestHandler = async (req, res) => {
await swapEvents(from, to);
res.sendStatus(200);
} catch (error) {
res.status(400).send(error);
res.status(400).send({ message: error.toString() });
}
};
@@ -83,7 +83,7 @@ export const rundownApplyDelay: RequestHandler = async (req, res) => {
await applyDelay(req.params.eventId);
res.sendStatus(200);
} catch (error) {
res.status(400).send(error);
res.status(400).send({ message: error.toString() });
}
};
@@ -94,7 +94,7 @@ export const deleteEventById: RequestHandler = async (req, res) => {
await deleteEvent(req.params.eventId);
res.sendStatus(204);
} catch (error) {
res.status(400).send(error);
res.status(400).send({ message: error.toString() });
}
};
@@ -105,6 +105,6 @@ export const rundownDelete: RequestHandler = async (req, res) => {
await deleteAllEvents();
res.sendStatus(204);
} catch (error) {
res.status(400).send(error);
res.status(400).send({ message: error.toString() });
}
};
+1 -1
View File
@@ -67,7 +67,7 @@ async function loadDb() {
return { db, data };
}
export let db = {};
export let db = {} as Low<DatabaseModel>;
export let data = {} as DatabaseModel;
export const dbLoadingProcess = loadDb();
@@ -158,7 +158,7 @@ export function updateTimer(affectedIds?: string[]) {
export async function addEvent(eventData: Partial<OntimeEvent> | Partial<OntimeDelay> | Partial<OntimeBlock>) {
const numEvents = DataProvider.getRundownLength();
if (numEvents > MAX_EVENTS) {
throw new Error(`ERROR: Reached limit number of ${MAX_EVENTS} events`);
throw new Error(`Reached limit number of ${MAX_EVENTS} events`);
}
let newEvent: Partial<OntimeBaseEvent> = {};