Some debugging advice, please

This morning I tried to rebuild a site I had not touched in a month or so. To my surprise it would not build, I got this: ERROR 2017/08/07 19:24:42 Failed to render “theme/_default/single.html”: reflect: call of reflect.Value.Int on string Value
ERROR 2017/08/07 19:24:42 Failed to render “index.html”: reflect: call of reflect.Value.Int on string Value

It turned out after a lot of clumsy flailing about on my part to be a line of code in trial and error in …layouts/partials/menu.html, this: {{ if isset $page.Site.Sections $key }}
just a few lines after {{ $page := . }}.

Now, I haven’t figured out why this code that was working a while back has quit working. I don’t understand the issue well enough to figure out what .Site.Sections means, where it’s initialized, or how to interrogate it.

Sorry if this is a stupid question, but I feel rather stupid.

What I’m asking isn’t “what’s wrong with the code?” as much ask “what are the steps one takes to effectively debug any such code?” I hope there’s a Fine Manual that I am negligent for not reading. If so, please point me to it.

If you haven’t found it, Hugo has extensive documentation.

As for your issue, it would be much easier to debug yer issue if you put the source of your site someone online and point us to it. :slight_smile:

When I debug, I normally print whatever object I’m playing with and see what they are.

Print {{ $page }} and see the result.

The next step is to see what methods, variables and functions are available for the object you’re using. For example, if $page is in fact a page, I don’t think it will have a . Site method.

I’m assuming this issue is related to upgrading Hugo versions.

First, see an earlier reply I made to a separate thread about reading the release notes.

I think the root cause here is that .Site.Sections changed a few versions back so that it returns a slice (think “array”) of Page objects. If you pass a slice to isset, isset expects to receive an integer index and will tell you if that index is defined.

Happily, print {{ $page }} proved helpful in seeing more, but I’m enough of a dullard as to not know how to inventory this object’s methods, variables, and functions. If I’m running a debugger, I can generally set a breakpoint, then examine the object at my leisure. I’m clumsy enough that I rely upon a debugger to figure out what the class of an object is. (Unless I’m working with my own deathless prose, in which case I know everything intimately and nothing ever fails.) <-- if you believe that, i have swampland in Florida for sale.

Any “this is where the ground is” hints at setting up a debugger would definitely be appreciated. I’m debugging with old-school print statements and that is slow going.

Your comment that .Site.Sections has changed to a slice was spot-on and it is truly the source of my unhappiness. 10^6 Thanks!

There is no debugger, sorry. I generally use <pre>{{ print "%#v" $var }}</pre> if I’m unsure of what a variable contains.

You can combine that with this tip @gaetawoo gave here: To debug which layout is being used, make a config param to check

1 Like

Thank you all. i have a much better feel for what i must do and how to fix

1 Like