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

option to assert validity of results #9

Open
catwell opened this issue Apr 4, 2013 · 2 comments
Open

option to assert validity of results #9

catwell opened this issue Apr 4, 2013 · 2 comments

Comments

@catwell
Copy link
Contributor

catwell commented Apr 4, 2013

I have updated the Python binding to the latest version of nanomsg today. They do something different than us which is interesting: they raise exceptions when the result from nanomsg is unexpected.

We could have a mode that does the same thing, which could even be the default. It would simplify usage of the lib. For instance, the main loop of the echo server currently looks like this:

local cid, err = rep:bind( ADDRESS )
assert( cid, nn.strerror(err) )
local msg, sz
msg, err = rep:recv_zc()
assert( msg, nn.strerror(err) )
echo = msg:tostring()
print( "GOT:", '"' .. echo .. '"' )
sz, err = rep:send( echo, #echo )
assert( sz > 0, nn.strerror(err) )
rep:shutdown( cid )

Note that we don't check the result of nn_shutdown...

With that mode it would look like this:

local cid = rep:bind( ADDRESS )
local msg = rep:recv_zc()
echo = msg:tostring()
print( "GOT:", '"' .. echo .. '"' )
local sz = rep:send( echo, #echo )
rep:shutdown( cid )

Thoughts?

EDIT: I can implement it, I just want to know what you think about the idea doing it and submitting a PR.

@neomantra
Copy link
Contributor

Definitely looks nicer, and I do appreciate exceptions in C++, Ruby, and Python. They aren't so common in Lua though.

Can you paste what you would wrap around the second code sample to catch the error? Wrap it in a function and pcall it?

@catwell
Copy link
Contributor Author

catwell commented Apr 4, 2013

Yes. I would not use that style everywhere, sometimes you need fine-grained error handling, this is why I want that as an option. But sometimes you just want things to fail if an error happens. For instance a web application calling a service over nanomsg could log the error and return a generic HTTP 500 error to users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants