Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log spam caused by try #13320

Closed
akbyrd opened this issue Jan 30, 2025 · 4 comments · Fixed by #13323
Closed

Log spam caused by try #13320

akbyrd opened this issue Jan 30, 2025 · 4 comments · Fixed by #13323

Comments

@akbyrd
Copy link

akbyrd commented Jan 30, 2025

I think try might have some debug logging enabled. When I use {{ $width := (try $resource.Width) }} I get a bunch of logging of what looks like the SubType of the resource

Start building sites …

x-icon
x-icon    
css       
css       
x-icon    
css       
x-icon    
css       
x-icon    
css       
x-icon    
css       
x-icon    
css       
javascript
javascript
javascript
javascript
javascript
javascript
javascript
x-icon
x-icon
css
css
...

What version of Hugo are you using (hugo version)?

hugo v0.142.0-1f746a872442e66b6afd47c8c04ac42dc92cdb6f+extended windows/amd64 BuildDate=2025-01-22T12:20:52Z VendorInfo=gohugoio

Does this issue reproduce with the latest release?

Yes

@jmooring
Copy link
Member

jmooring commented Jan 30, 2025

{{ $width := (try $resource.Width) }}

That construct is eating the error. See https://gohugo.io/functions/go-template/try/. Do this instead:

{{ with try $resource.Width }}
  {{ with .Err }}
    {{ errorf "%s" . }}
  {{ else }}
    {{ fmt.Printf "The width is %v" .Value }}
  {{ end }}
{{ end }}

Directly above the error you will see the media subtype. We'll remove that.

If you want to ignore the error, perhaps while ranging over a collection of resources:

{{ range resources.Match "*" }}
  {{ with try .Width }}
    {{ fmt.Printf "The width is %v" .Value }}
  {{ end }}
{{ end }}

jmooring added a commit to jmooring/hugo that referenced this issue Jan 30, 2025
@akbyrd
Copy link
Author

akbyrd commented Jan 30, 2025

Thanks for the tip. I minimized it for the sake of example. My actual use case is setting a value in a dictionary where I'm ok with it being null:

{{ $ret = dict
	"Resource"     $resource
	"Integrity"    $integrity
	"Permalink"    $resource.Permalink
	"RelPermalink" $resource.RelPermalink
	"Width"        (try $resource.Width).Value
	"Height"       (try $resource.Height).Value
}}
{{ return $ret }}

I think this is ok? The user of the partial is expected to deal with the potentially-nil value

@jmooring
Copy link
Member

this is ok?

Yes.

jmooring added a commit to jmooring/hugo that referenced this issue Jan 30, 2025
bep pushed a commit that referenced this issue Jan 30, 2025
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants