Replies: 2 comments 2 replies
-
Hello, Some thoughts :
So, my answers :
func toInt(value any) (int,error) {
if(defaultMode == must) {
return mustToInt(value)
}
return safeToInt(value)
}
func mustToInt(value any) (int, error) {
intVal,err := safeToInt(value);
if err != nil{
panic(err)
}
return intVal, nil
}
func safeToInt(value any) (int,error) {
// conversion...
if err == nil {
return intVal, nil
}
return 0, err
}
|
Beta Was this translation helpful? Give feedback.
2 replies
-
UPDATE: Solution have a dedicated page on documentation: |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Introduction
This RFC addresses the need for improved error handling mechanisms in Go templates within the
go-sprout/sprout
project. The goal is to ensure that template functions can operate without causing panics, with an emphasis on providing bothsafe
andmust
operational modes.Important
Actual implementation selected are available here https://github.com/orgs/go-sprout/discussions/32#discussioncomment-10520650
Motivation
Currently, unexpected errors in functions within templates can lead to panics, disrupting the rendering process and potentially causing system instability. By providing more robust error handling capabilities, developers can choose between failing silently and returning default values, or explicitly handling errors, depending on their specific needs and the application context.
Proposal
We propose two distinct operational modes for functions used in templates:
To implement these modes, the following solutions are proposed:
ErrHandler
that can be attached to thefunctionHandler
. This would allow developers to use a default handler without configuration or to customize their own error handling to achieve greater autonomy and control.safeToInt
andmustToInt
from a single function definition in the library (in this exampletoInt
), written in the "Must" style (e.g.,func(...any)(any, error)
). Also, provide a default mode to reduce the need for specifying "safe" or "must" each time, such as defaulting to "must" mode where callingtoInt
in template would executemustToInt
.This is an arbitrary example of how to set the default mode, the implementation will be
go generate
tool to define functions on the Sprout side, automatically generating them within the Sprout library and making them available for use at build time. This approach, however, limits external functions from utilizing the "safe/must" error management btw.Benefits
Drawbacks
Open Questions
Conclusion
Your insights and comments are crucial for refining and improving the proposed feature, ensuring it meets the needs and expectations of a broad range of developers. Please share your thoughts on the proposed functionalities, any concerns you might have, and any additional features you believe should be considered. This collaborative approach is essential for us to achieve a robust and versatile implementation. We look forward to your contributions and are excited to see how together we can enhance the capabilities of sprout.
Beta Was this translation helpful? Give feedback.
All reactions