Hugo errors needs to be more specific

Hi there,

I recently come across Hugo error :

Error: error building site: render: failed to render pages: render of "home" failed: "/Users/gora/Documents/hugo-plumber-site/layouts/index.html:278:6": execute of template failed: template: index.html:278:6: executing "main" at <partial "components/card.html" .>: error calling partial: "/Users/gora/Documents/hugo-plumber-site/layouts/partials/components/card.html:15:27": execute of template failed: template: partials/components/card.html:15:27: executing "partials/components/card.html" at <$imagesrc.Resize>: nil pointer evaluating resource.Resource.Resize

After spending hours trying to figure out what’s wrong with the partials/components/card.html It turns out the issue was related to front matter error of a page where I by mistake add url of an image which didnt exist (as I deleted the image).

coverImage: images/agents/83.jpg

having 100’s of pages it become very difficult to check each page’s front matter 1 by 1 to find out what’s different. It would have been very helpful if Hugo can point out that the front matter of this pages is wrong which is responsible for creating error because front matter of this page have an image which doesn’t exist.

Not sure if others agree but this is what I felt after being lost for hours trying to pinpoint the issue.

Thanks

This has happened to other people, too. The solution is to program defensively, I’d raise an error if the image can’t be found before you treat it as a resource.

The error night look cryptic, but at the time it happens, Hugo might not be able to tell you more about it than it does.

The frontmatter is not “wrong” for Hugo. It’s what you do with it that creates the error. That you intend the path to point to an image that you want to display can not be inferred from the frontmatter.

Hi @chrillek

can you please explain what you mean by

is this a Hugo feature or are you just telling me to be careful?

Thanks

https://www.google.com/search?q=defensive%20coding

No ($i can be nil):

{{ $i := .Resources.Get .Params.cover_image }}
{{ $resized := $i.Resize "200x" }}
<img src="{{ $resized.RelPermalink }}" width="{{ $resized.Width }}" height="{{ $resized.Height }}" alt="">

Better:

{{ with .Resources.Get .Params.cover_image }}
  {{ $resized := .Resize "200x" }}
  <img src="{{ $resized.RelPermalink }}" width="{{ $resized.Width }}" height="{{ $resized.Height }}" alt="">
{{ end }}

Best (no var initialization required):

{{ with .Resources.Get .Params.cover_image }}
  {{ with .Resize "200x" }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}
4 Likes

Hi @jmooring

this doesn’t work for me
{{ with .Resources.Get .Params.cover_image }}

I am using multiple image sizes and doing watermark filter like this

{{ $imagesrc := resources.Get .Params.coverImage }}

    <!-- Image size 736 width-->
    {{ $img736 := $imagesrc.Resize "736x552" }} {{ $imgwebp736 :=
    $imagesrc.Resize "736x552 webp" }}
    <!-- Image size 492 width-->
    {{ $img492 := $imagesrc.Resize "492x369" }} {{ $imgwebp492 :=
    $imagesrc.Resize "492x369 webp" }}
    <!-- Image size 392 width-->
    {{ $img392 := $imagesrc.Resize "392x294" }} {{ $imgwebp392 :=
    $imagesrc.Resize "392x294 webp" }}

<!-- Text waterark for image size 736px-->
    {{ $image736 := $img736 | images.Filter (images.Text "©copyright image"
    (dict "color" "#ffffff" "size" 26 "linespacing" 2 "x" 420 "y" 500 ))}} {{
    $imagewebp736 := $imgwebp736 | images.Filter (images.Text
    "©copyright image" (dict "color" "#ffffff" "size" 26 "linespacing" 2
    "x" 420 "y" 500 ))}}
    <!-- Text waterark for image size 492px-->
    {{ $image492 := $img492 | images.Filter (images.Text "©copyright image"
    (dict "color" "#ffffff" "size" 20 "linespacing" 2 "x" 250 "y" 310 ))}} {{
    $imagewebp492 := $imgwebp492 | images.Filter (images.Text
    "©copyright image" (dict "color" "#ffffff" "size" 20 "linespacing" 2
    "x" 250 "y" 310 ))}}

    <!-- Text waterark for image size 392px-->
    {{ $image392 := $img392 | images.Filter (images.Text "©copyright image"
    (dict "color" "#ffffff" "size" 18 "linespacing" 2 "x" 180 "y" 240 ))}} {{
    $imagewebp392 := $imgwebp392 | images.Filter (images.Text
    "©copyright image" (dict "color" "#ffffff" "size" 18 "linespacing" 2
    "x" 180 "y" 240 ))}}
<div class="relative">
      <picture>
        <source
          srcset="{{ $imagewebp392.RelPermalink | absURL }}"
          type="image/webp"
          media="(max-width: 440px)"
          width="392"
          height="294"
        />
        <source
          srcset="{{ $image392.RelPermalink | absURL }}"
          type="{{ $image392.MediaType }}"
          media="(max-width: 440px)"
          width="392"
          height="294"
        />
        <source
          srcset="{{ $imagewebp492.RelPermalink | absURL }}"
          type="image/webp"
          media="(min-width: 441px) and (max-width: 540px)"
          width="492"
          height="369"
        />
        <source
          srcset="{{ $image492.RelPermalink | absURL }}"
          type="{{ $image492.MediaType }}"
          media="(min-width: 441px) and (max-width: 540px)"
          width="492"
          height="369"
        />
        <source
          srcset="{{ $imagewebp736.RelPermalink | absURL }}"
          type="image/webp"
          media="(min-width: 541px) and (max-width: 767px)"
          width="736"
          height="552"
        />
        <source
          srcset="{{ $image736.RelPermalink | absURL }}"
          type="{{ $image736.MediaType }}"
          media="(min-width: 541px) and (max-width: 767px)"
          width="736"
          height="552"
        />
        <source
          srcset="{{ $imagewebp492.RelPermalink | absURL }}"
          type="image/webp"
          edia="(min-width: 768px) and (max-width: 1023px)"
          width="492"
          height="369"
        />
        <source
          srcset="{{ $image492.RelPermalink | absURL }}"
          type="{{ $image492.MediaType }}"
          edia="(min-width: 768px) and (max-width: 1023px)"
          width="492"
          height="369"
        />
        <source
          srcset="{{ $imagewebp392.RelPermalink | absURL }}"
          type="image/webp"
          media="(min-width: 1024px)"
          width="392"
          height="294"
        />
        <source
          srcset="{{ $image392.RelPermalink | absURL }}"
          type="{{ $image392.MediaType }}"
          media="(min-width: 1024px)"
          width="392"
          height="294"
        />
        <img
          class="object-cover aspect-[4/3]"
          src="{{ $image736.RelPermalink }}"
          alt="Cover image for the - {{ .Title }}"
          loading="lazy"
          width="736"
          height="552"
        />
      </picture>
</div>

Thanks

Then wrap the whole thing with a conditional.

2 Likes

Hi @jmooring

Condition such as {{ if .Params.coverImage }} doesn’t solve the error issue as it only check if the page have .Params.coverImage but when there is some mistake in the .Params.coverImage for example:

# instead of 
coverImage: images/correct-image.png

# I enter by mistake
coverImage: images/orrect-image.png #an image that doesn't exist

Hugo points out to layout error instead of pointing out the error is in page front matter.

Thanks

That’s why I provided this example:

Hi @jmooring

with your example

how can I resize and get multiple image format with image filter like this:

can you please provide an example using the

Thanks

Just use if instead of with.

Hi @jmooring

If I apply your suggestion like this:

{{ if .Resources.Get .Params.cover_image }}
{{ $imagesrc := resources.Get .Params.coverImage }}

    <!-- Image size 736 width-->
    {{ $img736 := $imagesrc.Resize "736x552" }} {{ $imgwebp736 :=
    $imagesrc.Resize "736x552 webp" }}
    <!-- Image size 492 width-->
    {{ $img492 := $imagesrc.Resize "492x369" }} {{ $imgwebp492 :=
    $imagesrc.Resize "492x369 webp" }}
    <!-- Image size 392 width-->
    {{ $img392 := $imagesrc.Resize "392x294" }} {{ $imgwebp392 :=
    $imagesrc.Resize "392x294 webp" }}

<!-- Text waterark for image size 736px-->
    {{ $image736 := $img736 | images.Filter (images.Text "©copyright image"
    (dict "color" "#ffffff" "size" 26 "linespacing" 2 "x" 420 "y" 500 ))}} {{
    $imagewebp736 := $imgwebp736 | images.Filter (images.Text
    "©copyright image" (dict "color" "#ffffff" "size" 26 "linespacing" 2
    "x" 420 "y" 500 ))}}
    <!-- Text waterark for image size 492px-->
    {{ $image492 := $img492 | images.Filter (images.Text "©copyright image"
    (dict "color" "#ffffff" "size" 20 "linespacing" 2 "x" 250 "y" 310 ))}} {{
    $imagewebp492 := $imgwebp492 | images.Filter (images.Text
    "©copyright image" (dict "color" "#ffffff" "size" 20 "linespacing" 2
    "x" 250 "y" 310 ))}}

    <!-- Text waterark for image size 392px-->
    {{ $image392 := $img392 | images.Filter (images.Text "©copyright image"
    (dict "color" "#ffffff" "size" 18 "linespacing" 2 "x" 180 "y" 240 ))}} {{
    $imagewebp392 := $imgwebp392 | images.Filter (images.Text
    "©copyright image" (dict "color" "#ffffff" "size" 18 "linespacing" 2
    "x" 180 "y" 240 ))}}
<div class="relative">
      <picture>
        <source
          srcset="{{ $imagewebp736.RelPermalink | absURL }}"
          type="image/webp"
          media="(min-width: 541px) and (max-width: 767px)"
          width="736"
          height="552"
        />
        <source
          srcset="{{ $image736.RelPermalink | absURL }}"
          type="{{ $image736.MediaType }}"
          media="(min-width: 541px) and (max-width: 767px)"
          width="736"
          height="552"
        />
        <source
          srcset="{{ $imagewebp492.RelPermalink | absURL }}"
          type="image/webp"
          edia="(min-width: 768px) and (max-width: 1023px)"
          width="492"
          height="369"
        />
        <source
          srcset="{{ $image492.RelPermalink | absURL }}"
          type="{{ $image492.MediaType }}"
          edia="(min-width: 768px) and (max-width: 1023px)"
          width="492"
          height="369"
        />
        <source
          srcset="{{ $imagewebp392.RelPermalink | absURL }}"
          type="image/webp"
          media="(min-width: 1024px)"
          width="392"
          height="294"
        />
        <source
          srcset="{{ $image392.RelPermalink | absURL }}"
          type="{{ $image392.MediaType }}"
          media="(min-width: 1024px)"
          width="392"
          height="294"
        />
        <img
          class="object-cover aspect-[4/3]"
          src="{{ $image736.RelPermalink }}"
          alt="Cover image for the - {{ .Title }}"
          loading="lazy"
          width="736"
          height="552"
        />
      </picture>
</div>
{{ end}}

then incase there is 1 page with wrong front matter Hugo is not going to generate any image for any card on list page. which again leave me clueless in Hugo to what is the error as with this method it throw no error and simply just doesn’t generate any image.

Thanks

IF PARACHUTE
    JUMP
ELSE
    TELL ME I AM NOT WEARING A PARACHUTE
END
1 Like

Hi @jmooring

it make sense what you say but how you make it work in Hugo? More specifically in the example code I provided, I am not able to understand as in Hugo.

if 5 people are jumping and 1 person have no parachute and Hugo is responsible for parachute Hugo tell them to jump as when using {{ if .Resources.Get .Params.cover_image }} Hugo generate no error, and just generate pages without images. Similarly in parachute example Hugo will tell them to jump with missing parachute and not inform them that the parachute is missing.

Thanks

Print the error message with the name of the missing image. It’s not really rocket science.

And I suggest you try to clean up your code. It looks overly verbose to me for a simple responsive image.

Hugo does tell you that it can’t find the resource – it returns NIL. You’re simply not listening. That’s your problem, and @jmooring explained how to fix it.

1 Like

Hi @chrillek

I am listening very carefully but as I am beginner somethings which sounds like an obvious solution to you makes no sense to me.

Thanks for pointing out the code cleansing I realise I was using the below code twice

<source
          srcset="{{ $imagewebp492.RelPermalink | absURL }}"
          type="image/webp"
          edia="(min-width: 768px) and (max-width: 1023px)"
          width="492"
          height="369"
        />
        <source
          srcset="{{ $image492.RelPermalink | absURL }}"
          type="{{ $image492.MediaType }}"
          edia="(min-width: 768px) and (max-width: 1023px)"
          width="492"
          height="369"
        />
        <source
          srcset="{{ $imagewebp392.RelPermalink | absURL }}"
          type="image/webp"
          media="(min-width: 1024px)"
          width="392"
          height="294"
        />
        <source
          srcset="{{ $image392.RelPermalink | absURL }}"
          type="{{ $image392.MediaType }}"
          media="(min-width: 1024px)"
          width="392"
          height="294"
        />

Then maybe you should be more careful blaming responsibility.
Lookup errorf in the documentation and add it in the else part of the if checking your resource. This topic has been discussed several times here, you’ll find more pointers looking for „defensive“

Hi @chrillek

My complaint is regarding Hugo not pointing to the exact location of error and it points to top level such as card.html while the issue is in the page front matter.

the solution of errorf is not the solution for me, as it is just a custom error message which cannot point out the exact error as Hugo points out to card.html which the issue is in the front matter of a page {{ erroridf "my-custom-error" "You should consider fixing this." }} is as useless as the Hugo error calling partial: "/Users/gora/Documents/hugo-plumber-site/layouts/partials/components/card.html:15:27": execute of template failed: template: partials/components/card.html:15:27: executing "partials/components/card.html" at <$imagesrc.Resize>: nil pointer evaluating resource.Resource.Resize

if Hugo can go one step down to point out that error calling partial fails because the content/plumber/page.md front matter coverImage: is invalid that will help a lot

but no need to get offensive I simply provide the disappointment in Hugo error specificity in the hope that it can be improved in the future releases.

Thanks

How would it even know that there levels down. Your code is at fault, fix it. There’s nothing more to it.

1 Like

Hi @chrillek

An example of the improved code will help a lot if you ever find time to demonstrate how this code can be fixed (while keeping the multiple image size and webp format with fallback and copyright text on image) to avoid errors that will be greatly appreciated.

Thanks

According to chatGPT, Here are some tips for coding defensively in Hugo:

  1. Error Handling:
  • Use error handling techniques in your Go templates and Hugo configurations. Check for errors and handle them gracefully, so your site doesn’t break when unexpected issues arise.
  1. Template Conditionals:
  • Use conditional statements to check for the existence of variables or content before attempting to use them. This helps prevent null or undefined errors. For example:

go

{{ if .Params.author }}
  Author: {{ .Params.author }}
{{ end }}