mildly nicer error handling

This commit is contained in:
Marks Polakovs 2020-04-13 09:33:00 +02:00
parent 3ec6b9b25c
commit cd6c69062f
3 changed files with 93 additions and 53 deletions

4
.gitignore vendored
View file

@ -28,4 +28,6 @@ yarn-error.log*
.mypy_cache/
env/
shittyserver.ini
shittyserver.ini
.idea/

View file

@ -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);
}
}

View file

@ -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<K extends keyof BroadcastState>(state: BroadcastState, action: PayloadAction<{ key: K, val: BroadcastState[K] }>) {
changeSetting<K extends keyof BroadcastState>(
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<number | null>) {
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<ConnectionStateEnum>) {
@ -66,24 +68,23 @@ const broadcastState = createSlice({
},
setRecordingState(state, action: PayloadAction<ConnectionStateEnum>) {
state.recordingState = action.payload;
},
},
}
}
});
export default broadcastState.reducer;
export interface TrackListItem {
audiologid: number;
}
export const changeBroadcastSetting = <K extends keyof BroadcastState>(key: K, val: BroadcastState[K]): AppThunk => async (dispatch, getState) => {
dispatch(broadcastState.actions.changeSetting({key: key, val: val}));
dispatch(changeTimeslot());
}
export const changeBroadcastSetting = <K extends keyof BroadcastState>(
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<any> {
export function sendBroadcastRegister(
timeslotid: number | undefined,
memberid: number | undefined,
sourceid: number
): Promise<any> {
return broadcastApiRequest("/registerTimeslot", "POST", {
memberid: memberid,
timeslotid: timeslotid,
sourceid: sourceid
});
}
export function sendBroadcastCancel(connid: number | null): Promise<string | null> {
export function sendBroadcastCancel(
connid: number | null
): Promise<string | null> {
return broadcastApiRequest("/cancelTimeslot", "POST", {
connid: connid
});
}
export function sendBroadcastChange(connid: number | null, sourceid: number, beginning: boolean, middle: boolean, end: boolean): Promise<any> {
export function sendBroadcastChange(
connid: number | null,
sourceid: number,
beginning: boolean,
middle: boolean,
end: boolean
): Promise<any> {
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<TrackListItem> {
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!");
}
}
};