API: Proper error types instead of String#1424
API: Proper error types instead of String#1424rniii wants to merge 5 commits intoRust-SDL2:masterfrom
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.
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.
The source may also be returned for Self::Sdl
|
bump because this would be mad useful if finished |
Work in progress. I am also taking time to unify all error types, since there were many
SdlErrors 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,WindowBuildErrorandFontErrorin favor ofError::InvalidStringcommon::IntegerOrSdlErrorin favor ofError::IntOverflowset_error_from_code(internal SDL api, shouldn't be here? could add back asSdlErrorCode)NulErrortoError::InvalidString?pref_pathchecking for empty string instead of nullTODO:
TargetRenderError,TextureValueError,UpdateTextureError,UpdateTextureYUVError,AddMappingErrorttf::InitErrorcould be removed, if same ref-counting as subsystems is implemented for itttf::FontErrorwas removed, it might be good to review removingRendererableText, as aCStringis always allocated anyway?String->SdlError, however this is messy becauseRWOpsshould useError, as it currently convertsio::ErrortoString, and that should be avoided