Skip to content

Commit

Permalink
fix(maloja): Improve handling for Maloja warnings-as-errors
Browse files Browse the repository at this point in the history
* Fixes incorrect handling for duplicate scrobble error from Maloja 3.2.2 #180
* More defensive Maloja scrobble parsing
  • Loading branch information
FoxxMD committed Aug 26, 2024
1 parent ce95c3a commit 5b53b74
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/backend/common/vendor/maloja/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface MalojaScrobbleV3RequestData extends MalojaScrobbleRequestData {
nofix?: boolean
}

interface MalojaScrobbleWarning {
export interface MalojaScrobbleWarning {
type: string
value: string[] | string
desc: string
Expand Down
30 changes: 25 additions & 5 deletions src/backend/scrobblers/MalojaScrobbler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
MalojaScrobbleRequestData,
MalojaScrobbleV2RequestData,
MalojaScrobbleV3RequestData,
MalojaScrobbleV3ResponseData,
MalojaScrobbleV3ResponseData, MalojaScrobbleWarning,
MalojaV2ScrobbleData,
MalojaV3ScrobbleData,
} from "../common/vendor/maloja/interfaces.js";
Expand Down Expand Up @@ -59,7 +59,7 @@ export default class MalojaScrobbler extends AbstractScrobbleClient {
// when the track was scrobbled
time: mTime,
track: {
artists: mArtists,
artists: mArtists = [],
title: mTitle,
album: mAlbum,
// length of the track
Expand All @@ -77,14 +77,14 @@ export default class MalojaScrobbler extends AbstractScrobbleClient {
const {
albumtitle,
name: mAlbumName,
artists: albumArtists
artists: albumArtists = []
} = mAlbum || {};
album = albumtitle ?? mAlbumName;
}
} else {
// scrobble data structure for v2 and below
const {
artists: mArtists,
artists: mArtists = [],
title: mTitle,
album: mAlbum,
duration: mDuration,
Expand Down Expand Up @@ -438,7 +438,11 @@ export default class MalojaScrobbler extends AbstractScrobbleClient {
}
if(warnings.length > 0) {
for(const w of warnings) {
this.logger.warn(`Maloja Warning: ${w.desc} => ${JSON.stringify(w.value)}`)
const warnStr = buildWarningString(w);
if(warnStr.includes('The submitted scrobble was not added')) {
throw new UpstreamError(`Maloja returned a warning but MS treating as error: ${warnStr}`, {showStopper: false});
}
this.logger.warn(`Maloja Warning: ${warnStr}`);
}
}
} else {
Expand Down Expand Up @@ -517,3 +521,19 @@ const buildErrorString = (body: MalojaResponseV3CommonData) => {
}
return `Maloja API returned ${status} of type ${type} "${desc}"${valString !== undefined ? `: ${valString}` : ''}`;
}

const buildWarningString = (w: MalojaScrobbleWarning): string => {
const parts: string[] = [`${typeof w.type === 'string' ? `(${w.type}) ` : ''}${w.desc ?? ''}`];
let vals: string[] = [];
if(w.value !== null && w.value !== undefined) {
if(Array.isArray(w.value)) {
vals = w.value;
} else {
vals.push(w.value);
}
}
if(vals.length > 0) {
parts.push(vals.join(' | '));
}
return parts.join(' => ');
}

0 comments on commit 5b53b74

Please sign in to comment.