浏览代码

Convert exceptions for missing MediaSource or MediaAttachment to ResourceNotFoundException with appropriate message.

Andrew Mahone 5 年之前
父节点
当前提交
04a96788f9
共有 1 个文件被更改,包括 20 次插入4 次删除
  1. 20 4
      MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs

+ 20 - 4
MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs

@@ -65,10 +65,26 @@ namespace MediaBrowser.MediaEncoding.Attachments
             }
 
             var mediaSources = await _mediaSourceManager.GetPlayackMediaSources(item, null, true, false, cancellationToken).ConfigureAwait(false);
-            var mediaSource = mediaSources
-                .First(i => string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase));
-            var mediaAttachment = mediaSource.MediaAttachments
-                .First(i => i.Index == attachmentStreamIndex);
+            MediaSourceInfo mediaSource;
+            MediaAttachment mediaAttachment;
+            try
+            {
+                mediaSource = mediaSources
+                    .First(i => string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase));
+            }
+            catch (Exception ex) when (ex is ArgumentNullException || ex is InvalidOperationException)
+            {
+                throw new ResourceNotFoundException($"MediaSource {mediaSourceId} not found");
+            }
+            try
+            {
+                mediaAttachment = mediaSource.MediaAttachments
+                    .First(i => i.Index == attachmentStreamIndex);
+            }
+            catch (Exception ex) when (ex is ArgumentNullException || ex is InvalidOperationException)
+            {
+                throw new ResourceNotFoundException($"MediaSource {mediaSourceId} has no attachment with stream index {attachmentStreamIndex}");
+            }
             var attachmentStream = await GetAttachmentStream(mediaSource, mediaAttachment, cancellationToken)
                     .ConfigureAwait(false);