angularFire2 - ISSUES with error handling, exception handling ?? :'(
AngularCli 1.0.0-beta.15 Angular 2.0.0 (stable) angularFire2 2.0.0-beta5 Windows 10 Chrome
When valid value is provided
_af ---> AngularFireDatabase
this._af.list('attendees').push(someValidData)
.then(resolve => {
console.log('all good');
}, reject => {
console.log('error');
})
.catch(reject => {
console.log('catch');
});
Console logs: all good
When INVALID value is provided
this._af.list('someList').push(null)
.then(resolve => {
console.log('all good');
}, reject => {
console.log('error');
})
.catch(reject => {
console.log('catch');
});
error_handler.js:47 ORIGINAL EXCEPTION: this._af.list(...).push(...).then is not a function Then my angular2 app does some weird posting or refresh of the page
I have not seen found any of the documentation with examples of proper error handling.
According to https://firebase.google.com/docs/database/web/save-data And since angular2 and angularFire2 implement use of Observables. I tried doing
let result = this._af.list('someList').push(someData);
return Observable.fromPromise(result);
But typescript tells me...
Argument of type 'ThenableReference' is not assignable to parameter of type 'Promise
Thanks for any help/guidance provided
Regards, e. Vazquez
Please provide a complete and minimal repro (the minimal is very important) demonstrating the problem. We won't be much help with something this nuanced unless we can fiddle with the code under the same conditions.
Mr. Sir @katowulf
Repo https://github.com/edoardoVD/angularFireExpHandling
fiddle with this part the data being sent on
push({some: 'data'}) push(null)
export class AppComponent {
title = 'app works!';
constructor(private _af: AngularFire) {
console.log('app component constructor');
}
ngOnInit() {
console.log('testing here');
this._af.database.list('items').push({some: 'data'})
.then(resolve => {
console.log('all good');
}, reject => {
console.log('error');
})
.catch(reject => {
console.log('catch');
});
}
}
Thanks
I pulled the repo but it threw errors when I went to run it (typical ng2 build stuff)
Can you log out what happens when you do .push(null) ?
var pushTest = this._af.database.list('items').push(null);
console.log('push test:', pushTest);
console.log(typeof pushTest);
If you pass an empty .push() I know it uses the root firebase push method
but in my experience it always returns at least a $ref
@DennisSmolek , @katowulf
ngOnInit() {
console.log('testing here');
var pushing = this._af.database.list('items').push(null);
console.log('push test', pushing);
console.log(typeof pushing);
}
from the looks of it the catch func and then funcs are Undefined, I guess thats the problem... . ?_?, it seems that when the exception is thrown no call backs are attached who knows.... may its the cyber leprecauns ...
If I try to do an empty push(), typescript cries about it, and reports a transpile error
Howdy @katowulf, don't mind my example, but can you guys provide a simple working demonstration of angularfire2 exception handling on the documentation.
Thanks :-)
Can this be reproduced on the latest version of AngularFire2?
I don't understand what you're asking for with exception handling. The methods return promises and you use catch to handle errors. Do you need something beyond this?
@katowulf update to the latest fire fire ("angularfire2": "2.0.0-beta.6")
The link you sent me is for data as objects, and that works ok The issue is with data as lists
var pushingToList = this._af.database.list('items').push(null);
console.log('push list test', pushingToList);
var settingObject = this._af.database.object('someObj').set(null);
console.log('push object test', settingObject);
settingObject
.then(() => {
console.log('setting Object OK');
}).catch(e => {
console.log('Catched object set:' + e.message);
})
Console output: Observe the "push list test"
I've updated the repo with the example https://github.com/edoardoVD/angularFireExpHandling
Again, this is returning Firebase.database.ref.push()'s return value, which is a ThenableReference. The link I provided was just an example of using try/catch off of these methods.

From what I can tell, this looks like a specific behavior of the SDK when you pass null into the push() method. It returns a reference without a promise; because nothing is going to get saved to the Database; there's no async operation to chain.
@katowulf sounds good then, thanks for explaining, I'll just make sure I don't push nulls to the lists But I would think it would be beneficial getting some sort of exception when doing invalid operations
Keeping this open for discussion. I think we can at least fix the promise chain by wrapping nulls with Promise.resolve() or something similar.
However this sort of just passes the buck (as your promise will resolve to null or an object now, rather than just breaking at the promise level), so it's worth some deliberation before deciding to address it here. I've also submitted this as a bug against the SDK (hopefully we can fix it at the source).
@katowulf more on the exception handling that I had forgotten but now that I'm revising my code and making sure I handle errors, I have the following
let dataToSave = { fieldone: 'one', fieldtwo: 'two', fieldthree: undefined };
this._af.database.object('somePath').update(dataToSave)
.then(() => {
console.log('all good')
})
.catch(err => {
console.log(err);
});
fieldthree has "undefined" value
Error stack trace
error_handler.js:51Error: Firebase.update failed: First argument contains undefined in property 'somePath.fieldthree'
at Ef (firebase.js:347)
at firebase.js:350
at Cb (firebase.js:267)
at Gf (firebase.js:350)
at U.h.update (firebase.js:473)
at FirebaseObjectObservable.update (firebase_object_observable.js:30)
at AppComponent.ngOnInit (app.component.ts:18)
Meanwhile, I'm just making sure that the data to be saved has no undefined values But I would think that once push gets invoked, the catch callback would pick up the exception for me to handle, but it's not
I'm not sure if the code is wrong or the description, but your code has .update() and you mentioned "once push gets invoked". Undefined should throw an error, btw. If that's not being caught, it may be worth filing a separate issue to address.
Closing this in favor of #668 for tracking a fix; a potential workaround is just to wrap the return value in promise.resolve:
Promise.resolve( this._af.list('items').push(<anything>) ).then(...);
You should check this stackoverflow http://stackoverflow.com/questions/39300276/firebase-3-angularfire-2-cannot-catch-error-when-trying-to-set-firebase
The catch() clause is called when the write fails on the server, so when the security rules reject the operation.
When you're passing in undefined, the operation already fails on the client. You could probably try catch it, but it's way better to simply detect the undefined and not write: