handle errors correctly

fixes #3
add error cases to the test
fix error handling
always return `undefined`