From 15f475d3217672f9f627d2009d254ab48b02a62e Mon Sep 17 00:00:00 2001 From: RemixDev Date: Mon, 2 Aug 2021 20:38:50 +0200 Subject: [PATCH] Moved all Error types in its own file --- deezer/api.js | 78 ++++++------------------------------ deezer/errors.js | 102 +++++++++++++++++++++++++++++++++++++++++++++++ deezer/gw.js | 12 +----- deezer/index.js | 30 +------------- 4 files changed, 118 insertions(+), 104 deletions(-) create mode 100644 deezer/errors.js diff --git a/deezer/api.js b/deezer/api.js index 292010a..c59f53f 100644 --- a/deezer/api.js +++ b/deezer/api.js @@ -1,4 +1,15 @@ const got = require('got') +const { + APIError, + ItemsLimitExceededException, + PermissionException, + InvalidTokenException, + WrongParameterException, + MissingParameterException, + InvalidQueryException, + DataException, + IndividualAccountChangedNotAllowedException +} = require('./errors.js') // Possible values for order parameter in search const SearchOrder = { @@ -448,72 +459,7 @@ class API{ } } -// Base class for Deezer exceptions -class APIError extends Error { - constructor(message) { - super(message); - this.name = "APIError"; - } -} -class ItemsLimitExceededException extends APIError { - constructor(message) { - super(message); - this.name = "ItemsLimitExceededException"; - } -} -class PermissionException extends APIError { - constructor(message) { - super(message); - this.name = "PermissionException"; - } -} -class InvalidTokenException extends APIError { - constructor(message) { - super(message); - this.name = "InvalidTokenException"; - } -} -class WrongParameterException extends APIError { - constructor(message) { - super(message); - this.name = "WrongParameterException"; - } -} -class MissingParameterException extends APIError { - constructor(message) { - super(message); - this.name = "MissingParameterException"; - } -} -class InvalidQueryException extends APIError { - constructor(message) { - super(message); - this.name = "InvalidQueryException"; - } -} -class DataException extends APIError { - constructor(message) { - super(message); - this.name = "DataException"; - } -} -class IndividualAccountChangedNotAllowedException extends APIError { - constructor(message) { - super(message); - this.name = "IndividualAccountChangedNotAllowedException"; - } -} - module.exports = { SearchOrder, - API, - APIError, - ItemsLimitExceededException, - PermissionException, - InvalidTokenException, - WrongParameterException, - MissingParameterException, - InvalidQueryException, - DataException, - IndividualAccountChangedNotAllowedException + API } diff --git a/deezer/errors.js b/deezer/errors.js new file mode 100644 index 0000000..694f467 --- /dev/null +++ b/deezer/errors.js @@ -0,0 +1,102 @@ +class DeezerError extends Error { + constructor(message) { + super(message) + this.name = "DeezerError" + } +} + +class WrongLicense extends DeezerError { + constructor(format) { + super() + this.name = "WrongLicense" + this.message = `Your account can't request urls for ${format} tracks` + this.format = format + } +} + +class WrongGeolocation extends DeezerError { + constructor(country) { + super() + this.name = "WrongGeolocation" + this.message = `The track you requested can't be streamed in country ${country}` + this.country = country + } +} + +// APIError +class APIError extends DeezerError { + constructor(message) { + super(message); + this.name = "APIError"; + } +} +class ItemsLimitExceededException extends APIError { + constructor(message) { + super(message); + this.name = "ItemsLimitExceededException"; + } +} +class PermissionException extends APIError { + constructor(message) { + super(message); + this.name = "PermissionException"; + } +} +class InvalidTokenException extends APIError { + constructor(message) { + super(message); + this.name = "InvalidTokenException"; + } +} +class WrongParameterException extends APIError { + constructor(message) { + super(message); + this.name = "WrongParameterException"; + } +} +class MissingParameterException extends APIError { + constructor(message) { + super(message); + this.name = "MissingParameterException"; + } +} +class InvalidQueryException extends APIError { + constructor(message) { + super(message); + this.name = "InvalidQueryException"; + } +} +class DataException extends APIError { + constructor(message) { + super(message); + this.name = "DataException"; + } +} +class IndividualAccountChangedNotAllowedException extends APIError { + constructor(message) { + super(message); + this.name = "IndividualAccountChangedNotAllowedException"; + } +} +class GWAPIError extends DeezerError { + constructor(message) { + super(message); + this.name = "GWAPIError"; + } +} + +module.exports = { + DeezerError, + WrongLicense, + WrongGeolocation, + APIError, + ItemsLimitExceededException, + PermissionException, + InvalidTokenException, + WrongParameterException, + MissingParameterException, + InvalidQueryException, + DataException, + IndividualAccountChangedNotAllowedException, + GWAPIError +} diff --git a/deezer/gw.js b/deezer/gw.js index 94e9f08..b73e253 100644 --- a/deezer/gw.js +++ b/deezer/gw.js @@ -1,5 +1,6 @@ const got = require('got') const {map_artist_album, map_user_track, map_user_artist, map_user_album, map_user_playlist} = require('./utils.js') +const { GWAPIError } = require('./errors.js') // Explicit Content Lyrics const LyricsStatus = { @@ -436,18 +437,9 @@ class GW{ } -// Base class for Deezer exceptions -class GWAPIError extends Error { - constructor(message) { - super(message); - this.name = "APIError"; - } -} - module.exports = { LyricsStatus, PlaylistStatus, EMPTY_TRACK_OBJ, - GW, - GWAPIError + GW } diff --git a/deezer/index.js b/deezer/index.js index abf92cf..59af702 100644 --- a/deezer/index.js +++ b/deezer/index.js @@ -2,6 +2,7 @@ const got = require('got') const {CookieJar, Cookie} = require('tough-cookie') const { API } = require('./api.js') const { GW } = require('./gw.js') +const { DeezerError, WrongLicense, WrongGeolocation } = require('./errors.js') // Number associtation for formats const TrackFormats = { @@ -175,38 +176,11 @@ class Deezer{ } } -class DeezerError extends Error { - constructor() { - super() - this.name = "DeezerError" - } -} - -class WrongLicense extends DeezerError { - constructor(format) { - super() - this.name = "WrongLicense" - this.message = `Your account can't request urls for ${format} tracks` - } -} - -class WrongGeolocation extends DeezerError { - constructor(country) { - super() - this.name = "WrongGeolocation" - this.message = `The track you requested can't be streamed in country ${country}` - } -} - module.exports = { TrackFormats, Deezer, api: {...require('./api.js')}, gw: {...require('./gw.js')}, utils: {...require('./utils.js')}, - errors: { - DeezerError, - WrongLicense, - WrongGeolocation - } + errors: {...require('./errors.js')} }