Refactor exceptions

* Suffix names for custom exceptions with "Error"
* Introduce exceptions for when the coressponding encoder isn't found
This commit is contained in:
Ritiek Malhotra
2020-03-17 03:09:56 +05:30
parent 083c430489
commit 29005f24ed
16 changed files with 276 additions and 28 deletions

View File

@@ -1,5 +1,5 @@
class LyricsNotFound(Exception):
class LyricsNotFoundError(Exception):
__module__ = Exception.__module__
def __init__(self, message=None):
super(LyricsNotFound, self).__init__(message)
super(LyricsNotFoundError, self).__init__(message)

View File

@@ -2,7 +2,7 @@ from bs4 import BeautifulSoup
import urllib.request
from spotdl.lyrics.lyric_base import LyricBase
from spotdl.lyrics.exceptions import LyricsNotFound
from spotdl.lyrics.exceptions import LyricsNotFoundError
BASE_URL = "https://genius.com"
@@ -26,7 +26,7 @@ class Genius(LyricBase):
try:
response = urllib.request.urlopen(request, timeout=timeout)
except urllib.request.HTTPError:
raise LyricsNotFound(
raise LyricsNotFoundError(
"Could not find lyrics for {} - {} at URL: {}".format(
self.artist, self.song, url
)
@@ -40,7 +40,7 @@ class Genius(LyricBase):
if lyrics_paragraph:
return lyrics_paragraph.get_text()
else:
raise LyricsNotFound("The lyrics for this track are yet to be released.")
raise LyricsNotFoundError("The lyrics for this track are yet to be released.")
def get_lyrics(self, linesep="\n", timeout=None):
url = self._guess_lyric_url()

View File

@@ -1,7 +1,7 @@
import lyricwikia
from spotdl.lyrics.lyric_base import LyricBase
from spotdl.lyrics.exceptions import LyricsNotFound
from spotdl.lyrics.exceptions import LyricsNotFoundError
class LyricWikia(LyricBase):
@@ -13,6 +13,6 @@ class LyricWikia(LyricBase):
try:
lyrics = lyricwikia.get_lyrics(self.artist, self.song, linesep, timeout)
except lyricwikia.LyricsNotFound as e:
raise LyricsNotFound(e.args[0])
raise LyricsNotFoundError(e.args[0])
else:
return lyrics

View File

@@ -33,5 +33,5 @@ class TestGenius:
raise urllib.request.HTTPError("", "", "", "", "")
monkeypatch.setattr("urllib.request.urlopen", mocked_urlopen)
with pytest.raises(exceptions.LyricsNotFound):
with pytest.raises(exceptions.LyricsNotFoundError):
track.get_lyrics()

View File

@@ -25,11 +25,11 @@ class TestLyricWikia:
def lyricwikia_lyrics_not_found(msg):
raise lyricwikia.LyricsNotFound(msg)
# Wrap `lyricwikia.LyricsNotFound` with `exceptions.LyricsNotFound` error.
# Wrap `lyricwikia.LyricsNotFoundError` with `exceptions.LyricsNotFoundError` error.
monkeypatch.setattr(
"lyricwikia.get_lyrics",
lambda a, b, c, d: lyricwikia_lyrics_not_found("Nope, no lyrics."),
)
track = LyricWikia("Lyricwikia", "Lyricwikia")
with pytest.raises(exceptions.LyricsNotFound):
with pytest.raises(exceptions.LyricsNotFoundError):
track.get_lyrics()