hummingbird-project / swift-mustache

Mustache templating engine
Apache License 2.0
17 stars 2 forks source link

A way to have a report of error situations #40

Open MahdiBM opened 1 month ago

MahdiBM commented 1 month ago

I just implemented this for EnumeratorMacro:

Screenshot 2024-07-16 at 10 46 36 PM

This will basically report back if a function call is invalid, and won't result in a silent failure. Note that this is only possible because EnumeratorMacro defines its own wrapper types to have control over the transforms.

How it works is using a TaskLocal: https://github.com/MahdiBM/enumerator-macro/blob/0a1b67d2941dec346f59f06d0c7c86888712aae0/Sources/EnumeratorMacroImpl/EnumeratorMacroType.swift#L50 Which are used to store an error: https://github.com/MahdiBM/enumerator-macro/blob/0a1b67d2941dec346f59f06d0c7c86888712aae0/Sources/EnumeratorMacroImpl/Types/EString.swift#L70

I think swift-mustache can also have such an option. If the user has inserted a value for the TaskLocal, swift-mustache will populate it. swift-mustache seems a nil task-local value and just moves on with the current behavior. The TaskLocal should be defined in swift-mustache, but it just defaults to nil which would result in no capturing of errors.

adam-fowler commented 1 month ago

I could make the render function throwing. Seems odd to add a task local to store errors. Although these could be considered warnings I guess. The other thing that could cause a warning is missed variables ie {{var}} when var doesn't exist.

MahdiBM commented 1 month ago

Yeah using task locals is odd, but I guess that was the best I could come up with without manipulating swift-mustache, in EnumeratorMacro.

EnumeratorMacro is now using pretty much only internal wrapper types so it can have full control over transforms etc... so this is no longer an issue for me, because I won't be falling back to swift-mustache's transforms.

But it is still a question if swift-mustache should report errors better anyways and not be loose like in JS. Possibly having an option to configure.