added a ApiError class and added exception handling