From cd6c69062ff427e7e02ab3c55fb45773a2b16558 Mon Sep 17 00:00:00 2001 From: Marks Polakovs Date: Mon, 13 Apr 2020 09:33:00 +0200 Subject: [PATCH] mildly nicer error handling --- .gitignore | 4 +- src/api.ts | 8 +-- src/broadcast/state.ts | 134 ++++++++++++++++++++++++++--------------- 3 files changed, 93 insertions(+), 53 deletions(-) diff --git a/.gitignore b/.gitignore index b0e2808..4386f7f 100644 --- a/.gitignore +++ b/.gitignore @@ -28,4 +28,6 @@ yarn-error.log* .mypy_cache/ env/ -shittyserver.ini \ No newline at end of file +shittyserver.ini + +.idea/ \ No newline at end of file diff --git a/src/api.ts b/src/api.ts index 48ed382..672bb84 100644 --- a/src/api.ts +++ b/src/api.ts @@ -8,7 +8,7 @@ export const BROADCAST_API_BASE_URL = process.env.REACT_APP_BROADCAST_API_BASE!; const MYRADIO_API_KEY = process.env.REACT_APP_MYRADIO_KEY!; -class ApiException extends Error {} +export class ApiException extends Error {} export async function apiRequest( url: string, @@ -47,7 +47,7 @@ export async function myradioApiRequest( return json.payload; } else { console.error(json.payload); - throw new ApiException("Request failed!"); + throw new ApiException(json.payload); } } @@ -61,8 +61,8 @@ export async function broadcastApiRequest( if (json.status === "OK") { return json.payload; } else { - console.error(json.payload); - throw new ApiException("Request failed!"); + console.error(json.reason); + throw new ApiException(json.reason); } } diff --git a/src/broadcast/state.ts b/src/broadcast/state.ts index 4f81243..71c90dd 100644 --- a/src/broadcast/state.ts +++ b/src/broadcast/state.ts @@ -1,6 +1,6 @@ import { createSlice, PayloadAction } from "@reduxjs/toolkit"; import { AppThunk } from "../store"; -import { myradioApiRequest, broadcastApiRequest } from "../api"; +import { myradioApiRequest, broadcastApiRequest, ApiException } from "../api"; import { WebRTCStreamer } from "./rtc_streamer"; import * as MixerState from "../mixer/state"; import * as NavbarState from "../navbar/state"; @@ -10,10 +10,9 @@ import { RecordingStreamer } from "./recording_streamer"; export let streamer: WebRTCStreamer | null = null; export type BroadcastStageEnum = -| "NOT_REGISTERED" -| "REGISTERED" -| "FAILED_REGISTRATION"; - + | "NOT_REGISTERED" + | "REGISTERED" + | "FAILED_REGISTRATION"; interface BroadcastState { stage: BroadcastStageEnum; @@ -47,7 +46,10 @@ const broadcastState = createSlice({ recordingState: "NOT_CONNECTED" } as BroadcastState, reducers: { - changeSetting(state: BroadcastState, action: PayloadAction<{ key: K, val: BroadcastState[K] }>) { + changeSetting( + state: BroadcastState, + action: PayloadAction<{ key: K; val: BroadcastState[K] }> + ) { state[action.payload.key] = action.payload.val; }, toggleTracklisting(state) { @@ -56,9 +58,9 @@ const broadcastState = createSlice({ setConnID(state, action: PayloadAction) { state.connID = action.payload; if (action.payload != null) { - state.stage = "REGISTERED" + state.stage = "REGISTERED"; } else { - state.stage = "NOT_REGISTERED" + state.stage = "NOT_REGISTERED"; } }, setConnectionState(state, action: PayloadAction) { @@ -66,24 +68,23 @@ const broadcastState = createSlice({ }, setRecordingState(state, action: PayloadAction) { state.recordingState = action.payload; - }, - }, + } + } }); export default broadcastState.reducer; - export interface TrackListItem { audiologid: number; } - -export const changeBroadcastSetting = (key: K, val: BroadcastState[K]): AppThunk => async (dispatch, getState) => { - dispatch(broadcastState.actions.changeSetting({key: key, val: val})); - dispatch(changeTimeslot()); - } - - +export const changeBroadcastSetting = ( + key: K, + val: BroadcastState[K] +): AppThunk => async (dispatch, getState) => { + dispatch(broadcastState.actions.changeSetting({ key: key, val: val })); + dispatch(changeTimeslot()); +}; export const registerTimeslot = (): AppThunk => async (dispatch, getState) => { if (getState().broadcast.stage === "NOT_REGISTERED") { @@ -92,24 +93,52 @@ export const registerTimeslot = (): AppThunk => async (dispatch, getState) => { const timeslotid = state.currentTimeslot?.timeslot_id; console.log("Attempting to Register for Broadcast."); var sourceid = getState().broadcast.sourceID; - var connID = (await sendBroadcastRegister(timeslotid, memberid, sourceid)); - if (connID !== undefined) { - dispatch(broadcastState.actions.setConnID(connID["connid"])); - dispatch(startStreaming()); + try { + var connID = await sendBroadcastRegister(timeslotid, memberid, sourceid); + if (connID !== undefined) { + dispatch(broadcastState.actions.setConnID(connID["connid"])); + dispatch(startStreaming()); + } + } catch (e) { + if (e instanceof ApiException) { + dispatch( + NavbarState.showAlert({ + content: e.message, + color: "danger", + closure: 10000 + }) + ); + } else { + // let raygun handle it + throw e; + } } - } }; export const cancelTimeslot = (): AppThunk => async (dispatch, getState) => { if (getState().broadcast.stage === "REGISTERED") { console.log("Attempting to Cancel Broadcast."); - var response = (await sendBroadcastCancel(getState().broadcast.connID)); - dispatch(stopStreaming()); - if (response != null) { - dispatch(broadcastState.actions.setConnID(null)); + try { + var response = await sendBroadcastCancel(getState().broadcast.connID); + dispatch(stopStreaming()); + if (response != null) { + dispatch(broadcastState.actions.setConnID(null)); + } + } catch (e) { + if (e instanceof ApiException) { + dispatch( + NavbarState.showAlert({ + content: e.message, + color: "danger", + closure: 10000 + }) + ); + } else { + // let raygun handle it + throw e; + } } - } }; @@ -117,25 +146,42 @@ export const changeTimeslot = (): AppThunk => async (dispatch, getState) => { var state = getState().broadcast; if (state.stage === "REGISTERED") { console.log("Attempting to Change Broadcast Options."); - var response = (await sendBroadcastChange(state.connID, state.sourceID, state.autoNewsBeginning, state.autoNewsMiddle, state.autoNewsEnd)); - + var response = await sendBroadcastChange( + state.connID, + state.sourceID, + state.autoNewsBeginning, + state.autoNewsMiddle, + state.autoNewsEnd + ); } }; -export function sendBroadcastRegister(timeslotid: number | undefined, memberid: number | undefined, sourceid: number): Promise { +export function sendBroadcastRegister( + timeslotid: number | undefined, + memberid: number | undefined, + sourceid: number +): Promise { return broadcastApiRequest("/registerTimeslot", "POST", { memberid: memberid, timeslotid: timeslotid, sourceid: sourceid }); } -export function sendBroadcastCancel(connid: number | null): Promise { +export function sendBroadcastCancel( + connid: number | null +): Promise { return broadcastApiRequest("/cancelTimeslot", "POST", { connid: connid }); } -export function sendBroadcastChange(connid: number | null, sourceid: number, beginning: boolean, middle: boolean, end: boolean): Promise { +export function sendBroadcastChange( + connid: number | null, + sourceid: number, + beginning: boolean, + middle: boolean, + end: boolean +): Promise { return broadcastApiRequest("/changeTimeslot", "POST", { connid: connid, sourceid: sourceid, @@ -145,14 +191,6 @@ export function sendBroadcastChange(connid: number | null, sourceid: number, beg }); } - - - - - - - - export const { toggleTracklisting } = broadcastState.actions; export const tracklistStart = ( @@ -184,7 +222,7 @@ export function sendTracklistStart(trackid: number): Promise { return myradioApiRequest("/tracklistItem", "POST", { trackid: trackid, source: "w", - state: "c", + state: "c" }); } @@ -194,19 +232,19 @@ export const startStreaming = (): AppThunk => async (dispatch, getState) => { NavbarState.showAlert({ color: "warning", content: "You are not WebStudio Trained and cannot go live.", - closure: 7000, + closure: 7000 }) ); return; } streamer = new WebRTCStreamer(MixerState.destination.stream); - streamer.addConnectionStateListener((state) => { + streamer.addConnectionStateListener(state => { dispatch(broadcastState.actions.setConnectionState(state)); }); await streamer.start(); }; -export const stopStreaming = (): AppThunk => async (dispatch) => { +export const stopStreaming = (): AppThunk => async dispatch => { if (streamer) { await streamer.stop(); streamer = null; @@ -219,11 +257,11 @@ let recorder: RecordingStreamer; export const startRecording = (): AppThunk => async dispatch => { recorder = new RecordingStreamer(MixerState.destination.stream); - recorder.addConnectionStateListener((state) => { + recorder.addConnectionStateListener(state => { dispatch(broadcastState.actions.setRecordingState(state)); }); await recorder.start(); -} +}; export const stopRecording = (): AppThunk => async dispatch => { if (recorder) { @@ -231,4 +269,4 @@ export const stopRecording = (): AppThunk => async dispatch => { } else { console.warn("stopRecording called with no recorder!"); } -} +};