Update apg-tool.py

Handling errors