Added onError

Work in progress. Needs tests.