Updating from Err to TryValue?

It looks like a recent Hugo update created an incompatibility for a shortcode that jmooring was extraordinarily helpful in guiding me through in an October 2024 thread, “Custom shortcode to display the date when a remote rss feed was last updated (e.g. Substack, Mastodon)”.

The relevant error that I’m seeing when I try to build my website with Hugo:

execute of template failed: template: _shortcodes/feed-updated.html:3:12: executing “_shortcodes/feed-updated.html” at <.Err>: can’t evaluate field Err in type resource.Resource: Resource.Err was removed in Hugo v0.141.0 and replaced with a new try keyword, see try

I’ve reviewed the documentation, as well as the only forum thread I’ve found that seems to address this issue (“Unexpected breakage in 0.141.0“), and I’m still confused about what changes I need to make in the shortcode that I’ve been using:

{{ with .Get "url" }}
  {{ with resources.GetRemote . }}
    {{ with .Err }}
      {{ warnf "%s" . }}
    {{ else }}
      {{ with .Content | transform.Unmarshal }}
        {{ with .channel.item }}
          {{ range first 1 . }}
            {{ with .pubDate }}
              {{ dateFormat "2006-01-02" . }}
            {{ end }}
          {{ end }}
        {{ end }}
      {{ end }}
    {{ end }}
  {{ end }}
{{ end }}

See if this works by updating the second line. :

{{ with try (resources.GetRemote .) }}

Thank you for responding so quickly. Unfortunately, the Hugo build failed:

execute of template failed: template: _shortcodes/feed-updated.html:6:14: executing "_shortcodes/feed-updated.html" at <.Content>: can't evaluate field Content in type template.TryValue

See if allocating the url value to a variable works :

{{ with .Get "url" }}
  {{ $tempurl := . }}
    {{ with try (resources.GetRemote $tempurl) }}
1 Like

I suggest we stop guessing and instead look at the documentation:
https://gohugo.io/functions/go-template/try/#example

Your shortcode, with some additional error checking:

{{ with $url := .Get "url" }}
  {{ with try (resources.GetRemote .) }}
    {{ with .Err }}
      {{ errorf "The %q shortcode was unable to get the remote resource %s: %s: see %s" $.Name $url . $.Position }}
    {{ else with .Value }}
      {{ with . | transform.Unmarshal }}
        {{ with .channel.item }}
          {{ range first 1 . }}
            {{ with .pubDate }}
              {{ dateFormat "2006-01-02" . }}
            {{ end }}
          {{ end }}
        {{ end }}
      {{ end }}
    {{ else }}
      {{ errorf "The %q shortcode was unable to get the remote resource %s: see %s" $.Name $url $.Position }}
    {{ end }}
  {{ end }}
{{ else }}
  {{ errorf "The %q shortcode requires a argument named url: see %s" .Name .Position }}
{{ end }}

Thank you! Success!

You’re welcome. I just edited the above to improve performance, passing the resource to transform.Unmarshal instead of passing the resource content.

It looks like the latest updates created a different error:

execute of template failed: template: _shortcodes/feed-updated.html:6:27: executing "_shortcodes/feed-updated.html" at <transform.Unmarshal>: error calling Unmarshal: MIME "application/rss+xml" not supported

I also have an etiquette question: Should I go back to original thread and add a note to look in this thread for updated code? I would hate for someone to view the thread from last year, try the code, and have to figure out why it doesn’t work for them.

What is the failing URL?
Which hugo version?
Which OS?
Can you try this locally?

git clone --single-branch -b hugo-forum-topic-55568 https://github.com/jmooring/hugo-testing hugo-forum-topic-55568
cd hugo-forum-topic-55568
hugo server

Or share your repository?

In the meantime, switch back to passing .Content to transform.Unmarshal.

That would be great.

Failing URL: https://disappearingmoment.com/roll/

Hugo version: hugo v0.148.2+extended+withdeploy darwin/arm64 BuildDate=2025-07-27T12:43:24Z VendorInfo=brew

OS: 15.5 (24F74) (I just noticed that I need to update my OS. I’ll do it after I hit Reply)

My repository (updated link): https://git.sr.ht/\~bonfield/disappearingmoment/tree

I tried to update the forum thread that we’ve referenced elsewhere in this thread. It’s closed and I’m not able to add to the thread or edit my posts.

That URL is an HTML file.

Sorry, I updated the link and tested it in a different browser that wasn’t logged in to Sourcehut. It should work properly now.

You’ve defined these media types in your site config:

[mediaTypes."application/atom+xml"]
suffixes = ["xml"]

[mediaTypes."application/rss+xml"]
suffixes = ["rss"]

[mediaTypes."application/xml"]
suffixes = ["rss"]

Do you remember what the last one is for? If I remove it, or add “xml” to the suffix array, it works.

And to figure which URLs are causing the problem, you can wrap the transform.Unmarshal bit in another try statement, e.g.

{{ with $url := .Get "url" }}
  {{ with try (resources.GetRemote .) }}
    {{ with .Err }}
      {{ errorf "The %q shortcode was unable to get the remote resource %s: %s: see %s" $.Name $url . $.Position }}
    {{ else with .Value }}
      {{ with try (. | transform.Unmarshal) }}
        {{ with .Err }}
          {{ errorf "The %q shortcode was unable to unmarshal the remote resource %s: %s: see %s" $.Name $url . $.Position }}
        {{ else with .Value }}
          {{ with .channel.item }}
            {{ range first 1 . }}
              {{ with .pubDate }}
                {{ dateFormat "2006-01-02" . }}
              {{ end }}
            {{ end }}
          {{ end }}
        {{ end }}
      {{ end }}
    {{ else }}
      {{ errorf "The %q shortcode was unable to get the remote resource %s: see %s" $.Name $url $.Position }}
    {{ end }}
  {{ end }}
{{ else }}
  {{ errorf "The %q shortcode requires a argument named url: see %s" .Name .Position }}
{{ end }}

Or don’t worry about it and, as I suggested earlier, go back to passing .Content to transform.Unmarshal and don’t worry about the performance hit.

1 Like

I used this latest code, and also deleted the two lines that refer to RSS and it works well. Thank you!

After poking around in my code, my memory, my bookmarks, and my notes to myself, I think the RSS references are legacy code. I switched to Atom and left them in the file in case the code that I used to change to Atom didn’t work properly and I needed to roll back my updates (this was before I was using Git to manage my website).

I remain deeply impressed with your dedication to Hugo and its users. I can’t imagine publishing my writing with a different platform. Again, apologies for not being able to update the previous thread. I hope that anyone who’s using this code finds this thread and makes the necessary updates.

2 Likes

Thank you for your kind words. Much appreciated.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.