feat(template): print actual error

relates to #3218
This commit is contained in:
Jan De Dobbeleer 2022-12-18 12:32:05 +01:00 committed by Jan De Dobbeleer
parent 39de419ed5
commit 2dc8dab25b
4 changed files with 8 additions and 7 deletions

View file

@ -4,7 +4,6 @@ import (
"oh-my-posh/mock"
"oh-my-posh/platform"
"oh-my-posh/properties"
"oh-my-posh/template"
"os"
"path/filepath"
"testing"
@ -50,7 +49,7 @@ func TestAzSegment(t *testing.T) {
{
Case: "Faulty template",
ExpectedEnabled: true,
ExpectedString: template.IncorrectTemplate,
ExpectedString: "<.Data.Burp>: can't evaluate field Burp in type template.Data",
Template: "{{ .Burp }}",
HasPowerShell: true,
},

View file

@ -4,7 +4,6 @@ import (
"errors"
"oh-my-posh/mock"
"oh-my-posh/properties"
"oh-my-posh/template"
"testing"
"github.com/stretchr/testify/assert"
@ -125,7 +124,7 @@ func TestNSSegment(t *testing.T) {
JSONResponse: `
[{"sgv":50,"direction":"DoubleDown"}]`,
Template: "\ue2a1 {{.Sgv}}{{.Burp}}",
ExpectedString: template.IncorrectTemplate,
ExpectedString: "<.Data.Burp>: can't evaluate field Burp in type template.Data",
ExpectedEnabled: true,
CacheTimeout: 10,
},

View file

@ -4,7 +4,6 @@ import (
"errors"
"oh-my-posh/mock"
"oh-my-posh/properties"
"oh-my-posh/template"
"testing"
"time"
@ -85,7 +84,7 @@ func TestStravaSegment(t *testing.T) {
},
},
Template: "{{.Ago}}{{.Burp}}",
ExpectedString: template.IncorrectTemplate,
ExpectedString: "<.Data.Burp>: can't evaluate field Burp in type template.Data",
ExpectedEnabled: true,
},
}

View file

@ -58,7 +58,11 @@ func (t *Text) Render() (string, error) {
err = tmpl.Execute(buffer, context)
if err != nil {
t.Env.Error("Render", err)
return "", errors.New(IncorrectTemplate)
msg := regex.FindNamedRegexMatch(`at (?P<MSG><.*)$`, err.Error())
if len(msg) == 0 {
return "", errors.New(IncorrectTemplate)
}
return "", errors.New(msg["MSG"])
}
text := buffer.String()
// issue with missingkey=zero ignored for map[string]interface{}