error handling,handle error codes from server, refactoring