Skip to content

Commit 7006dd1

Browse files
authored
error handling & panic screen (#19)
1 parent 6d8b276 commit 7006dd1

File tree

7 files changed

+63
-10
lines changed

7 files changed

+63
-10
lines changed

docker-compose.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,5 @@ services:
2727
- ./nginx.conf:/etc/nginx/nginx.conf:ro
2828
- ./web/static/index.html:/www/data/static/index.html:ro
2929
- ./web/dist:/www/data/static/dist:ro
30-
- ./art/remote_art/static/notfound.png:/www/data/static/notfound.png:ro
3130
ports:
3231
- 80:80

web/src/AlbumPage/Component.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,15 @@ export default function AlbumPage({ album, albumartist }) {
4242
const { loaded, err, data } = useMPDQuery(
4343
`find albumartist "${albumartist}" album "${album}"`
4444
);
45-
if (!loaded) return <div />;
46-
if (err) return <h3>Error</h3>;
45+
if (!loaded) return <h2>Loading...</h2>;
46+
if (err) {
47+
debugger;
48+
return <>
49+
<h1>{album}</h1>
50+
<h2>Error Fetching Data</h2>
51+
<p>{data.toString()}</p>
52+
</>
53+
}
4754

4855
const tracks = Array.from(tracksFromData(data));
4956

web/src/Browse/Browse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import urls from "../urls";
2-
import React from "react";
2+
import React, { useEffect } from "react";
33
import { Link, Route, Switch } from "react-router-dom";
44

55
import map from "lodash/map";

web/src/PanicScreen.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from "react";
2+
3+
export function PanicScreen({uncaughtPromise, obj}) {
4+
let contents;
5+
if (obj instanceof Error) {
6+
contents = <p>{obj.toString()}</p>;
7+
} else if (obj instanceof Response) {
8+
contents = <>
9+
<p><b>The application provided a fetch Response object for context</b></p>
10+
<p>Status: {obj.status} {obj.statusText}</p>
11+
</>
12+
} else if (obj === undefined) {
13+
contents = <p>No further context was provided.</p>
14+
} else {
15+
contents = <p>The provided context (type {typeof obj}) could not be interpreted.</p>
16+
}
17+
return <>
18+
<h1>An Unexpected Issue Occurred</h1>
19+
<p><b>{uncaughtPromise ? "Uncaught Promise" : "Standard Error"}</b></p>
20+
<h2>Context</h2>
21+
{contents}
22+
</>;
23+
}

web/src/hooks.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,16 @@ export function useMPDQuery(cmd) {
4040
const [ls, success, err, reset] = useLoadState();
4141

4242
useEffect(() => {
43-
if (cmd !== "") {
44-
reset();
45-
mpdQuery(cmd).then(success).catch(err);
46-
}
43+
if (cmd === "") {
44+
return;
45+
}
46+
const doit = async () => {
47+
reset();
48+
mpdQuery(cmd)
49+
.then(success)
50+
.catch(err);
51+
};
52+
doit().catch(err);
4753
}, [cmd]);
4854

4955
return ls;

web/src/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { SnackbarContext, SnackbarProvider } from "./Snackbar/Context";
1010
import { ConnectionContext, ConnectionProvider } from "./App/Context";
1111
import { QueueContext, QueueProvider } from "./Queue/Context";
1212
import { PlaybackContext, PlaybackProvider } from "./PlaybackControls/Context";
13+
import { PanicScreen } from "./PanicScreen";
1314

1415
import { pullPlaybackInfo, pullQueueInfo } from "./mpd";
1516
import { ChannelProvider } from "./ChannelPage/Context";
@@ -28,6 +29,17 @@ function Root() {
2829
return <App />;
2930
}
3031
async function start() {
32+
const root = document.getElementById("root");
33+
window.onerror = function(_a, _b, _c, _d, err) {
34+
ReactDOM.render(<PanicScreen
35+
uncaughtPromise={false}
36+
obj={err}/>, root)
37+
};
38+
window.onunhandledrejection = function(ev) {
39+
ReactDOM.render(<PanicScreen
40+
uncaughtPromise={true}
41+
obj={ev.reason}/>, root)
42+
};
3143
const playback = await pullPlaybackInfo();
3244
const queue = await pullQueueInfo();
3345
const channels = await fetch("/go/channels").then((res) => res.json());

web/src/mpd.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
*/
44

55
export async function mpdQuery(command) {
6-
const resp = await fetch(`/go/cmd?q=${encodeURIComponent(command)}`);
7-
return await resp.text();
6+
const path = `/go/cmd?q=${encodeURIComponent(command)}`;
7+
const resp = await fetch(path);
8+
if (!resp.ok) {
9+
const err = new Error(`${path}: ${resp.status} ${resp.statusText}`);
10+
return Promise.reject(err);
11+
}
12+
const data = await resp.text();
13+
return Promise.resolve(data);
814
}
915

1016
export function* parsePairs(data) {

0 commit comments

Comments
 (0)