From 4a1d65552312868c11756b5f96bbbb2ba3e29481 Mon Sep 17 00:00:00 2001 From: Roberto Tonino Date: Thu, 13 May 2021 21:14:35 +0200 Subject: [PATCH] chore(server): moved errors to dedicated file --- server/src/helpers/errors.ts | 25 +++++++++++++++++++++++++ server/src/main.ts | 30 ++++-------------------------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/server/src/helpers/errors.ts b/server/src/helpers/errors.ts index 39ab13d..5c98c05 100644 --- a/server/src/helpers/errors.ts +++ b/server/src/helpers/errors.ts @@ -13,3 +13,28 @@ export class BadRequestError extends Error { } export const isBadRequestError = (error: any) => error instanceof BadRequestError + +export class QueueError extends Error { + constructor(message: string) { + super(message) + this.name = 'QueueError' + } +} + +export class AlreadyInQueue extends QueueError { + item: any + silent: boolean + constructor(dwObj: any, silent: boolean) { + super(`${dwObj.artist} - ${dwObj.title} is already in queue.`) + this.name = 'AlreadyInQueue' + this.item = dwObj + this.silent = silent + } +} + +export class NotLoggedIn extends QueueError { + constructor() { + super(`You must be logged in to start a download.`) + this.name = 'NotLoggedIn' + } +} diff --git a/server/src/main.ts b/server/src/main.ts index 4103378..fac5864 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -4,10 +4,12 @@ import { sep } from 'path' import deemix from 'deemix' import WebSocket from 'ws' import { wss } from './app' +import { Settings } from './types' +import { AlreadyInQueue, NotLoggedIn } from './helpers/errors' const Downloader = deemix.downloader.Downloader const { Single, Collection, Convertable } = deemix.types.downloadObjects -export const defaultSettings: any = deemix.settings.DEFAULTS +export const defaultSettings: Settings = deemix.settings.DEFAULTS export const configFolder: string = deemix.utils.localpaths.getConfigFolder() export let settings: any = deemix.settings.load(configFolder) export const sessionDZ: any = {} @@ -38,6 +40,7 @@ export async function addToQueue(dz: any, url: string, bitrate: number) { if (!dz.logged_in) throw new NotLoggedIn() console.log(`Adding ${url} to queue`) let downloadObjs = await deemix.generateDownloadObject(dz, url, bitrate, deemixPlugins, listener) + console.log({ downloadObjs }) const isSingleObject = !Array.isArray(downloadObjs) console.log(downloadObjs) @@ -94,28 +97,3 @@ async function startQueue(dz: any): Promise { currentJob = null } while (queueOrder.length) } - -class QueueError extends Error { - constructor(message: string) { - super(message) - this.name = 'QueueError' - } -} - -class AlreadyInQueue extends QueueError { - item: any - silent: boolean - constructor(dwObj: any, silent: boolean) { - super(`${dwObj.artist} - ${dwObj.title} is already in queue.`) - this.name = 'AlreadyInQueue' - this.item = dwObj - this.silent = silent - } -} - -class NotLoggedIn extends QueueError { - constructor() { - super(`You must be logged in to start a download.`) - this.name = 'NotLoggedIn' - } -}