Added support for error handling via onError and onFinal.