Handler errors in cb or at return value

37 views
Skip to first unread message

Bodo Kaiser

unread,
May 12, 2013, 3:49:37 AM5/12/13
to li...@googlegroups.com
Hello,

I would like to know if it is better to handle errors be the return value:

int r = uv_fs_open(loop, &open_req, "file", O_RDONLY, S_IRUSR, open_cb);

if ( r) {
fprintf(stderr, "Error at opening file: %s.\n", uv_strerror(uv_last_error(loop)));
}

or:

uv_fs_open(loop, &open_req, "file", O_RDONLY, S_IRUSR, open_cb);

void open_cb(uv_fs_t* req) {
int result = req->result;

if (result != -1) {
fprintf(stderr, "Error at opening file: %s.\n", uv_strerror(uv_last_error(loop)));
}

uv_fs_cleanup_req(req);
}

or is it recommend to do both?

Bodo

Ben Noordhuis

unread,
May 12, 2013, 9:04:54 AM5/12/13
to li...@googlegroups.com
uv_fs_open() and friends return an error immediately when:

a) initialization fails, probably because of UV_ENOMEM, or
b) you invoke it synchronously (cb==NULL) and the operation fails

In all other cases, the return value is zero; any errors are passed on
to your callback.

bodo....@enabre.com

unread,
May 20, 2013, 2:29:56 AM5/20/13
to li...@googlegroups.com
Forgotten to subscribe...

Thank you for your answer. So I try to handle both errors.

Bodo
Reply all
Reply to author
Forward
0 new messages