-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Make every not exist error unwrappable to a fs.ErrNotExist #20891
Make every not exist error unwrappable to a fs.ErrNotExist #20891
Conversation
The next step following this would be to change |
96d8eaa
to
7b0550a
Compare
A lot of our code is repeatedly testing if individual errors are specific types of Not Exist errors. This is repetitative and unnecesary. `Unwrap() error` provides a common way of labelling an error as a NotExist error and we can/should use this. This PR has chosen to use the common `io/fs` errors e.g. `fs.ErrNotExist` for our errors. This is in some ways not completely correct as these are not filesystem errors but it seems like a reasonable thing to do and would allow us to simplify a lot of our code to `errors.Is(err, fs.ErrNotExist)` instead of `package.IsErr...NotExist(err)` I am open to suggestions to use a different base error - perhaps `models/db.ErrNotExist` if that would be felt to be better. Signed-off-by: Andrew Thornton <art27@cantab.net>
7b0550a
to
a86d070
Compare
…err-a-not-exist-err
Conflicts fixed. |
Signed-off-by: Andrew Thornton <art27@cantab.net>
Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: delvh <dev.lh@web.de>
If the aim is to use this in ServerError why not to create interface to implement for errors that would return status code to be used for this error in response and safe error message to return in response? something like: https://github.com/azugo/azugo/blob/main/errors.go#L16-L26 |
It's tiring to implement the interface by writing lots of type WrapErrNotExist struct{} // TODO: need a better name
func (err WrapErrNotExist) Unwrap() error {
return util.ErrExist
}
// ---
type ErrFooNotExist {
WrapErrNotExist
// ...
}
type ErrBarNotExist {
WrapErrNotExist
// ...
}
|
I agree with "this is in some ways not completely correct as these are not filesystem errors" But :
Why not use our own error system, instead of using |
@zeripath Do you think if it's better to use our own errors instead of fs errors? I agree with your thought that "This is in some ways not completely correct as these are not filesystem errors". At the moment, I can not imagine any benefit of using the fs errors, IMO using fs errors might make code a little confusing. |
Signed-off-by: Andrew Thornton <art27@cantab.net>
I've changed it - the main benefit of using the fs errors was that we might have been able to rely on upstream handling of these errors, but it's fine. |
Whilst I can see your point I'm not sure it's much better than just writing the three-line function. What I'd like to do is get rid of most of these BlahNotExistErrs. We can create a common detailed NotExistErr if necesary: type DetailedNotExist struct {
Resource interface{}
}
func (d DetailedNotExist) Error() string {
// This would use reflection... to reveal the type of the Resource and which non-empty fields are set
}
func (d DetailedNotExist) Unwrap() error {
return NotExistErr
} |
Signed-off-by: Andrew Thornton <art27@cantab.net>
🚀 |
* upstream/main: [skip ci] Updated translations via Crowdin Remove unnecessary misspell ignore pattern (go-gitea#21475) Fix read system configuration bug when installing (go-gitea#21489) Fix viewing user subscriptions (go-gitea#21482) Make every not exist error unwrappable to a fs.ErrNotExist (go-gitea#20891)
A lot of our code is repeatedly testing if individual errors are
specific types of Not Exist errors. This is repetitative and unnecesary.
Unwrap() error
provides a common way of labelling an error as aNotExist error and we can/should use this.
This PR has chosen to use the common
io/fs
errors e.g.fs.ErrNotExist
for our errors. This is in some ways not completelycorrect as these are not filesystem errors but it seems like a
reasonable thing to do and would allow us to simplify a lot of our code
to
errors.Is(err, fs.ErrNotExist)
instead ofpackage.IsErr...NotExist(err)
I am open to suggestions to use a different base error - perhaps
models/db.ErrNotExist
if that would be felt to be better.Signed-off-by: Andrew Thornton art27@cantab.net