-
Notifications
You must be signed in to change notification settings - Fork 3
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
Add document about error methods #17
Conversation
Thanks for creating this! I wanna discuss this at dconf! |
Incidentally, I don't know if I'm on board... maybe I am... I really don't know. I'd like to expand this document to detail the options considered, and the arguments for/against. |
yes we can talk about it at Dconf. bool decodeImage(out ImageBuffer buffer, out string errorMessage)
{
// check SOI marker
if (!expectMarker(0xFFD8))
{
errorMessage = "no SOI marker";
return false; // alternatively: buffer = ImageBuffer.init;
}
// bla...
} There is 3 piece of information to return: the buffer, an error message, and whether the operation succeeded (could use With Result!ImageBuffer decodeImage()
{
// check SOI marker
if (!expectMarker(0xFFD8))
{
return Result!ImageBuffererror.fail("no SOI marker");
}
// bla...
} Also if Result can own a string, it would lead the path towards error strings that can be |
I returned the string in my functions, and string decodeImage(out ImageBuffer buffer)
{
// check SOI marker
if (!expectMarker(0xFFD8))
return "no SOI marker";
// bla...
} Yeah, I don't really wanna think about string allocation for dynamic messages right now :/ |
To be clear, I'm not saying this is great... but that it's functional, it's theoretically faster, and it doesn't require bonus machinery. |
I agree those are the pros and I will add one:
I would say the cons are:
For library public API, readability might be more important than efficiency, at least to some degree. I guess that is the main point around which we need to agree on. |
This is clever but then the correct way to test for success is to write: if (!decodeImage(buffer))
{
// success
} which is a bit like |
One more variant is to make Pros:
Cons:
|
Whatever method we come up with, I would hope that it is only transitory until -betterC gets exceptions. My preference would be |
While I agree, it seems there are no plans that -betterC ever gets exceptions (we asked the question several times). I've updated the document to account for the discussion at DConf.
PLEASE VOTE |
I'm fine with either approach but please use error codes instead of just strings. I can't imagine having to check for string equality to determine why the function failed and branch my own code accordingly. Same with bool out params, if the function can fail for a variety of different reasons then it should return an int and it should be documented properly what the error codes mean. |
We probably should add int error codes, but note that in png reader at least there are basically 2 types of errors: out of memory and png file is invalid. The message why is it invalid could be useful to the human to read but there is nothing you can really code based on that reason. |
@ahmetsait OK but then we declare a special return value WG_NO_ERROR == 0 for no error? (like other libraries do?). Good idea about Error vs Exception which is the dark side of error codes. @igor84 yeah we already get an irrecoverable error and a recoverable one so better distinguish them like @ahmetsait suggested, which is neat. <0 is Error, > 0 is Exception, == 0 is no error... (EDIT: alternative way is to crash on Error, which is to me the right thing to do) Note that Should |
Well in this case you really wanted a |
In theory error codes are nice because you can handle each error individually, and you don't have to be afraid of anything breaking because of changing of the error msg string (or potential i18n). The (from my perception) common solution for exception handling in D is to not create specific exception subtypes, but just go for one (or none) type and just differentiate the specific exceptional cases by a meaningful message. |
I think is the current state of the consensus. Destroy!