tyler-sommer / stick

A golang port of the Twig templating engine
MIT License
183 stars 36 forks source link

Better error handling #11

Open tyler-sommer opened 7 years ago

tyler-sommer commented 7 years ago

There are several areas that could use some tweaking with regard to error handling in Stick. This issue will be used to track and explain the reasoning behind these changes.

Each section below describes a different category of improvement. Tasks to carry out these improvements are defined together at the end, grouped by category.

Missing default statements

Incomplete switch statements with no default handling may silently fail which could lead to frustration if a user encounters this behavior.

Places affected

These cases are unlikely to encounter these silent failures in normal operation ("should never happen" barring the existence of a bug), but anyone hacking on stick itself may experience frustration when something isn't working and there is no hint as to what's going on.

Fixing these is technically a BC break as any templates relying on these silent failures will now fail to execute, returning an error instead. However, I don't think this is a real concern in practice and fixing these does not warrant a major version bump.

Impossible to return an error

User-defined additions to Stick do not currently support returning any error. This results in needing to return some "empty" value rather than trying to signal an error.

Places affected

I'm a little bit on the fence on whether some of these should be fixed. I see two main considerations:

Behavior in Twig itself is fairly loose and template execution generally tends to be forgiving of type mismatches and failed coercions. In Twig, user-defined functionality would need to throw an exception which would interrupt template execution. This can also be done in Go, if desired, with panic(). However, in many places it makes sense to just return some sensible "empty" value rather than halting template execution. Using panic() is also generally kind of smelly, though, so I hesitate to make that the go-to solution.

On the flip side, the current situation can lead to difficult to debug situations where user-defined functionality is not working as expected and there is no sign of what's going on because any error is being silently thrown away. Of course, logging could be used to provide some clue to the user.

One final factor to consider here is modifying the affected method signatures to return an error (arguably the best choice) would be a fairly major BC break, requiring all existing user-defined code to be updated. This would probably warrant a major version bump.

Tentative plan

Inconsistent error messages

Error messages throughout Stick are inconsistent and could use some attention.

These are relatively minor, though any code expecting specific error messages may stop working when the messages change. I think this is unlikely and don't consider this reason enough to bump the major version, however.

Places affected

Other improvements

Tasks

Missing default case in switch statements

User-defined functionality

Consistent error messages

Other improvements

tyler-sommer commented 1 year ago

Updated the original comment with comprehensive plans for improving error handling :D