Giter Site home page Giter Site logo

Comments (6)

puellanivis avatar puellanivis commented on August 26, 2024

What version of pkg/errors are you using? Some earlier versions have different behavior.

Using errors.Wrap(…) at every layer is inefficient, because it tends to end up repeating a lot of the same stacktrace information all the way up the chain. Ideally, you should use errors.Wrap(…) only on the non-pkg/errors error at the very start of the chain, and then at each additional level use errors.WithMessage(…).

Finally, you do not have to extract the stacktrace yourself to print it out. It should be printed out just by using fmt.Printf("%+v\n", err)

from errors.

MAAARKIN avatar MAAARKIN commented on August 26, 2024

@puellanivis i'm using "github.com/pkg/errors v0.9.1" version.

i will try to refactor my code to using like you said, i will change today and i go back to say if work. @puellanivis thx for your help.

from errors.

MAAARKIN avatar MAAARKIN commented on August 26, 2024

@puellanivis i change the code... but not work exactly i want, i need to extract the stacktrace to report to the team, to be honest, i just use the pkg error to show the stacktrace, but the stracktrace just print the first 2 levels of my services, if i change wraps to WithMessage, the stacktrace dont show.

i tried to use WithStack(err) to chain and just when the cause started i use Wrap, but the stacktrace dont show the entire stacktrace.

from errors.

MAAARKIN avatar MAAARKIN commented on August 26, 2024

Using code:

fmt.Printf("%+v", err.StackTrace()[0:6])

Print:

Stack Trace:

myrepos/conductortech/prepaid/_git/orchestrator.git/internal/service.AuthorizationServiceImpl.GetDetails
	C:/Workspace/prepaid/orchestrator/internal/service/authorization.go:42
myrepos/conductortech/prepaid/_git/orchestrator.git/cmd/http/handlers.AuthorizationController.checkAuthorization
	C:/Workspace/prepaid/orchestrator/cmd/http/handlers/authorizations.go:48
net/http.HandlerFunc.ServeHTTP
	C:/Go/src/net/http/server.go:2007
github.com/go-chi/chi.(*Mux).routeHTTP
	C:/Users/marcos.filho/go/pkg/mod/github.com/go-chi/[email protected]+incompatible/mux.go:425
net/http.HandlerFunc.ServeHTTP

Using the second code:

fmt.Printf("%+v\n", err)

print:

myrepos/conductortech/prepaid/_git/orchestrator.git/internal/delivery/cards.(*RestImpl).CardDetails
	C:/Workspace/prepaid/orchestrator/internal/delivery/cards/rest.go:45
myrepos/conductortech/prepaid/_git/orchestrator.git/internal/service.CardServiceImpl.GetCard
	C:/Workspace/prepaid/orchestrator/internal/service/card.go:28
myrepos/conductortech/prepaid/_git/orchestrator.git/internal/service.AuthorizationServiceImpl.GetDetails
	C:/Workspace/prepaid/orchestrator/internal/service/authorization.go:39
myrepos/conductortech/prepaid/_git/orchestrator.git/cmd/http/handlers.AuthorizationController.checkAuthorization
	C:/Workspace/prepaid/orchestrator/cmd/http/handlers/authorizations.go:48
net/http.HandlerFunc.ServeHTTP
	C:/Go/src/net/http/server.go:2007
github.com/go-chi/chi.(*Mux).routeHTTP
	C:/Users/marcos.filho/go/pkg/mod/github.com/go-chi/[email protected]+incompatible/mux.go:425
net/http.HandlerFunc.ServeHTTP

The problem it's happen only if i limit the stack, the correct stack is the second code, But is too Huge, i want to limit the print. But if i limit the code don't print first two lines (in bold), this lines are most important for the stacktrace.

it's a bug ?

from errors.

puellanivis avatar puellanivis commented on August 26, 2024

if i change wraps to WithMessage, the stacktrace dont show.

Yes, WithMessage does not attach stack traces, at all, so you will get no stack traces. But you should still use it instead of errors.Wrap() or errors.WithStack() at each level after the first wrap, otherwise you keep overwriting stacks with a newer stack. Notice:

	C:/Workspace/prepaid/orchestrator/internal/service/authorization.go:42
vs
	C:/Workspace/prepaid/orchestrator/internal/service/authorization.go:39

The authorization.go:42 is where you’re calling the errors.Wrap, or errors.WithStack, not where you are calling the function that produced the error. This is why you are missing the top two layers of your stacktrace, because you’re overriding them with a new stacktrace at each layer.

You need to only attach the stack trace at the very first level. if you do not actually care about the messages, then just use errors.WithStack(err), at rest.go:45. And just return that error at each level, without doing anything to it.

If you still actually do want the messages, then at rest.go:45 use errors.Wrap(err, …), then at each other level use errors.WithMessage(err, …) then, when you go to print the stack, you need to unwrap each layer until you find a stackTracer which is easy given the new errors.As from go1.13:

Do:

                fmt.Println("Cause:")
		fmt.Println(errors.Cause(err))
		var st stackTracer
		if errors.As(err, &st) {
			fmt.Println("Stack Trace:")
			result := fmt.Sprintf("%+v", st.StackTrace()[0:6])
			fmt.Println(result)
		}

If you’re not using go1.13 then use xerrors.As(…).

from errors.

MAAARKIN avatar MAAARKIN commented on August 26, 2024

thx u so much @puellanivis it worked very well. As you said, i was doing wrong, now at the first level i'm using WithStack and after that i'm just return the error, not wrap anymore. And now when i limit the message work very well.

from errors.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.