Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a base exception class #244

Merged
merged 3 commits into from
Jan 12, 2018
Merged

Conversation

emyller
Copy link
Contributor

@emyller emyller commented Jan 11, 2018

This introduces the PydubException base exception class.

It's a very small contribution but makes Pydub-related errors more consistent and a bit easier to handle:

@celery_app.task(bind=True, max_retries=3)
def process_audio(self, audio_src: str) -> bytes:
    """
    Find and convert a WAV audio source into MP3
    """
    try:
        audio_bytes = retrieve_audio_file(audio_src)
        wav_audio = pydub.AudioSegment.from_wav(wav_container)
        mp3_container = io.BytesIO()
        wav_audio.export(
            mp3_container,
            codec='libmp3lame', format='mp3', parameters=['-q:a', '5'],
        )
    
    # The file might be taking a bit longer to show up
    # Or ffmpeg is being flaky (it can happen for mysterious reasons)
    except (FileNotFoundError, PydubException,) as err:
        self.retry(exc=err)
    
    else:
        return mp3.read()

...And also fix some PEP8 complaints.

@jiaaro jiaaro merged commit 0d8d594 into jiaaro:master Jan 12, 2018
@emyller emyller deleted the emyller/base-exception branch January 13, 2018 01:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants