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

Add errorf template func #3817

Closed
jetwash opened this issue Aug 19, 2017 · 6 comments · Fixed by #3927
Closed

Add errorf template func #3817

jetwash opened this issue Aug 19, 2017 · 6 comments · Fixed by #3927

Comments

@jetwash
Copy link

jetwash commented Aug 19, 2017

I'm working on a heavily data-driven hugo site; virtually all the content is in front matter, and it's all relatively structured. As a result I tend to have a lot of templates, and they're more complicated than a blog or something where you mostly just need {{ .Content }}.

One thing I've struggled a bit with is making sure the input data is formatted correctly. I'd like to add a bit of logic to my templates to verify that required properties exist and, if not, emit an error message. I was thinking something like errorf, which would be just like printf but output to the console, and cause the build to fail. That way I could do something like

{{ if not .Params.foo }}{{ errorf "foo parameter missing for %s" .URL }}{{ end }}

Without this, sometimes Hugo will crash (e.g., if I try to dereference a null pointer) or emit an error message (e.g., passing an unset value to jsonify results in an error about an incorrect number of arguments), but often Hugo will just quietly keep going and I end up with bad data.

I know doing this in the template isn't really ideal, but the only other thing I can think of would be validating against a schema, but I'm not aware of anything for markdown (which is the front matter format I'm using)… Maybe a way to automatically convert front matter to JSON then validate it against a JSON schema?

@bep bep added this to the v0.27 milestone Aug 19, 2017
@bep
Copy link
Member

bep commented Aug 19, 2017

I think an errorf func would be a great addition.

/cc @moorereason

@bep bep changed the title error reporting function for templates Add errorf template func Aug 19, 2017
@digitalcraftsman
Copy link
Member

digitalcraftsman commented Aug 19, 2017

Should errorf print to stdout like the regular log infos or to stderr?

@bep
Copy link
Member

bep commented Aug 19, 2017

Should errorf to stdout like the regular log infos or to stderr?

It should log to the defined ERROR logger.

@bep bep modified the milestones: v0.27, v0.28 Sep 7, 2017
@bep bep modified the milestones: v0.28, v0.29 Sep 21, 2017
@bep bep modified the milestones: v0.29, v0.30 Sep 26, 2017
@bep
Copy link
Member

bep commented Sep 29, 2017

@bmon no panics/exits here. What you need to is just to log to ns.Deps.Logger.ERROR (or something).

We have some log counting magic going on behind the scenes, so any ERROR logging will exit -1 at the end.

@bep
Copy link
Member

bep commented Sep 29, 2017

@bmon thinking a little about this, template errors can be a bit spammy, so use the "distinct error logger":

var DistinctErrorLog = NewDistinctErrorLogger()

bmon added a commit to bmon/hugo that referenced this issue Sep 30, 2017
Add template function that will build a string from the given format
string and arguments, then log it to ERROR. This has an intended
side-effect of causing the build to fail, when executed.

Resolves gohugoio#3817
bmon added a commit to bmon/hugo that referenced this issue Sep 30, 2017
Add template function that will build a string from the given format
string and arguments, then log it to ERROR. This has an intended
side-effect of causing the build to fail, when executed.

Resolves gohugoio#3817
bmon added a commit to bmon/hugo that referenced this issue Oct 1, 2017
Add template function that will build a string from the given format
string and arguments, then log it to ERROR. This has an intended
side-effect of causing the build to fail, when executed.

Resolves gohugoio#3817
@bep bep closed this as completed in #3927 Oct 3, 2017
bep pushed a commit that referenced this issue Oct 3, 2017
Add template function that will build a string from the given format
string and arguments, then log it to ERROR. This has an intended
side-effect of causing the build to fail, when executed.

Resolves #3817
@github-actions
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 Mar 14, 2022
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.

3 participants