Fix error handling for invalid ObjectIds