Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/FoxxMD/multi-scrobbler. Scrobble plays from multiple sources to multiple clients docs.multi-scrobbler.app
deezer docker jellyfin koito lastfm listenbrainz maloja mopidy mpris music music-assistant plex scrobble self-hosted spotify subsonic tautulli youtube-music
Something went wrong. Try again.
2.5 kB · 82 lines
TypeScript
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283import type { MarkOptional } from "ts-essentials";import { truncateStringToLength } from "./StringUtils.ts";import { type ErrorLike, isErrorLike } from 'serialize-error';
export type ErrorIsh = Error | MarkOptional<ErrorLike, 'stack'>;
export const isErrorIsh = (val: unknown): val is ErrorIsh => { if(val === undefined || val === null || typeof val !== 'object') { return false; } if(!('message' in val)) { return false; } if('cause' in val) { if(!isErrorIsh(val.cause)) { return false; } } return true;}
/** * Adapted from https://github.com/voxpelli/pony-cause * */export const _messageWithCauses = (err: ErrorIsh, seen = new Set<ErrorIsh>(), msgTransform: MessageTransformer = MessageTransformerDefault, joiner: string = ' => '): string => { if (!(err instanceof Error) && !isErrorLike(err)) return '';
const message = err.message;
// Ensure we don't go circular or crazily deep if (seen.has(err)) { return msgTransform(message) + `${joiner}...`; }
const cause = getErrorCause(err);
if (cause) { seen.add(err);
return (msgTransform(message) + joiner + _messageWithCauses(cause, seen, msgTransform, joiner)); } else { return msgTransform(message); }};/** * Adapted from https://github.com/voxpelli/pony-cause * */export const messageWithCauses = (err: ErrorIsh, msgTransformer?: MessageTransformer, joiner?: string) => _messageWithCauses(err, new Set<ErrorIsh>(), msgTransformer, joiner);export const messageWithCausesTruncated = (length: number) => { const t = truncateStringToLength(length); return (err: ErrorIsh) => messageWithCauses(err, t);};export type MessageTransformer = (val: string) => string;export const MessageTransformerDefault = (val: string) => val;export const messageWithCausesTruncatedDefault = messageWithCausesTruncated(100);/** * Adapted from https://github.com/voxpelli/pony-cause * */export const getErrorCause = (err: Error | ErrorLike | { cause?: unknown | (() => ErrorIsh | { cause?: unknown | (() => ErrorIsh | any); });}): ErrorIsh | undefined => { if (!err) return;
const cause = err.cause;
// VError / NError style causes if (typeof cause === 'function') { const causeResult = cause();
return isErrorLike(causeResult) ? causeResult : undefined; } else { return isErrorLike(cause) ? cause : undefined; }};