tangled
alpha
login
or
join now
vielle.dev
/
site
0
fork
atom
Personal Site
0
fork
atom
overview
issues
pulls
pipelines
Handle exceptions in the SSE
vielle.dev
7 months ago
11459e8c
cc79a25c
verified
This commit was signed with the committer's
known signature
.
vielle.dev
SSH Key Fingerprint:
SHA256:/4bvxqoEh9iMdjAPgcgAgXKZZQTROL3ULiPt6nH9RSs=
+29
-20
1 changed file
expand all
collapse all
unified
split
src
pages
now-playing-sse.ts
+29
-20
src/pages/now-playing-sse.ts
···
1
1
-
import { nowPlaying } from "/components/playing/spotify";
1
1
+
import { spotifyNowPlaying, SpotifyError } from "/components/playing/spotify";
2
2
3
3
export async function GET() {
4
4
const update = async (): Promise<string> => {
5
5
// extract a subset to reduce size for client
6
6
// not huge savings but tesco yk
7
7
-
const playing = await nowPlaying().then((playing) =>
8
8
-
playing
9
9
-
? {
10
10
-
type: "track",
11
11
-
id: playing.id,
12
12
-
name: playing.name,
13
13
-
album: {
14
14
-
images: playing.album.images.map((x) => ({
15
15
-
url: x.url,
16
16
-
width: x.width,
17
17
-
height: x.height,
7
7
+
const playing = await spotifyNowPlaying()
8
8
+
.then((playing) =>
9
9
+
playing
10
10
+
? {
11
11
+
type: "track",
12
12
+
id: playing.id,
13
13
+
name: playing.name,
14
14
+
album: {
15
15
+
images: playing.album.images.map((x) => ({
16
16
+
url: x.url,
17
17
+
width: x.width,
18
18
+
height: x.height,
19
19
+
})),
20
20
+
},
21
21
+
artists: playing.artists.map((x) => ({
22
22
+
name: x.name,
23
23
+
id: x.id,
18
24
})),
19
19
-
},
20
20
-
artists: playing.artists.map((x) => ({
21
21
-
name: x.name,
22
22
-
id: x.id,
23
23
-
})),
24
24
-
}
25
25
-
: null,
26
26
-
);
25
25
+
}
26
26
+
: null,
27
27
+
)
28
28
+
.catch((err) => {
29
29
+
if (!(err instanceof SpotifyError))
30
30
+
throw new Error("Unhandled exception");
31
31
+
console.error(err.code, err.human, err.details);
32
32
+
33
33
+
if (err.code === "NO_CONTENT") return null;
34
34
+
return err;
35
35
+
});
27
36
28
37
// SSE syntax: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format
29
38
return `event: playing\ndata: ${JSON.stringify(playing)}\n\n`;