Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrapping errors for detecting custom errors #7

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pongo2

import (
"errors"
"fmt"
"regexp"
)
Expand Down Expand Up @@ -108,8 +107,8 @@ func NewChildExecutionContext(parent *ExecutionContext) *ExecutionContext {
return newctx
}

func (ctx *ExecutionContext) Error(msg string, token *Token) *Error {
return ctx.OrigError(errors.New(msg), token)
func (ctx *ExecutionContext) Error(err error, token *Token) *Error {
return ctx.OrigError(err, token)
}

func (ctx *ExecutionContext) OrigError(err error, token *Token) *Error {
Expand Down
6 changes: 3 additions & 3 deletions filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (p *Parser) parseFilter() (*filterCall, *Error) {

// Check filter ident
if identToken == nil {
return nil, p.Error("Filter name must be an identifier.", nil)
return nil, p.Error(fmt.Errorf("Filter name must be an identifier."), nil)
}

filter := &filterCall{
Expand All @@ -115,15 +115,15 @@ func (p *Parser) parseFilter() (*filterCall, *Error) {
// Get the appropriate filter function and bind it
filterFn, exists := filters[identToken.Val]
if !exists {
return nil, p.Error(fmt.Sprintf("Filter '%s' does not exist.", identToken.Val), identToken)
return nil, p.Error(fmt.Errorf("Filter '%s' does not exist.", identToken.Val), identToken)
}

filter.filterFunc = filterFn

// Check for filter-argument (2 tokens needed: ':' ARG)
if p.Match(TokenSymbol, ":") != nil {
if p.Peek(TokenSymbol, "}}") != nil {
return nil, p.Error("Filter parameter required after ':'.", nil)
return nil, p.Error(fmt.Errorf("Filter parameter required after ':'."), nil)
}

// Get filter argument expression
Expand Down
15 changes: 7 additions & 8 deletions parser.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pongo2

import (
"errors"
"fmt"
"strings"
)
Expand Down Expand Up @@ -179,7 +178,7 @@ func (p *Parser) GetR(shift int) *Token {
// The 'token'-argument is optional. If provided, it will take
// the token's position information. If not provided, it will
// automatically use the CURRENT token's position information.
func (p *Parser) Error(msg string, token *Token) *Error {
func (p *Parser) Error(err error, token *Token) *Error {
if token == nil {
// Set current token
token = p.Current()
Expand All @@ -202,7 +201,7 @@ func (p *Parser) Error(msg string, token *Token) *Error {
Line: line,
Column: col,
Token: token,
OrigError: errors.New(msg),
OrigError: err,
}
}

Expand Down Expand Up @@ -244,7 +243,7 @@ func (p *Parser) WrapUntilTag(names ...string) (*NodeWrapper, *Parser, *Error) {
t := p.Current()
p.Consume()
if t == nil {
return nil, nil, p.Error("Unexpected EOF.", p.lastToken)
return nil, nil, p.Error(fmt.Errorf("Unexpected EOF."), p.lastToken)
}
tagArgs = append(tagArgs, t)
}
Expand All @@ -261,7 +260,7 @@ func (p *Parser) WrapUntilTag(names ...string) (*NodeWrapper, *Parser, *Error) {
wrapper.nodes = append(wrapper.nodes, node)
}

return nil, nil, p.Error(fmt.Sprintf("Unexpected EOF, expected tag %s.", strings.Join(names, " or ")),
return nil, nil, p.Error(fmt.Errorf("Unexpected EOF, expected tag %s.", strings.Join(names, " or ")),
p.lastToken)
}

Expand Down Expand Up @@ -298,7 +297,7 @@ func (p *Parser) SkipUntilTag(names ...string) *Error {
p.Consume()
if p.Current() == nil {
// EOF encountered
return p.Error("Unexpected EOF, expected '%}'", p.lastToken)
return p.Error(fmt.Errorf("Unexpected EOF, expected '%%}'"), p.lastToken)
}
}
}
Expand All @@ -307,9 +306,9 @@ func (p *Parser) SkipUntilTag(names ...string) *Error {
t := p.Current()
p.Consume()
if t == nil {
return p.Error("Unexpected EOF.", p.lastToken)
return p.Error(fmt.Errorf("Unexpected EOF."), p.lastToken)
}
}

return p.Error(fmt.Sprintf("Unexpected EOF, expected tag %s.", strings.Join(names, " or ")), p.lastToken)
return p.Error(fmt.Errorf("Unexpected EOF, expected tag %s.", strings.Join(names, " or ")), p.lastToken)
}
4 changes: 3 additions & 1 deletion parser_document.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pongo2

import "fmt"

// Doc = { ( Filter | Tag | HTML ) }
func (p *Parser) parseDocElement() (INode, *Error) {
t := p.Current()
Expand Down Expand Up @@ -31,7 +33,7 @@ func (p *Parser) parseDocElement() (INode, *Error) {
return tag, nil
}
}
return nil, p.Error("Unexpected token (only HTML/tags/filters in templates allowed)", t)
return nil, p.Error(fmt.Errorf("Unexpected token (only HTML/tags/filters in templates allowed)"), t)
}

func (tpl *Template) parse() *Error {
Expand Down
20 changes: 10 additions & 10 deletions parser_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (expr *Expression) Evaluate(ctx *ExecutionContext) (*Value, *Error) {
return AsValue(v2.IsTrue()), nil
}
default:
return nil, ctx.Error(fmt.Sprintf("unimplemented: %s", expr.opToken.Val), expr.opToken)
return nil, ctx.Error(fmt.Errorf("unimplemented: %s", expr.opToken.Val), expr.opToken)
}
} else {
return v1, nil
Expand Down Expand Up @@ -217,7 +217,7 @@ func (expr *relationalExpression) Evaluate(ctx *ExecutionContext) (*Value, *Erro
case "in":
return AsValue(v2.Contains(v1)), nil
default:
return nil, ctx.Error(fmt.Sprintf("unimplemented: %s", expr.opToken.Val), expr.opToken)
return nil, ctx.Error(fmt.Errorf("unimplemented: %s", expr.opToken.Val), expr.opToken)
}
} else {
return v1, nil
Expand All @@ -243,10 +243,10 @@ func (expr *simpleExpression) Evaluate(ctx *ExecutionContext) (*Value, *Error) {
case result.IsInteger():
result = AsValue(-1 * result.Integer())
default:
return nil, ctx.Error("Operation between a number and a non-(float/integer) is not possible", nil)
return nil, ctx.Error(fmt.Errorf("Operation between a number and a non-(float/integer) is not possible"), nil)
}
} else {
return nil, ctx.Error("Negative sign on a non-number expression", expr.GetPositionToken())
return nil, ctx.Error(fmt.Errorf("Negative sign on a non-number expression"), expr.GetPositionToken())
}
}

Expand Down Expand Up @@ -275,7 +275,7 @@ func (expr *simpleExpression) Evaluate(ctx *ExecutionContext) (*Value, *Error) {
// Result will be an integer
return AsValue(result.Integer() - t2.Integer()), nil
default:
return nil, ctx.Error("Unimplemented", expr.GetPositionToken())
return nil, ctx.Error(fmt.Errorf("Unimplemented"), expr.GetPositionToken())
}
}

Expand Down Expand Up @@ -305,25 +305,25 @@ func (expr *term) Evaluate(ctx *ExecutionContext) (*Value, *Error) {
// Result will be float
divisor := f2.Float()
if divisor == 0 {
return nil, ctx.Error("float divide by zero", expr.factor2.GetPositionToken())
return nil, ctx.Error(fmt.Errorf("float divide by zero"), expr.factor2.GetPositionToken())
}
return AsValue(f1.Float() / divisor), nil
}
// Result will be int
divisor := f2.Integer()
if divisor == 0 {
return nil, ctx.Error("integer divide by zero", expr.factor2.GetPositionToken())
return nil, ctx.Error(fmt.Errorf("integer divide by zero"), expr.factor2.GetPositionToken())
}
return AsValue(f1.Integer() / divisor), nil
case "%":
// Result will be int
divisor := f2.Integer()
if divisor == 0 {
return nil, ctx.Error("integer divide by zero", expr.factor2.GetPositionToken())
return nil, ctx.Error(fmt.Errorf("integer divide by zero"), expr.factor2.GetPositionToken())
}
return AsValue(f1.Integer() % divisor), nil
default:
return nil, ctx.Error("unimplemented", expr.opToken)
return nil, ctx.Error(fmt.Errorf("unimplemented"), expr.opToken)
}
} else {
return f1, nil
Expand Down Expand Up @@ -352,7 +352,7 @@ func (p *Parser) parseFactor() (IEvaluator, *Error) {
return nil, err
}
if p.Match(TokenSymbol, ")") == nil {
return nil, p.Error("Closing bracket expected after expression", nil)
return nil, p.Error(fmt.Errorf("Closing bracket expected after expression"), nil)
}
return expr, nil
}
Expand Down
8 changes: 4 additions & 4 deletions tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,19 @@ func (p *Parser) parseTagElement() (INodeTag, *Error) {

// Check for identifier
if tokenName == nil {
return nil, p.Error("Tag name must be an identifier.", nil)
return nil, p.Error(fmt.Errorf("Tag name must be an identifier."), nil)
}

// Check for the existing tag
tag, exists := tags[tokenName.Val]
if !exists {
// Does not exists
return nil, p.Error(fmt.Sprintf("Tag '%s' not found (or beginning tag not provided)", tokenName.Val), tokenName)
return nil, p.Error(fmt.Errorf("Tag '%s' not found (or beginning tag not provided)", tokenName.Val), tokenName)
}

// Check sandbox tag restriction
if _, isBanned := p.template.set.bannedTags[tokenName.Val]; isBanned {
return nil, p.Error(fmt.Sprintf("Usage of tag '%s' is not allowed (sandbox restriction active).", tokenName.Val), tokenName)
return nil, p.Error(fmt.Errorf("Usage of tag '%s' is not allowed (sandbox restriction active).", tokenName.Val), tokenName)
}

var argsToken []*Token
Expand All @@ -113,7 +113,7 @@ func (p *Parser) parseTagElement() (INodeTag, *Error) {

// EOF?
if p.Remaining() == 0 {
return nil, p.Error("Unexpectedly reached EOF, no tag end found.", p.lastToken)
return nil, p.Error(fmt.Errorf("Unexpectedly reached EOF, no tag end found."), p.lastToken)
}

p.Match(TokenSymbol, "%}")
Expand Down
8 changes: 5 additions & 3 deletions tags_autoescape.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pongo2

import "fmt"

type tagAutoescapeNode struct {
wrapper *NodeWrapper
autoescape bool
Expand Down Expand Up @@ -30,18 +32,18 @@ func tagAutoescapeParser(doc *Parser, start *Token, arguments *Parser) (INodeTag

modeToken := arguments.MatchType(TokenIdentifier)
if modeToken == nil {
return nil, arguments.Error("A mode is required for autoescape-tag.", nil)
return nil, arguments.Error(fmt.Errorf("A mode is required for autoescape-tag."), nil)
}
if modeToken.Val == "on" {
autoescapeNode.autoescape = true
} else if modeToken.Val == "off" {
autoescapeNode.autoescape = false
} else {
return nil, arguments.Error("Only 'on' or 'off' is valid as an autoescape-mode.", nil)
return nil, arguments.Error(fmt.Errorf("Only 'on' or 'off' is valid as an autoescape-mode."), nil)
}

if arguments.Remaining() > 0 {
return nil, arguments.Error("Malformed autoescape-tag arguments.", nil)
return nil, arguments.Error(fmt.Errorf("Malformed autoescape-tag arguments."), nil)
}

return autoescapeNode, nil
Expand Down
14 changes: 7 additions & 7 deletions tags_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (node *tagBlockNode) Execute(ctx *ExecutionContext, writer TemplateWriter)
lenBlockWrappers := len(blockWrappers)

if lenBlockWrappers == 0 {
return ctx.Error("internal error: len(block_wrappers) == 0 in tagBlockNode.Execute()", nil)
return ctx.Error(fmt.Errorf("internal error: len(block_wrappers) == 0 in tagBlockNode.Execute()"), nil)
}

blockWrapper := blockWrappers[lenBlockWrappers-1]
Expand Down Expand Up @@ -80,16 +80,16 @@ func (t tagBlockInformation) Super() (*Value, error) {

func tagBlockParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
if arguments.Count() == 0 {
return nil, arguments.Error("Tag 'block' requires an identifier.", nil)
return nil, arguments.Error(fmt.Errorf("Tag 'block' requires an identifier."), nil)
}

nameToken := arguments.MatchType(TokenIdentifier)
if nameToken == nil {
return nil, arguments.Error("First argument for tag 'block' must be an identifier.", nil)
return nil, arguments.Error(fmt.Errorf("First argument for tag 'block' must be an identifier."), nil)
}

if arguments.Remaining() != 0 {
return nil, arguments.Error("Tag 'block' takes exactly 1 argument (an identifier).", nil)
return nil, arguments.Error(fmt.Errorf("Tag 'block' takes exactly 1 argument (an identifier)."), nil)
}

wrapper, endtagargs, err := doc.WrapUntilTag("endblock")
Expand All @@ -100,13 +100,13 @@ func tagBlockParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Er
endtagnameToken := endtagargs.MatchType(TokenIdentifier)
if endtagnameToken != nil {
if endtagnameToken.Val != nameToken.Val {
return nil, endtagargs.Error(fmt.Sprintf("Name for 'endblock' must equal to 'block'-tag's name ('%s' != '%s').",
return nil, endtagargs.Error(fmt.Errorf("Name for 'endblock' must equal to 'block'-tag's name ('%s' != '%s').",
nameToken.Val, endtagnameToken.Val), nil)
}
}

if endtagnameToken == nil || endtagargs.Remaining() > 0 {
return nil, endtagargs.Error("Either no or only one argument (identifier) allowed for 'endblock'.", nil)
return nil, endtagargs.Error(fmt.Errorf("Either no or only one argument (identifier) allowed for 'endblock'."), nil)
}
}

Expand All @@ -118,7 +118,7 @@ func tagBlockParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Er
if !hasBlock {
tpl.blocks[nameToken.Val] = wrapper
} else {
return nil, arguments.Error(fmt.Sprintf("Block named '%s' already defined", nameToken.Val), nil)
return nil, arguments.Error(fmt.Errorf("Block named '%s' already defined", nameToken.Val), nil)
}

return &tagBlockNode{name: nameToken.Val}, nil
Expand Down
4 changes: 3 additions & 1 deletion tags_comment.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pongo2

import "fmt"

type tagCommentNode struct{}

func (node *tagCommentNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
Expand All @@ -16,7 +18,7 @@ func tagCommentParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *
}

if arguments.Count() != 0 {
return nil, arguments.Error("Tag 'comment' does not take any argument.", nil)
return nil, arguments.Error(fmt.Errorf("Tag 'comment' does not take any argument."), nil)
}

return commentNode, nil
Expand Down
6 changes: 4 additions & 2 deletions tags_cycle.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pongo2

import "fmt"

type tagCycleValue struct {
node *tagCycleNode
value *Value
Expand Down Expand Up @@ -81,7 +83,7 @@ func tagCycleParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Er

nameToken := arguments.MatchType(TokenIdentifier)
if nameToken == nil {
return nil, arguments.Error("Name (identifier) expected after 'as'.", nil)
return nil, arguments.Error(fmt.Errorf("Name (identifier) expected after 'as'."), nil)
}
cycleNode.asName = nameToken.Val

Expand All @@ -95,7 +97,7 @@ func tagCycleParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Er
}

if arguments.Remaining() > 0 {
return nil, arguments.Error("Malformed cycle-tag.", nil)
return nil, arguments.Error(fmt.Errorf("Malformed cycle-tag."), nil)
}

return cycleNode, nil
Expand Down
Loading