Skip to content

Commit

Permalink
Display a warning if XML parsing error is encountered
Browse files Browse the repository at this point in the history
ref #75
  • Loading branch information
dougwaldron committed Oct 1, 2024
1 parent ef6f3e4 commit 6f31351
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
21 changes: 17 additions & 4 deletions PodcastRewind/Pages/Setup.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,25 @@
<div class="d-flex flex-row justify-content-start align-items-center mb-3 bg-secondary bg-opacity-10 p-2 rounded">
@if (!string.IsNullOrEmpty(Model.PodcastImageUrl))
{
<img class="img-fluid rounded me-2" src="@Model.PodcastImageUrl" height="100" width="100" alt="" />
<img class="img-fluid rounded" src="@Model.PodcastImageUrl" height="100" width="100" alt="" />
}
<h2>@Model.PodcastTitle</h2>
<h2 class="m-2">@Model.PodcastTitle</h2>
</div>

@if (Model.PodcastEpisodes.Count == 0)
@if (Model.XmlParsingError)
{
<div class="alert alert-warning" role="alert">
Whoops! There's a
<a class="alert-link" href="https://github.com/dougwaldron/podcast-rewind/issues/75">
bug in Microsoft's feed parsing library
</a>
that prevents some feeds from working, including this one. Unfortunately, there's nothing I can do about it
until Microsoft fixes it. I've already sent them
<a class="alert-link" href="https://github.com/dotnet/runtime/pull/99194">code that fixes the issue</a>.
If you want, you can go there and express your support.
</div>
}
else if (Model.PodcastEpisodes.Count == 0)
{
<p>That podcast feed doesn't seem to have any episodes!</p>
}
Expand Down Expand Up @@ -77,5 +90,5 @@ else

@section Scripts
{
<partial name="_ValidationScriptsPartial" />
<partial name="_ValidationScriptsPartial" />
}
31 changes: 19 additions & 12 deletions PodcastRewind/Pages/Setup.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using PodcastRewind.Services;
using System.ServiceModel.Syndication;
using PodcastRewind.Models.Dto;
using PodcastRewind.Services;
using System.Xml;

namespace PodcastRewind.Pages;

Expand All @@ -15,6 +15,7 @@ public class SetupModel(IFeedRewindInfoRepository repository, ISyndicationFeedSe
public string PodcastTitle { get; private set; } = string.Empty;
public string PodcastImageUrl { get; private set; } = string.Empty;
public List<ViewPodcastEpisodeDto> PodcastEpisodes { get; private set; } = [];
public bool XmlParsingError { get; private set; }

public async Task<IActionResult> OnGetAsync(string? feedUrl, int interval = 7)
{
Expand All @@ -24,7 +25,18 @@ public async Task<IActionResult> OnGetAsync(string? feedUrl, int interval = 7)
var feed = await feedService.GetSyndicationFeedAsync(feedUrl);
if (feed is null) return NotFound();

LoadData(feed);
PodcastTitle = feed.Title.Text;
PodcastImageUrl = feed.ImageUrl?.ToString() ?? "";
try
{
PodcastEpisodes = feed.Items.Select(item => new ViewPodcastEpisodeDto(item))
.OrderBy(dto => dto.PublishDate).ToList();
}
catch (XmlException e)

Check warning on line 35 in PodcastRewind/Pages/Setup.cshtml.cs

View workflow job for this annotation

GitHub Actions / Analyze with SonarCloud

The variable 'e' is declared but never used

Check warning on line 35 in PodcastRewind/Pages/Setup.cshtml.cs

View workflow job for this annotation

GitHub Actions / Analyze with SonarCloud

The variable 'e' is declared but never used
{
XmlParsingError = true;
return Page();
}

if (PodcastEpisodes.Count > 0)
{
Expand All @@ -46,19 +58,14 @@ public async Task<IActionResult> OnPostAsync()
var feed = await feedService.GetSyndicationFeedAsync(CreateFeedRewindInfo.FeedUrl);
if (feed is null) return NotFound();

LoadData(feed);
PodcastTitle = feed.Title.Text;
PodcastEpisodes = feed.Items.Select(item => new ViewPodcastEpisodeDto(item))
.OrderBy(dto => dto.PublishDate).ToList();
PodcastImageUrl = feed.ImageUrl?.ToString() ?? "";
return Page();
}

var id = await repository.SaveAsync(CreateFeedRewindInfo);
return RedirectToPage("Details", new { id });
}

private void LoadData(SyndicationFeed feed)
{
PodcastTitle = feed.Title.Text;
PodcastEpisodes = feed.Items.Select(item => new ViewPodcastEpisodeDto(item))
.OrderBy(dto => dto.PublishDate).ToList();
PodcastImageUrl = feed.ImageUrl?.ToString() ?? "";
}
}

0 comments on commit 6f31351

Please sign in to comment.