Making error message clearer

I received this error today on my site

ERROR 2022/12/10 12:03:03 Rebuild failed: "C:\steppa\site\content\test\index.md:1:1": "C:\steppa\site\themes\hugo-theme\layouts\_default\_markup\render-image.html:2:21": execute of template failed at <$img.Resize>: nil pointer evaluating resource.Resource.Resize'

It was caused by a back slash in an image link ![image](/image/image.jpg) which should have been ![image](image/image.jpg) in the page bundle. How can this error be made more clearer? It took me a few hours to notice the issue causing it.

The first part of my image render hook look like this

{{ $img := .Page.Resources.GetMatch (printf "%s" (.Destination | safeURL)) }}
{{ $minwebp := $img.Resize "540x webp" }}
{{ $midwebp := $img.Resize "800x webp" }}
{{- $alt := .PlainText | safeHTML -}}
{{- $caption := "" -}}
{{- with .Title -}}
{{ $caption = . | safeHTML -}}
{{ end }}

With a multi-author site, it will be easier to diagnose where the issue is if, for example, the error message points out the image path is incorrect.

You should be coding defensively.

A simplified example (ignores alt and title attributes):

{{ $r := "" }}
{{ with .Page.Resources.Get .Destination }}
  {{ $r = . }}
{{ else }}
  {{ errorf "The image render hook was unable to get image %q." .Destination  }}
{{ end }}

{{ with $r }}
  {{ with .Fill "400x200 webp q50"  }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}

If the image isn’t present, processing stops, and you will never get to the image processing bits.

2 Likes

I am using <figure> with srcset. How can that factor in your solution? I want to retain the image sizes.

Do the existence check before you get to the .Resize bit.

I don’t know, I think the error message is already pretty good. The error message is pointing you to the line and column where the error happens. If you edit e.g. Visual Code, clicking on the error will take you to that line.

But i guess the error message “nil pointer …” (which I think comes from the Go std library) is a little foreign to many and could be elaborated on.

I need this part in, thus I will put your solution on hold because integrating that part throws errors and stops the build.
My markdown images are in this format ![image here](img/image.jpg "image title") with image here as the $alt and image title as the $caption.
But your answer works with the caption part excluded.

That will ease everything. I realized today the preceding \ points to the base URL and not the file path in page bundles.

It’s just an example of coding defensively, not a cut and paste solution.

$.Title and $.PlainText solves it. That global context is a lifesaver. But that means everything else needs to be wrapped in the $, for example, the .Ordinal bit which I found in the release notes.

A nil pointer exception is in this context is perhaps related to the image not being found. A typo in its name or something like that. So check for the existence of the Resource before using it.

I have seen that error a few times, but the language is not beginner-friendly. Search the forum for similar questions and there are several. Making it more clearer is better.

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