-
Notifications
You must be signed in to change notification settings - Fork 701
Obtaining only message #93
Comments
Can I suggest an alternative? Have your errors implement a specific interface, and then when you respond to the client check if the error satisfies that interface and respond appropriately. Here's a short example of what I mean: https://play.golang.org/p/xpl-71EKbj |
Maybe It feels like you are trying to create a side channel to send extra data related to the path the code took to the site the error was generated. You may be better served by writing your own error type. See also #34 |
I found myself wanting this as well. In my case I was wrapping my errors with an additional message. My http service has a single point of logging errors, and to avoid sending too much information (some that may be private) to the http client, I'd like to just extract the friendly the message as well, but log the stack trace. So I can do it using my own event type, but then I'm supply 2 messages which is redundant. Of course I could be doing it all wrong as well, but in my case a Wrap without requiring a message could be an alternative. |
A wrap without a message is called WithStack. Is that useful for your use case? |
Yes, sorry for the noise, I should have dug a bit deeper first. Just tried this out for my use case and it gives me exactly what I need. Thanks. |
I've been in the same situation as @gudmundur. I love this package, I'm using it in all my project. So first, thank you. My use case is the following:
I'm using What I found too bad is that doing What about treating I can try to come up with a PR if you think that's a good idea. |
I’m sorry, I don’t think what you are doing is correct according to the philosophy of his package.
I think you should not use wrap to wrap a predeclared error, but instead use errors.Errorf at the point where you return the error.
… On 9 Dec 2017, at 08:17, tehmoon ***@***.***> wrote:
Hi guys,
I've been in the same situation as @gudmundur. I love this package, I'm using it in all my project. So thank you first.
My use case is the following:
var ErrNotFound = errors.New("")
func fn(f string) (error) {
return errors.Wrapf(ErrNotFound, "The file %s has not been found", f)
}
func main() {
err := fn("/nonexistant/blah")
if err != nil {
log.Fatal(err.Error())
}
}
I'm using ErrNotFound just to contextualize the error, meaning I can later do error.Cause() to get the underlying error and match it to ErrNotFound variable.
What I found too bad is that doing var ErrNotFound error which will set it to nil and Wrap() -- or others -- will return nil causing the error to never enter the condition.
What about treating Wrap(nil, "message").Error() like if "message" was the latest string which won't have the last :?
I can try to come up with a PR if you think that's a good idea.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Hi @davecheney thank you for answering this fast! Yes you are right, that's what I ended up using in most cases. But I am still leaning towards treating the root cause as a variable that I could compare to something without having to declare a new struct every time I have a new "simple" error. I really like for example the I'm also sure that I'm doing things wrong, I'll try to look at some code to really understand how to do errors. I actually discover your package through your blog entry: https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully which totally helped me a lot already. |
The thing is this errors package goes out of its way to make it hard (sometimes impossible) to compare two errors by value. If you do want to do that, I would use the stdlib errors package to create your error value, then wrap it with this packages errors.Wrap method, or perhaps use errors.WithStack which sounds like what you want to do.
… On 9 Dec 2017, at 14:06, tehmoon ***@***.***> wrote:
Hi @davecheney thank you for answering this fast!
Yes you are right, that's what I ended up using in most cases. But I am still leaning towards treating the root cause as a variable that I could compare to something without having to declare a new struct every time I have a new "simple" error.
I really like for example the "io" package where you can simply compare the error interface against the variables that are declared in the package. But I loose the "printf" ability though and was trying to find a compromise since I didn't want to return the message from the underlying error but from where it came last.
I'm also sure that I'm doing things wrong, I'll try to look at some code to really understand how to do errors. I actually discover your package through your blog entry: https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully which totally helped me a lot already.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Ok I'll think about it more and give it a try, maybe with a new approach. Thank you so much for taking the time. |
You’re welcome.
Since I wrote this package several years ago there has been a lot of discussion about the way that errors should be handled. This package is unapologetically a reflection of my views at the time and it’s unlikely to change at this point. However this does not mean that how this package operates is Right (if there is such an absolute) so it’s important to say that if this package doesn’t fit your requirements, you shouldn’t feel you have to use it.
… On 9 Dec 2017, at 14:35, tehmoon ***@***.***> wrote:
Ok I'll think about it more and give it a try, maybe with a new approach. Thank you so much for taking the time.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Yes, I have had a few people tell me, “but the standard library does XY” and I’m like, well… it was written in an earlier time, and views have changed about the best way to do XY now… Of course, I still think the “oob” conditions (io.EOF) being applied in the error namespace is the biggest example of this, with option applications second. |
Jumping in here because I haven't seen anyone mention my use-case. I would find being able to access the message useful for testing. i.e:
It would be nice to be able to treat Would this be a valid case, or am I misusing this library? |
I think in this case you are attempting to fit a square peg in a round hole. If you wish to treat Testing that |
Yeah, what you've described is the option I've settled for. My only qualm with it is that, in the non-contrived version of this scenario, I actually have multiple "things" that may error. So, I feel like it would be nice to assert that the correct error was raised, rather than just an error. That said, if I were truly blackbox testing ¹ Unless I were doing TDD to prescribe the errors that my function returns. -- Edit: I've since decided that this is the wrong approach. From the standpoint of my test, all I should know about the function-under-test is:
If I have multiple "things" that are all calling the same function on the stub, I might need to build a more advanced stub that is capable of handling many input values and only erroring for the ones that match the scenario I'm testing. Anything more than that, and I'm creeping into whitebox testing. Also, the function under test should be free to modify its error messages without breaking the test. |
Is it possible to get the message passed to
errors.Wrap(err, message)
? What I specifically mean ismessage
itself, without anything fromerr
.My use case is that I wrap underlying errors, and pass in a human friendly message, that I would like to expose to customers via our APIs. Currently I'm parsing the message out by splitting by
:
, which is brittle.The text was updated successfully, but these errors were encountered: