-
Notifications
You must be signed in to change notification settings - Fork 474
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
API: Proper error types instead of String #1424
base: master
Are you sure you want to change the base?
Conversation
I think I noted everything down, I'll leave actually changing all strings when I'm given the green light! It is a breaking change, however migrating code should be pretty easy: |
Self::Sdl(msg) => write!(f, "SDL error: {msg}"), | ||
Self::Io(err) => write!(f, "IO error: {err}"), | ||
Self::IntOverflow(name, value) => write!(f, "Integer '{name}' overflows: {value}"), | ||
Self::InvalidString(name, nul) => write!(f, "Invalid string '{name}': {nul}"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the definition the first field is the NulError
, not the name.
To prevent these bugs (in this crate but also user code using this crate) I advise to use a struct like variant instead of a tuple like one. This also adds context to the context-less &'static str
field. I suggest to change the definition to InvalidString { nul_err: NulError, var_name: &'static str }
(or something like this) and roughly the same for IntOverflow
.
impl error::Error for Error { | ||
fn source(&self) -> Option<&(dyn error::Error + 'static)> { | ||
match self { | ||
Self::Sdl(..) | Self::IntOverflow(..) => None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The source may also be returned for Self::Sdl
Work in progress. I am also taking time to unify all error types, since there were many
SdlError
s before. I would like to discuss if compatibility is necessary, e.g. it could be a feature-flag that doestype SdlError = String;
See also: #1053
Summary:
OpenUrlError
,PrefPathError
,ShowMessageError
,WindowBuildError
andFontError
in favor ofError::InvalidString
common::IntegerOrSdlError
in favor ofError::IntOverflow
set_error_from_code
(internal SDL api, shouldn't be here? could add back asSdlErrorCode
)NulError
toError::InvalidString
?pref_path
checking for empty string instead of nullTODO:
TargetRenderError
,TextureValueError
,UpdateTextureError
,UpdateTextureYUVError
,AddMappingError
ttf::InitError
could be removed, if same ref-counting as subsystems is implemented for itttf::FontError
was removed, it might be good to review removingRendererableText
, as aCString
is always allocated anyway?String
->SdlError
, however this is messy becauseRWOps
should useError
, as it currently convertsio::Error
toString
, and that should be avoided