Comments (6)
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.
@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.
@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.
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.
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.
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)
- Proposal: add errors.Is and errors.As
- RFC: Allow custom StackTraces
- func Is() does not works with errors.Wrap or errors.Wrapf HOT 3
- Cause() behavior has changed HOT 11
- respect -trimpath build flag HOT 3
- Unwrap doesn't return the base error HOT 7
- Questions about extending annotations HOT 5
- undefined errors.Wrap HOT 2
- How to change format return wrapped error message HOT 2
- errors.Wrap() created bugs in our codebase. HOT 3
- StackTrace.Format with "%+v" includes a leading newline HOT 1
- A new error caused by another error HOT 7
- Demo
- Change Wrap() to return an error even if we provide nil HOT 3
- return *stack instead of stack HOT 2
- Wrap() duplicates call stack HOT 6
- Elegant way to check error is wrapped HOT 4
- `Errorf` should be able to wrap errors HOT 4
- Package errors looking for new maintainers HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from errors.