In every documentation, I read something like that for sentinel errors:
Sentinel errors are usually used to indicate that you cannot start or proceed.
That could also be the case for any possible error, isn't it? Because anything unexpected can happen in Runtime. Does it mean errors that I expect in Runtime but can or should handle better, do I call sentinel errors?
Then I read how they should be used:
Sentinel errors are among the few variables declared at the package level. Their names start with
Err(Exceptionio.EOF). They should be threatened as read-only. (Go compiler cannot enforce this).
Before you define a sentinel error, make sure you need one. Once defined, it becomes part of your public API, and you have committed to making it available in all future backwards-compatible releases.
Or does the way I handle them make them sentinel errors? Could you give me an example to understand clearly, what the difference is?
Would it be wrong to say: Errors, I want to stand sentinel over in Runtime, and I explicitly define in my package root as variables (or constants) are sentinel errors?
I've prepared an example; maybe we can use it as a basis: https://go.dev/play/p/qwi4ligYZYh
The idea of a sentinel error is semantic more than computer science. The Digital Ocean tutorial on Error handling in go gives a very good explanation of what sentinel errors are (and is where I learned).
Sentinel errors are user defined errors that indicated very specific events that you, as a developer, anticipate & identify as adequately important to define and specify. As such, you declare them at the package level and, in doing so, imply that your package functions may return these errors (thereby committing you in the future to maintain these errors as others depending on your package will be checking for them).
Ex:
var VeryImportantError = fmt.ErrorF("Something very specific happened!")
[...]
if err == VeryImportantError {
// handle very specific error in very specific way
}
In general these are errors are "usually used to indicate that you cannot start or proceed."
In short, sentinel errors are not special from a compiler perspective. Rather, they are a way of conceptualizing certain, specific, errors that the developer wants to handle in specific ways because they signal specific things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With