Skip to content

Commit

Permalink
Improve error message for bad seek positions. (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
psobot authored Jul 22, 2024
1 parent 4f5bfa9 commit 9858e95
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions pedalboard/io/ReadableAudioFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -572,15 +572,20 @@ class ReadableAudioFile
const juce::ScopedReadLock scopedReadLock(objectLock);
if (!reader)
throw std::runtime_error("I/O operation on a closed file.");
if (targetPosition >
(reader->lengthInSamples + (lengthCorrection ? *lengthCorrection : 0)))

long long endOfFile =
(reader->lengthInSamples + (lengthCorrection ? *lengthCorrection : 0));

if (targetPosition > endOfFile)
throw std::domain_error(
"Cannot seek beyond end of file (" +
std::to_string(reader->lengthInSamples +
(lengthCorrection ? *lengthCorrection : 0)) +
" frames).");
"Cannot seek to position " + std::to_string(targetPosition) +
" frames, which is beyond end of file (" + std::to_string(endOfFile) +
" frames) by " + std::to_string(endOfFile - targetPosition) +
" frames.");

if (targetPosition < 0)
throw std::domain_error("Cannot seek before start of file.");
throw std::domain_error("Cannot seek before start of file (to position " +
std::to_string(targetPosition) + ").");

// Promote to a write lock as we're now modifying the object:
ScopedTryWriteLock scopedTryWriteLock(objectLock);
Expand Down

0 comments on commit 9858e95

Please sign in to comment.