refactor invoices controller for error handling