Skip to content

Commit

Permalink
Merge pull request #141 from gobuffalo/task-#96
Browse files Browse the repository at this point in the history
Better wrapping for errors.
  • Loading branch information
paganotoni authored May 15, 2021
2 parents a6ab98c + ff2b33d commit 63095e9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (c *compiler) compile() (string, error) {
if c.curStmt != nil {
s = c.curStmt
}
return "", fmt.Errorf("line %d: %s", s.T().LineNumber, err)
return "", fmt.Errorf("line %d: %w", s.T().LineNumber, err)
}

c.write(bb, res)
Expand Down Expand Up @@ -768,7 +768,7 @@ func (c *compiler) evalCallExpression(node *ast.CallExpression) (interface{}, er
res := rv.Call(args)
if len(res) > 0 {
if e, ok := res[len(res)-1].Interface().(error); ok {
return nil, fmt.Errorf("could not call %s function: %s", node.Function, e)
return nil, fmt.Errorf("could not call %s function: %w", node.Function, e)
}
return res[0].Interface(), nil
}
Expand Down
22 changes: 22 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package plush

import (
"database/sql"

"errors"
"testing"

"github.com/stretchr/testify/require"
)

func TestErrorType(t *testing.T) {
r := require.New(t)

ctx := NewContext()
ctx.Set("sqlError", func() error {
return sql.ErrNoRows
})

_, err := Render(`<%= sqlError() %>`, ctx)
r.True(errors.Is(err, sql.ErrNoRows))
}

0 comments on commit 63095e9

Please sign in to comment.