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

Error wrapping #1347

Merged
merged 40 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
2b4cd5b
Merge pull request #1086 from goplus/main
xushiwei Jan 5, 2022
239d373
Merge pull request #1088 from goplus/main
xushiwei Jan 5, 2022
5e98d13
Merge pull request #1090 from goplus/main
xushiwei Jan 5, 2022
521507a
Merge pull request #1092 from goplus/main
xushiwei Jan 5, 2022
a62a3a2
VERSION
xushiwei Jan 5, 2022
9decbcd
Merge branch 'v1.1' of github.com:goplus/gop into v1.1
xushiwei Jan 5, 2022
587c0db
Merge pull request #1094 from goplus/main
xushiwei Jan 7, 2022
a39f034
release version v1.1.0-beta2
xushiwei Jan 7, 2022
df9cff6
Merge pull request #1096 from goplus/main
xushiwei Jan 7, 2022
53b4965
Merge pull request #1157 from goplus/main
xushiwei Apr 23, 2022
9740ff1
Update VERSION
xushiwei Apr 23, 2022
16d3db4
Merge pull request #1160 from goplus/main
xushiwei Apr 24, 2022
031ed2a
Merge pull request #1201 from goplus/main
xushiwei May 19, 2022
297fa67
Update VERSION
xushiwei May 19, 2022
76ff319
Merge pull request #1212 from goplus/main
xushiwei May 21, 2022
da80614
Update VERSION
xushiwei May 21, 2022
bdbdf54
Merge pull request #1220 from goplus/main
xushiwei May 29, 2022
fe0d1e7
Update VERSION
xushiwei May 29, 2022
b8957db
Merge pull request #1225 from goplus/main
xushiwei May 31, 2022
f73fa9a
Update VERSION
xushiwei May 31, 2022
9833347
Merge pull request #1231 from goplus/main
xushiwei May 31, 2022
5948be9
Merge pull request #1233 from goplus/main
xushiwei Jun 3, 2022
a15b962
Merge pull request #1235 from goplus/main
xushiwei Jun 3, 2022
a82b787
Merge pull request #1239 from goplus/main
xushiwei Jun 4, 2022
fc15a95
Update VERSION
xushiwei Jun 5, 2022
7920b5c
Update VERSION
xushiwei Jun 5, 2022
43d1e58
Merge pull request #1249 from goplus/main
xushiwei Jun 6, 2022
85b1b0f
Merge pull request #1255 from goplus/main
xushiwei Jun 6, 2022
98f99f8
Merge pull request #1291 from goplus/main
xushiwei Jun 19, 2022
caedd51
Update VERSION
xushiwei Jun 19, 2022
ff1c29b
Merge pull request #1297 from goplus/main
xushiwei Jun 19, 2022
27e3da4
Update VERSION
xushiwei Jun 20, 2022
45d7bc8
Merge pull request #1304 from goplus/main
xushiwei Jun 20, 2022
13f6d09
Merge pull request #1325 from goplus/main
xushiwei Jul 5, 2022
57e8cbe
Merge pull request #1327 from goplus/main
xushiwei Jul 5, 2022
0719aed
Merge pull request #1334 from goplus/main
xushiwei Jul 16, 2022
f861dac
Update VERSION
xushiwei Jul 16, 2022
8455cf6
Merge pull request #1343 from goplus/main
xushiwei Aug 2, 2022
8fd2252
added error wrapping
libmonsoon-dev Aug 14, 2022
c655a50
fix tests
libmonsoon-dev Aug 14, 2022
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
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.1.3
Copy link
Member

@xushiwei xushiwei Aug 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't add VERSION file into the main branch (it can only exist in release branches).

34 changes: 31 additions & 3 deletions cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,15 @@ import (
fmt "fmt"
os "os"
iox "github.com/goplus/gop/builtin/iox"
errors "github.com/qiniu/x/errors"
)

func main() {
for _gop_it := iox.EnumLines(func() (_gop_ret *os.File) {
var _gop_err error
_gop_ret, _gop_err = os.Open("foo.txt")
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "open(\"foo.txt\")", "/foo/bar.gop", 2, "main.main")
panic(_gop_err)
}
return
Expand Down Expand Up @@ -912,7 +914,10 @@ func main() {
println(a, b)
}`, `package main

import fmt "fmt"
import (
fmt "fmt"
errors "github.com/qiniu/x/errors"
)

func t() (int, int, error) {
return 0, 0, nil
Expand All @@ -922,6 +927,7 @@ func main() {
var _gop_err error
_gop_ret, _gop_ret2, _gop_err = t()
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "t()", "/foo/bar.gop", 9, "main.main")
panic(_gop_err)
}
return
Expand All @@ -943,6 +949,8 @@ func main() {
t()!
}`, `package main

import errors "github.com/qiniu/x/errors"

func t() error {
return nil
}
Expand All @@ -951,6 +959,7 @@ func main() {
var _gop_err error
_gop_err = t()
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "t()", "/foo/bar.gop", 9, "main.main")
panic(_gop_err)
}
return
Expand Down Expand Up @@ -1454,6 +1463,7 @@ func foo(script string) {
import (
fmt "fmt"
goptest "github.com/goplus/gop/ast/goptest"
errors "github.com/qiniu/x/errors"
gopq "github.com/goplus/gop/ast/gopq"
)

Expand All @@ -1462,6 +1472,7 @@ func foo(script string) {
var _gop_err error
_gop_ret, _gop_err = goptest.New(script)
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "goptest.New(script)", "/foo/bar.gop", 4, "main.foo")
panic(_gop_err)
}
return
Expand All @@ -1486,6 +1497,7 @@ func foo(script string) {
import (
fmt "fmt"
goptest "github.com/goplus/gop/ast/goptest"
errors "github.com/qiniu/x/errors"
gopq "github.com/goplus/gop/ast/gopq"
)

Expand All @@ -1494,6 +1506,7 @@ func foo(script string) {
var _gop_err error
_gop_ret, _gop_err = goptest.New(script)
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "goptest.New(script)", "/foo/bar.gop", 4, "main.foo")
panic(_gop_err)
}
return
Expand All @@ -1513,14 +1526,18 @@ func add(x, y string) (int, error) {
}
`, `package main

import strconv "strconv"
import (
strconv "strconv"
errors "github.com/qiniu/x/errors"
)

func add(x string, y string) (int, error) {
var _autoGo_1 int
{
var _gop_err error
_autoGo_1, _gop_err = strconv.Atoi(x)
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "strconv.Atoi(x)", "/foo/bar.gop", 5, "main.add")
return 0, _gop_err
}
goto _autoGo_2
Expand All @@ -1531,6 +1548,7 @@ func add(x string, y string) (int, error) {
var _gop_err error
_autoGo_3, _gop_err = strconv.Atoi(y)
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "strconv.Atoi(y)", "/foo/bar.gop", 5, "main.add")
return 0, _gop_err
}
goto _autoGo_4
Expand Down Expand Up @@ -1577,12 +1595,16 @@ func TestErrWrapPanic(t *testing.T) {
var ret int = println("Hi")!
`, `package main

import fmt "fmt"
import (
fmt "fmt"
errors "github.com/qiniu/x/errors"
)

var ret int = func() (_gop_ret int) {
var _gop_err error
_gop_ret, _gop_err = fmt.Println("Hi")
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "println(\"Hi\")", "/foo/bar.gop", 2, "main.main")
panic(_gop_err)
}
return
Expand All @@ -1599,6 +1621,8 @@ func mkdir(name string) error {
mkdir! "foo"
`, `package main

import errors "github.com/qiniu/x/errors"

func mkdir(name string) error {
return nil
}
Expand All @@ -1607,6 +1631,7 @@ func main() {
var _gop_err error
_gop_err = mkdir("foo")
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "mkdir \"foo\"", "/foo/bar.gop", 6, "main.main")
panic(_gop_err)
}
return
Expand All @@ -1624,6 +1649,8 @@ func foo() (func(), error) {
foo()!()
`, `package main

import errors "github.com/qiniu/x/errors"

func foo() (func(), error) {
return nil, nil
}
Expand All @@ -1632,6 +1659,7 @@ func main() {
var _gop_err error
_gop_ret, _gop_err = foo()
if _gop_err != nil {
_gop_err = errors.NewFrame(_gop_err, "foo()", "/foo/bar.gop", 6, "main.main")
panic(_gop_err)
}
return
Expand Down
43 changes: 41 additions & 2 deletions cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package cl

import (
"bytes"
"github.com/goplus/gop/printer"
goast "go/ast"
gotoken "go/token"
"go/types"
Expand Down Expand Up @@ -70,6 +72,8 @@ const (
objCPkgRef
)

const errorPkgPath = "github.com/qiniu/x/errors"

func compileIdent(ctx *blockCtx, ident *ast.Ident, flags int) (obj *gox.PkgRef, kind int) {
fvalue := (flags&clIdentSelectorExpr) != 0 || (flags&clIdentLHS) == 0
name := ident.Name
Expand Down Expand Up @@ -1012,10 +1016,34 @@ func compileErrWrapExpr(ctx *blockCtx, v *ast.ErrWrapExpr, inFlags int) {
cb.Assign(n+1, 1)

cb.If().Val(err).CompareNil(gotoken.NEQ).Then()
if v.Default == nil {
pos := pkg.Fset.Position(v.Pos())
currentFunc := ctx.cb.Func().Ancestor()
const newFrameArgs = 5

currentFuncName := currentFunc.Name()
if currentFuncName == "" {
currentFuncName = "main"
}

currentFuncName = strings.Join([]string{currentFunc.Pkg().Name(), currentFuncName}, ".")

cb.
VarRef(err).
Val(pkg.Import(errorPkgPath).Ref("NewFrame")).
Val(err).
Val(sprintAst(pkg.Fset, v.X)).
Val(pos.Filename).
Val(pos.Line).
Val(currentFuncName).
Call(newFrameArgs).
Assign(1)
}

if v.Tok == token.NOT { // expr!
cb.Val(pkg.Builtin().Ref("panic")).Val(err).Call(1).EndStmt() // TODO: wrap err
cb.Val(pkg.Builtin().Ref("panic")).Val(err).Call(1).EndStmt()
} else if v.Default == nil { // expr?
cb.Val(err).ReturnErr(true) // TODO: wrap err & return err
cb.Val(err).ReturnErr(true)
} else { // expr?:val
compileExpr(ctx, v.Default)
cb.Return(1)
Expand All @@ -1026,4 +1054,15 @@ func compileErrWrapExpr(ctx *blockCtx, v *ast.ErrWrapExpr, inFlags int) {
}
}

func sprintAst(fset *token.FileSet, x ast.Node) string {
var buf bytes.Buffer
err := printer.Fprint(&buf, fset, x)
if err != nil {
panic("Unexpected error: " + err.Error())
}

return buf.String()
}


// -----------------------------------------------------------------------------