Skip to content

Commit

Permalink
Standardize exception messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Dec 26, 2023
1 parent 7ef4f8b commit 8896509
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 49 deletions.
7 changes: 4 additions & 3 deletions YoutubeExplode/Channels/ChannelClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ public ChannelClient(HttpClient http)
private Channel Get(ChannelPage channelPage)
{
var channelId =
channelPage.Id ?? throw new YoutubeExplodeException("Could not extract channel ID.");
channelPage.Id
?? throw new YoutubeExplodeException("Failed to extract the channel ID.");

var title =
channelPage.Title
?? throw new YoutubeExplodeException("Could not extract channel title.");
?? throw new YoutubeExplodeException("Failed to extract the channel title.");

var logoUrl =
channelPage.LogoUrl
?? throw new YoutubeExplodeException("Could not extract channel logo URL.");
?? throw new YoutubeExplodeException("Failed to extract the channel logo URL.");

var logoSize =
Regex
Expand Down
23 changes: 12 additions & 11 deletions YoutubeExplode/Playlists/PlaylistClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async ValueTask<Playlist> GetAsync(

var title =
response.Title
?? throw new YoutubeExplodeException("Could not extract playlist title.");
?? throw new YoutubeExplodeException("Failed to extract the playlist title.");

// System playlists have no author
var channelId = response.ChannelId;
Expand All @@ -52,15 +52,16 @@ channelId is not null && channelTitle is not null
.Select(t =>
{
var thumbnailUrl =
t.Url ?? throw new YoutubeExplodeException("Could not extract thumbnail URL.");
t.Url
?? throw new YoutubeExplodeException("Failed to extract the thumbnail URL.");

var thumbnailWidth =
t.Width
?? throw new YoutubeExplodeException("Could not extract thumbnail width.");
?? throw new YoutubeExplodeException("Failed to extract the thumbnail width.");

var thumbnailHeight =
t.Height
?? throw new YoutubeExplodeException("Could not extract thumbnail height.");
?? throw new YoutubeExplodeException("Failed to extract the thumbnail height.");

var thumbnailResolution = new Resolution(thumbnailWidth, thumbnailHeight);

Expand Down Expand Up @@ -100,13 +101,13 @@ public async IAsyncEnumerable<Batch<PlaylistVideo>> GetVideoBatchesAsync(
{
var videoId =
videoData.Id
?? throw new YoutubeExplodeException("Could not extract video ID.");
?? throw new YoutubeExplodeException("Failed to extract the video ID.");

lastVideoId = videoId;

lastVideoIndex =
videoData.Index
?? throw new YoutubeExplodeException("Could not extract video index.");
?? throw new YoutubeExplodeException("Failed to extract the video index.");

// Don't yield the same video twice
if (!encounteredIds.Add(videoId))
Expand All @@ -120,11 +121,11 @@ public async IAsyncEnumerable<Batch<PlaylistVideo>> GetVideoBatchesAsync(

var videoChannelTitle =
videoData.Author
?? throw new YoutubeExplodeException("Could not extract video author.");
?? throw new YoutubeExplodeException("Failed to extract the video author.");

var videoChannelId =
videoData.ChannelId
?? throw new YoutubeExplodeException("Could not extract video channel ID.");
?? throw new YoutubeExplodeException("Failed to extract the video channel ID.");

var videoThumbnails = videoData
.Thumbnails
Expand All @@ -133,19 +134,19 @@ public async IAsyncEnumerable<Batch<PlaylistVideo>> GetVideoBatchesAsync(
var thumbnailUrl =
t.Url
?? throw new YoutubeExplodeException(
"Could not extract thumbnail URL."
"Failed to extract the thumbnail URL."
);

var thumbnailWidth =
t.Width
?? throw new YoutubeExplodeException(
"Could not extract thumbnail width."
"Failed to extract the thumbnail width."
);

var thumbnailHeight =
t.Height
?? throw new YoutubeExplodeException(
"Could not extract thumbnail height."
"Failed to extract the thumbnail height."
);

var thumbnailResolution = new Resolution(thumbnailWidth, thumbnailHeight);
Expand Down
34 changes: 17 additions & 17 deletions YoutubeExplode/Search/SearchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,23 @@ public async IAsyncEnumerable<Batch<ISearchResult>> GetResultBatchesAsync(

var videoId =
videoData.Id
?? throw new YoutubeExplodeException("Could not extract video ID.");
?? throw new YoutubeExplodeException("Failed to extract the video ID.");

// Don't yield the same result twice
if (!encounteredIds.Add(videoId))
continue;

var videoTitle =
videoData.Title
?? throw new YoutubeExplodeException("Could not extract video title.");
?? throw new YoutubeExplodeException("Failed to extract the video title.");

var videoChannelTitle =
videoData.Author
?? throw new YoutubeExplodeException("Could not extract video author.");
?? throw new YoutubeExplodeException("Failed to extract the video author.");

var videoChannelId =
videoData.ChannelId
?? throw new YoutubeExplodeException("Could not extract video channel ID.");
?? throw new YoutubeExplodeException("Failed to extract the video channel ID.");

var videoThumbnails = videoData
.Thumbnails
Expand All @@ -82,19 +82,19 @@ public async IAsyncEnumerable<Batch<ISearchResult>> GetResultBatchesAsync(
var thumbnailUrl =
t.Url
?? throw new YoutubeExplodeException(
"Could not extract video thumbnail URL."
"Failed to extract the video thumbnail URL."
);

var thumbnailWidth =
t.Width
?? throw new YoutubeExplodeException(
"Could not extract video thumbnail width."
"Failed to extract the video thumbnail width."
);

var thumbnailHeight =
t.Height
?? throw new YoutubeExplodeException(
"Could not extract video thumbnail height."
"Failed to extract the video thumbnail height."
);

var thumbnailResolution = new Resolution(thumbnailWidth, thumbnailHeight);
Expand Down Expand Up @@ -126,15 +126,15 @@ public async IAsyncEnumerable<Batch<ISearchResult>> GetResultBatchesAsync(

var playlistId =
playlistData.Id
?? throw new YoutubeExplodeException("Could not extract playlist ID.");
?? throw new YoutubeExplodeException("Failed to extract the playlist ID.");

// Don't yield the same result twice
if (!encounteredIds.Add(playlistId))
continue;

var playlistTitle =
playlistData.Title
?? throw new YoutubeExplodeException("Could not extract playlist title.");
?? throw new YoutubeExplodeException("Failed to extract the playlist title.");

// System playlists have no author
var playlistAuthor =
Expand All @@ -150,19 +150,19 @@ public async IAsyncEnumerable<Batch<ISearchResult>> GetResultBatchesAsync(
var thumbnailUrl =
t.Url
?? throw new YoutubeExplodeException(
"Could not extract playlist thumbnail URL."
"Failed to extract the playlist thumbnail URL."
);

var thumbnailWidth =
t.Width
?? throw new YoutubeExplodeException(
"Could not extract playlist thumbnail width."
"Failed to extract the playlist thumbnail width."
);

var thumbnailHeight =
t.Height
?? throw new YoutubeExplodeException(
"Could not extract playlist thumbnail height."
"Failed to extract the playlist thumbnail height."
);

var thumbnailResolution = new Resolution(thumbnailWidth, thumbnailHeight);
Expand Down Expand Up @@ -192,11 +192,11 @@ public async IAsyncEnumerable<Batch<ISearchResult>> GetResultBatchesAsync(

var channelId =
channelData.Id
?? throw new YoutubeExplodeException("Could not extract channel ID.");
?? throw new YoutubeExplodeException("Failed to extract the channel ID.");

var channelTitle =
channelData.Title
?? throw new YoutubeExplodeException("Could not extract channel title.");
?? throw new YoutubeExplodeException("Failed to extract the channel title.");

var channelThumbnails = channelData
.Thumbnails
Expand All @@ -205,19 +205,19 @@ public async IAsyncEnumerable<Batch<ISearchResult>> GetResultBatchesAsync(
var thumbnailUrl =
t.Url
?? throw new YoutubeExplodeException(
"Could not extract channel thumbnail URL."
"Failed to extract the channel thumbnail URL."
);

var thumbnailWidth =
t.Width
?? throw new YoutubeExplodeException(
"Could not extract channel thumbnail width."
"Failed to extract the channel thumbnail width."
);

var thumbnailHeight =
t.Height
?? throw new YoutubeExplodeException(
"Could not extract channel thumbnail height."
"Failed to extract the channel thumbnail height."
);

var thumbnailResolution = new Resolution(thumbnailWidth, thumbnailHeight);
Expand Down
11 changes: 7 additions & 4 deletions YoutubeExplode/Videos/ClosedCaptions/ClosedCaptionClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ private async IAsyncEnumerable<ClosedCaptionTrackInfo> GetClosedCaptionTrackInfo
foreach (var trackData in playerResponse.ClosedCaptionTracks)
{
var url =
trackData.Url ?? throw new YoutubeExplodeException("Could not extract track URL.");
trackData.Url
?? throw new YoutubeExplodeException("Failed to extract the track URL.");

var languageCode =
trackData.LanguageCode
?? throw new YoutubeExplodeException("Could not extract track language code.");
?? throw new YoutubeExplodeException("Failed to extract the track language code.");

var languageName =
trackData.LanguageName
?? throw new YoutubeExplodeException("Could not extract track language name.");
?? throw new YoutubeExplodeException("Failed to extract the track language name.");

yield return new ClosedCaptionTrackInfo(
url,
Expand Down Expand Up @@ -103,7 +104,9 @@ private async IAsyncEnumerable<ClosedCaption> GetClosedCaptionsAsync(

var partOffset =
partData.Offset
?? throw new YoutubeExplodeException("Could not extract caption part offset.");
?? throw new YoutubeExplodeException(
"Failed to extract the caption part offset."
);

var part = new ClosedCaptionPart(partText, partOffset);

Expand Down
14 changes: 7 additions & 7 deletions YoutubeExplode/Videos/Streams/StreamClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ CancellationToken cancellationToken

return _cipherManifest =
playerSource.CipherManifest
?? throw new YoutubeExplodeException("Could not get cipher manifest.");
?? throw new YoutubeExplodeException("Failed to extract the cipher manifest.");
}

private async IAsyncEnumerable<IStreamInfo> GetStreamInfosAsync(
Expand All @@ -59,11 +59,11 @@ private async IAsyncEnumerable<IStreamInfo> GetStreamInfosAsync(
{
var itag =
streamData.Itag
?? throw new YoutubeExplodeException("Could not extract stream itag.");
?? throw new YoutubeExplodeException("Failed to extract the stream itag.");

var url =
streamData.Url
?? throw new YoutubeExplodeException("Could not extract stream URL.");
?? throw new YoutubeExplodeException("Failed to extract the stream URL.");

// Handle cipher-protected streams
if (!string.IsNullOrWhiteSpace(streamData.Signature))
Expand All @@ -88,11 +88,11 @@ private async IAsyncEnumerable<IStreamInfo> GetStreamInfosAsync(

var container =
streamData.Container?.Pipe(s => new Container(s))
?? throw new YoutubeExplodeException("Could not extract stream container.");
?? throw new YoutubeExplodeException("Failed to extract the stream container.");

var bitrate =
streamData.Bitrate?.Pipe(s => new Bitrate(s))
?? throw new YoutubeExplodeException("Could not extract stream bitrate.");
?? throw new YoutubeExplodeException("Failed to extract the stream bitrate.");

// Muxed or video-only stream
if (!string.IsNullOrWhiteSpace(streamData.VideoCodec))
Expand Down Expand Up @@ -155,7 +155,7 @@ streamData.VideoWidth is not null && streamData.VideoHeight is not null
}
else
{
throw new YoutubeExplodeException("Could not extract stream codec.");
throw new YoutubeExplodeException("Failed to extract the stream codec.");
}
}
}
Expand Down Expand Up @@ -281,7 +281,7 @@ public async ValueTask<string> GetHttpLiveStreamUrlAsync(
if (string.IsNullOrWhiteSpace(playerResponse.HlsManifestUrl))
{
throw new YoutubeExplodeException(
"Could not extract HTTP Live Stream manifest URL. "
"Failed to extract the HTTP Live Stream manifest URL. "
+ $"Video '{videoId}' is likely not a live stream."
);
}
Expand Down
2 changes: 1 addition & 1 deletion YoutubeExplode/Videos/Streams/StreamController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async ValueTask<PlayerSource> GetPlayerSourceAsync(

var version = Regex.Match(iframe, @"player\\?/([0-9a-fA-F]{8})\\?/").Groups[1].Value;
if (string.IsNullOrWhiteSpace(version))
throw new YoutubeExplodeException("Could not extract player version.");
throw new YoutubeExplodeException("Failed to extract the player version.");

return PlayerSource.Parse(
await Http.GetStringAsync(
Expand Down
13 changes: 7 additions & 6 deletions YoutubeExplode/Videos/VideoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,32 @@ public async ValueTask<Video> GetAsync(

var channelTitle =
playerResponse.Author
?? throw new YoutubeExplodeException("Could not extract video author.");
?? throw new YoutubeExplodeException("Failed to extract the video author.");

var channelId =
playerResponse.ChannelId
?? throw new YoutubeExplodeException("Could not extract video channel ID.");
?? throw new YoutubeExplodeException("Failed to extract the video channel ID.");

var uploadDate =
playerResponse.UploadDate
?? watchPage.UploadDate
?? throw new YoutubeExplodeException("Could not extract video upload date.");
?? throw new YoutubeExplodeException("Failed to extract the video upload date.");

var thumbnails = playerResponse
.Thumbnails
.Select(t =>
{
var thumbnailUrl =
t.Url ?? throw new YoutubeExplodeException("Could not extract thumbnail URL.");
t.Url
?? throw new YoutubeExplodeException("Failed to extract the thumbnail URL.");

var thumbnailWidth =
t.Width
?? throw new YoutubeExplodeException("Could not extract thumbnail width.");
?? throw new YoutubeExplodeException("Failed to extract the thumbnail width.");

var thumbnailHeight =
t.Height
?? throw new YoutubeExplodeException("Could not extract thumbnail height.");
?? throw new YoutubeExplodeException("Failed to extract the thumbnail height.");

var thumbnailResolution = new Resolution(thumbnailWidth, thumbnailHeight);

Expand Down

0 comments on commit 8896509

Please sign in to comment.