Skip to content
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

Different approach to error reporting #5

Closed
srdja opened this issue Mar 15, 2016 · 0 comments
Closed

Different approach to error reporting #5

srdja opened this issue Mar 15, 2016 · 0 comments
Assignees

Comments

@srdja
Copy link
Owner

srdja commented Mar 15, 2016

Curretnly all errors are reported by returning an error code, which is in itself not a problem, however if a function should also return a value, problems arise. One is that the error codes have to share the type with the return value and second, the error codes limit the range of valid values that can be returned from a function.

A better approach would be to have the function return value reserved for errors, while havnig the function result returned through a parameter.

For example:

int foo_new(struct foo *f, int baz) {
    if (baz) {
        *f = (struct foo) {.bar = 3};
         return OK;
    } else {
        // something went wrong
         return ERR;
    }
} 

This way the caller can check the return code to see if the value was set or not before using it.

If we did the inverse where the result is returned through function return and the error code set through a parameter like so:

struct foo foo_new(int baz, int *status) {
    if (baz) {
        *status = OK;
        return (struct foo) {.bar = 3};
    } else {
        // something went wrong
        *status = ERR;
        return ??? // What do we return?
    }
} 

We wouldn't be able to handle failures properly.

The return error / set result may not be ergonomic as the original way, but it allows for more flexibility in error reporting.

@srdja srdja self-assigned this Mar 15, 2016
@srdja srdja changed the title Descriptive error codes needed Different approach to error reporting Mar 16, 2016
@srdja srdja closed this as completed Mar 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant