How to halt script execution with errorf

I am writing a partial to process images.

I am trying to assign .variables to $variables if they meet criteria.

The following example shows how the last line in the script is executed even though an error is thrown

{{ $num1 := 10 }}
{{ $num2 := 11 }}
{{ $whole := 0 }}
{{ $div := 0 }}

{{ if le $num2 $num1 }}
{{ $whole = $num1 }}
{{ $div = $num2 }}
{{ else }}
{{ errorf “Cannot perform operation as %v is greater than %v” $num2 $num1 }}
{{ end }}

{{ div (float $whole) (float $div) }}

ERROR 2021/09/07 12:54:44 Failed to render pages: render of “home” failed: “C:\GitHub\powerfulweb-test\hyas-sept\layouts\index.html:29:3”: execute of template failed: template: index.html:29:3: executing “main” at <div (float $whole) (float $div)>: error calling div: can’t divide the value by 0
Total in 44 ms

If I remove the final line, the actual error is logged

{{ $num1 := 10 }}
{{ $num2 := 11 }}
{{ $whole := 0 }}
{{ $div := 0 }}

{{ if le $num2 $num1 }}
{{ $whole = $num1 }}
{{ $div = $num2 }}
{{ else }}
{{ errorf “Cannot perform operation as %v is greater than %v” $num2 $num1 }}
{{ end }}

{{/* div (float $whole) (float $div) */}}

ERROR 2021/09/07 12:59:09 Cannot perform operation as 11 is greater than 10
ERROR 2021/09/07 12:59:09 Rebuild failed:
ERROR 2021/09/07 12:59:09 Logged 1 error(s)
Total in 49 ms

Is this the expected behaviour or errorf ?
I am trying to catch errors before hugo throws an error. Obviously I could remove the $whole and $div variables to remove the condition where 0 is divided by 0, but I need to be able to modify parameters which are passed into a partial, before the image processing takes place.

If you need to see the whole script, It will be ready in a few days.

I am trying to avoid this example below which is not very dry. I have had to do a 2nd check before the operation takes place.

Any advice/help is appreciated.

{{ $num1 := 10 }}
{{ $num2 := 11 }}
{{ $whole := 0 }}
{{ $div := 0 }}

{{ if le $num2 $num1 }}
{{ $whole = $num1 }}
{{ $div = $num2 }}
{{ else }}
{{ errorf “Cannot perform operation as %v is greater than %v” $num2 $num1 }}
{{ end }}

{{ if and $whole $div}}
{{ div (float $whole) (float $div) }}
{{ end }}

ERROR 2021/09/07 13:03:56 Cannot perform operation as 11 is greater than 10
ERROR 2021/09/07 13:03:56 Rebuild failed:

ERROR 2021/09/07 13:03:56 Logged 1 error(s)
Total in 47 ms

If I try to run your code with the div line, I get both the errorf output and the div by 0 error from Hugo.

Why not

{{ $num1 := 10 }}
{{ $num2 := 11 }}
{{ $whole := 0 }}
{{ $div := 0 }}

{{ if le $num2 $num1 }}
  {{ $whole = $num1 }}
  {{ $div = $num2 }}
  {{ div (float $whole) (float $div) }}
{{ else }}
  {{ errorf "Cannot perform operation as %v is greater than %v" $num2 $num1 }}
{{ end }}

PS: Please use code blocks next time (three backticks ``` or the </> button in the text editor). Makes it easier to read chunks of code.

Thanks for that, ill see if I can make some changes to the script and ill post it here