added error handling with error code and error message