Skip to content

Commit

Permalink
[AMR-41] fix: json parse catch
Browse files Browse the repository at this point in the history
  • Loading branch information
N0chteil committed Dec 2, 2022
1 parent 5e269bd commit c766962
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/win32/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ function fetchAppleMusic() {

const data = fetchITunes();

if (Object.keys(data).length === 0 || data.playerState === "stopped")
if (
!data ||
Object.keys(data).length === 0 ||
data.playerState === "stopped"
)
AppleBridge.emit("stopped", "music");
else AppleBridge.emit(data.playerState, "music", data);
}
Expand All @@ -38,10 +42,10 @@ function fetchAppleMusic() {
* @returns {PlayerState} Player state
*/
export function getPlayerState(): PlayerState {
return fetchITunes().playerState;
return fetchITunes()?.playerState;
}

export function fetchITunes(type = "currentTrack") {
export function fetchITunes(type = "currentTrack"): TrackData {
if (process.platform !== "win32") return;

const data = execSync(
Expand All @@ -56,7 +60,13 @@ export function fetchITunes(type = "currentTrack") {
}
);

return JSON.parse(decodeURI(data)) as TrackData;
try {
return JSON.parse(data);
} catch (e) {
console.error("[Win32][fetchITunes]", "Error parsing JSON:", e);

return undefined;
}
}

/*
Expand Down

0 comments on commit c766962

Please sign in to comment.