added proper error reporting with error codes and messages