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

pkg/errors 调用栈重复问题 #1

Closed
yahuian opened this issue Jan 12, 2024 · 0 comments
Closed

pkg/errors 调用栈重复问题 #1

yahuian opened this issue Jan 12, 2024 · 0 comments
Labels
documentation Improvements or additions to documentation

Comments

@yahuian
Copy link
Owner

yahuian commented Jan 12, 2024

pkg/errors#242

package main

import (
	"fmt"

	"github.com/pkg/errors"
)

func main() {
	err := one()
	fmt.Printf("%+v\n", err)
}

func one() error {
	return errors.Wrap(two(), "call two error")
}

func two() error {
	return errors.Wrap(three(), "call three error")
}

func three() error {
	return errors.New("something error")
}

可以看到每个 error 都带了调用栈,冗余信息比较多

something error
main.three
        /home/anyh/Documents/golang/demo/errors_demo/main.go:23
main.two
        /home/anyh/Documents/golang/demo/errors_demo/main.go:19
main.one
        /home/anyh/Documents/golang/demo/errors_demo/main.go:15
main.main
        /home/anyh/Documents/golang/demo/errors_demo/main.go:10
runtime.main
        /usr/local/go/src/runtime/proc.go:250
runtime.goexit
        /usr/local/go/src/runtime/asm_amd64.s:1598
call three error
main.two
        /home/anyh/Documents/golang/demo/errors_demo/main.go:19
main.one
        /home/anyh/Documents/golang/demo/errors_demo/main.go:15
main.main
        /home/anyh/Documents/golang/demo/errors_demo/main.go:10
runtime.main
        /usr/local/go/src/runtime/proc.go:250
runtime.goexit
        /usr/local/go/src/runtime/asm_amd64.s:1598
call two error
main.one
        /home/anyh/Documents/golang/demo/errors_demo/main.go:15
main.main
        /home/anyh/Documents/golang/demo/errors_demo/main.go:10
runtime.main
        /usr/local/go/src/runtime/proc.go:250
runtime.goexit
        /usr/local/go/src/runtime/asm_amd64.s:1598

当然你可以仅在最底层的 error 包含 stack,中间函数调用直接 return,这样最顶层就没有冗余信息了

package main

import (
	"fmt"

	"github.com/pkg/errors"
)

func main() {
	err := one()
	fmt.Printf("%+v\n", err)
}

func one() error {
	return two()
}

func two() error {
	return three()
}

func three() error {
	return errors.New("something error")
}
something error
main.three
        /home/anyh/Documents/golang/demo/errors_demo/main.go:23
main.two
        /home/anyh/Documents/golang/demo/errors_demo/main.go:19
main.one
        /home/anyh/Documents/golang/demo/errors_demo/main.go:15
main.main
        /home/anyh/Documents/golang/demo/errors_demo/main.go:10
runtime.main
        /usr/local/go/src/runtime/proc.go:250
runtime.goexit
        /usr/local/go/src/runtime/asm_amd64.s:1598

但是如果项目比较复杂,调用层级比较深的时候,不好保证

@yahuian yahuian added the documentation Improvements or additions to documentation label Jan 12, 2024
@yahuian yahuian closed this as completed Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant