From cb04713aec0665562701fb7b24f2bb713643b3f4 Mon Sep 17 00:00:00 2001 From: afterlifepro Date: Fri, 25 Jul 2025 12:22:45 +0100 Subject: [PATCH] Make SpotifyError nicer to work with and move "throws" to /utils --- src/components/playing/spotify/access.ts | 3 +- src/components/playing/spotify/api.ts | 4 +- src/components/playing/spotify/errors.ts | 55 ++++++++++++++++++------ src/utils.ts | 12 +++++- 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/src/components/playing/spotify/access.ts b/src/components/playing/spotify/access.ts index a39fe24..22c942d 100644 --- a/src/components/playing/spotify/access.ts +++ b/src/components/playing/spotify/access.ts @@ -4,7 +4,8 @@ import { SPOTIFY_CLIENT_SECRET, SPOTIFY_REDIRECT_URI, } from "astro:env/server"; -import { SpotifyError, throws } from "./errors"; +import { SpotifyError } from "./errors"; +import { throws } from "/utils"; import { isAuthToken, isRefreshToken } from "./types"; /** diff --git a/src/components/playing/spotify/api.ts b/src/components/playing/spotify/api.ts index 7f3a0d4..1501bba 100644 --- a/src/components/playing/spotify/api.ts +++ b/src/components/playing/spotify/api.ts @@ -1,7 +1,7 @@ import getAccessCode from "./access"; -import { SpotifyError, throws } from "./errors"; +import { SpotifyError } from "./errors"; import { isNowPlaying, type nowPlaying } from "./types"; -import { isObj } from "/utils"; +import { isObj, throws } from "/utils"; /** * Wrapper for authorizing a spotify API with default headers etc diff --git a/src/components/playing/spotify/errors.ts b/src/components/playing/spotify/errors.ts index ab7bbfd..f7a8f0d 100644 --- a/src/components/playing/spotify/errors.ts +++ b/src/components/playing/spotify/errors.ts @@ -1,23 +1,52 @@ +import { isObj } from "/utils"; + +export const spotErrs = [ + "NO_AUTH", + "INVALID_AUTH_RES", + "UNHANDLED_API_ERR", + "RATE_LIMITED", + "NO_CONTENT", + "MALFORMED_SPOTIFY_RES", +] as const; + export class SpotifyError { - code: "NO_AUTH" | "INVALID_AUTH_RES" | "UNHANDLED_API_ERR" | "RATE_LIMITED" | "NO_CONTENT" | "MALFORMED_SPOTIFY_RES"; + code: typeof spotErrs[number]; details: unknown; human: string; + constructor(code: SpotifyError["code"], details: unknown, human: string); + constructor(error: SpotifyErrorInit); constructor( - code: SpotifyError["code"], - details: unknown, - human: string = code, + code: SpotifyError["code"] | SpotifyErrorInit, + details?: unknown, + human?: string, ) { - this.code = code; - this.details = details; - this.human = human; + if (typeof code === "string") { + this.code = code; + this.details = details; + this.human = human ?? code; + return; + } + + this.code = code.code; + this.details = code.details; + this.human = code.human ?? code.code; } } -/** - * Throw passed value (for ternaries/etc) - * @param val value to throw - */ -export function throws(val: unknown): never { - throw val; +export interface SpotifyErrorInit { + code: SpotifyError["code"]; + details: unknown; + human?: string; } + +export const isSpotifyError = ( + err: unknown, +): err is SpotifyError | SpotifyErrorInit => + isObj(err) && + "code" in err && + typeof err.code === "string" && + spotErrs.reduce((acc, cur) => acc || cur === err.code, false) && + "details" in err && + ("human" in err ? typeof err.human === "string" : true); + diff --git a/src/utils.ts b/src/utils.ts index 8fde61c..6564cd4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,4 @@ -/** +/** * via: https://www.totaltypescript.com/concepts/the-prettify-helper */ export type Prettify = { @@ -7,4 +7,12 @@ export type Prettify = { export function isObj(obj: unknown): obj is object { return typeof obj === "object" && obj !== null; -} \ No newline at end of file +} + +/** + * Throw passed value (for ternaries/etc) + * @param val value to throw + */ +export function throws(val: unknown): never { + throw val; +} -- 2.51.2