Handle and log errors
Add a test to validate error handling