-
Notifications
You must be signed in to change notification settings - Fork 572
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
Not getting the stack in an error #462
Comments
+1 |
You need to use WithStack() from pkg/errors. The documentation lists Not sure if there are any downsides to the below approach, but you can just wrap the import (
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/pkgerrors"
)
...
zerolog.ErrorStackMarshaler = func(err error) interface{} {
return pkgerrors.MarshalStack(errors.WithStack(err))
} |
I created another issue requesting |
could you explain why we need this? your code just works fine, but why its not in the official code? i couldnt get it to work with the example out of the readme. should we create a PR to add your fix? |
also this doesnt show a full stack of the wrapped errors. it just show the stack trace from defining the {"level":"error","devEui":"000","deviceName":"IOT-ELCMI-0001","applictionName":"Testlab","applicationId":954,"tags":{"productive":"false","usecase":"zaehler","vendor":"landis+gyr"},"pusher":"db","stack":[{"func":"init.0.func1","line":"87","source":"integration_test.go"},{"func":"(*Event).Err","line":"381","source":"event.go"},{"func":"Push.func1.1","line":"55","source":"event.go"},{"func":"goexit","line":"1594","source":"asm_amd64.s"}],"error":"could not push db entry: failed to read sql asset: open sqls/insert-recordings-without-location.sql: file does not exist","time":"2023-01-27T13:32:46Z","message":"error on push"} |
@xsteadfastx, if you use the Example from here:
For example in my tests
|
thank you alot for explaining. it helped me alot to understand it!
…On Sa, 04. Feb 09:18, Toni Pokki wrote:
@xsteadfastx, if you use the `pkgerrors.MarshalStack`, it can only print stack traces for errors that are from `github.com/pkg/errors`. That is, it won't print stack traces to any arbitrary `error` you encounter. In order to add the stack traces to "external errors", wrap them with `errors.Wrap(...)` when you first encounter them.
Example from here:
https://pkg.go.dev/github.com/pkg/errors#hdr-Adding_context_to_an_error
```
_, err := ioutil.ReadAll(r)
if err != nil {
return errors.Wrap(err, "read failed")
}
```
For example in my tests
```
import (
"fmt"
"github.com/pkg/errors"
"github.com/rs/zerolog"
zl "github.com/rs/zerolog/log"
"github.com/rs/zerolog/pkgerrors"
"testing"
)
func TestZl(t *testing.T) {
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
zl.Info().Stack().Err(wrapperError()).Msg("with stack trace")
zl.Info().Stack().Err(fmtError()).Msg("without stack trace")
}
func fmtError() error {
return fmt.Errorf("fmt error")
}
func wrapperError() error {
return errors.Wrap(fmtError(), "wrapped")
}
```
--
Reply to this email directly or view it on GitHub:
#462 (comment)
You are receiving this because you were mentioned.
Message ID: ***@***.***>
|
Im using the package in an AWS serverless codebase and when I force an error Im not seeing the stack trace in the logs. In the init() function I have zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack.
I can see my error message in the log that gets printed its just not printing the stack
The text was updated successfully, but these errors were encountered: