Skip to content

text/template/parse: remove truncated error context or add an template.Option #27930

@draeron

Description

@draeron

What version of Go are you using (go version)?

go1.11 windows/amd64

Problem

When an error is thrown because a template is missing a value ("missingkey=error"), the error sent will truncate the context upon which the field is evaluated.

executing "someTemplateName" at <.k8s.clusters.someCl...>: map has no entry for key "someField"

Sample code to demontrate : https://play.golang.org/p/h3_fgkbUo_S

This is where the string gets truncated :

if len(context) > 20 {

Why is that a problem

I'm pretty sure this has been met by many users using tools that use the text/template engine (hello helm users). Once the context is deep enough, it become excitedly difficult to understand where in the data tree something is missing. If the field is dynamically accessed, say through a variable, it become pretty much impossible to know what exactly is wrong and how to fix the template.

My particular use case is that i'm trying to list all accessed values in a template, since it is impossible to access the internal structure of the parse.Tree, i though i could execute a template, catch missing value errors and construct a list through error messages.

Proposal for a solution

There are multiple solutions

  • add more specific error (parse.ValueNotFound ?) which give detailed informations which include the context
  • modify func ErrorContext to remove truncation
  • enable some option to keep the behavior backward compatible

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions