Catch exception for transform.Unmarshal csv

Hello, is it possible to catch error when using unmarshal for csv? Or maybe an ignore-error flag?

consider this:

{{ $csv := "a,b,c" | transform.Unmarshal (dict "delimiter" ";") }}

(notice the delimiter difference)

error calling Unmarshal: unknown format

I want to keep the build going on and handle that error.
Thanks,

Hugo can’t catch errors. You could request a feature that makes it catchable, but I don’t know if that is useful enough for the majority.

Also, something might be off with your code, because unmarshalling “a,b,c” with a delimiter of “;” should result in [“a,b,c”] and no error, because not having the delimiter in it should not result in an error. It’s just one single element. The error states, that the format is not known. The format should be string or a resource. Are you sure those quotation marks are simple " and not some fancy typographic quotes in your code?

@davidsneighbour

You can use the transform.Unmarshal function with JSON, TOML, YAML, XML, and CSV data.

Hugo parses the data to determine the format:
https://github.com/gohugoio/hugo/blob/master/parser/metadecoders/format.go#L78-L82

Since it can’t find the delimiter in the data provided, it cannot determine the format.

1 Like

@ajboni

If possible, use a data format with rigid syntax. With CSV you have no idea if it contains a header row, if the fields are encapsulated, or which character is used to delimit the fields.

If you must use CSV, you could try to parse it yourself, but that will be very fragile. For example:

{{ $map := dict }}
{{ $data := "a,b,c" }}
{{ $delimiter := "," }}
{{ if in $data $delimiter }}
  {{ $map = $data | transform.Unmarshal (dict "delimiter" $delimiter) }}
{{ else }}
  {{ warnf "Data was not unmarshaled." }}
{{ end }}
1 Like

Thanks @jmooring , this is very fragile indeed, as the error can be elsewhere like an unescaped quote or a missing column.

As I’m not in control of the data coming in CSV this will be unusable as a little typo can break the build.

I suppose that sending a JSON with bad format will have the same problem…

1 Like

Is the incoming data a string, or a file somewhere in your project directory, or a remote file?

It’s a string in the frontmatter of a .md file inside the content folder.

If the variability is simply the delimiter, and you know what it is, you could store the delimiter as a separate front matter variable.

Otherwise, you will have to somehow validate the data before it’s included in front matter.

Well, I guess its a dead end.
I don’t know the technical implications, but for my use case, it would be really needed to have some sort try/catch or isValid mechanism.
Thanks for the replies @jmooring !!

So, the answer is currently “no” to “is it possible to catch this exception?”

This is the default behaviour of Go templates.

We have made resources.GeRemote work differently in this department, and it would be great if we could make that into something general.

4 Likes
2 Likes

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