android: fixed once and on error handling

This commit is contained in:
Salakar
2017-03-07 16:54:04 +00:00
parent 677b1d2836
commit 214928ba7f
9 changed files with 81 additions and 48 deletions

View File

@@ -213,17 +213,41 @@ export default class Database extends Base {
}
/**
*
* @param err
* Converts an native error object to a 'firebase like' error.
* @param error
* @returns {Error}
* @private
*/
_handleDatabaseError(err: Object) {
const body = err.body || {};
const { path, modifiersString, eventName, msg } = body;
const handle = this._handle(path, modifiersString);
_toFirebaseError(error) {
const { path, message, modifiers, code, details } = error;
let firebaseMessage = `FirebaseError: ${message}`;
this.log.debug('_handleDatabaseError ->', handle, eventName, err);
if (path) {
firebaseMessage = `${firebaseMessage}\r\nPath: /${path}\r\n`;
}
if (this.errorSubscriptions[handle]) this.errorSubscriptions[handle].forEach((cb) => cb(new Error(msg)));
const firebaseError = new Error(firebaseMessage);
firebaseError.code = code;
firebaseError.path = path;
firebaseError.details = details;
firebaseError.modifiers = modifiers;
return firebaseError;
}
/**
*
* @param error
* @private
*/
_handleDatabaseError(error: Object = {}) {
const { path, modifiers } = error;
const handle = this._handle(path, modifiers);
const firebaseError = this._toFirebaseError(error);
this.log.debug('_handleDatabaseError ->', handle, 'database_error', error);
if (this.errorSubscriptions[handle]) this.errorSubscriptions[handle].forEach(listener => listener(firebaseError));
}
}

View File

@@ -94,33 +94,54 @@ export default class Reference extends ReferenceBase {
});
}
on(eventName: string, cb: () => any, errorCb: () => any) {
if (!isFunction(cb)) throw new Error('The specified callback must be a function');
if (errorCb && !isFunction(errorCb)) throw new Error('The specified error callback must be a function');
/**
*
* @param eventType
* @param successCallback
* @param failureCallback
* @param context TODO
* @returns {*}
*/
on(eventType: string, successCallback: () => any, failureCallback: () => any, context) {
if (!isFunction(successCallback)) throw new Error('The specified callback must be a function');
if (failureCallback && !isFunction(failureCallback)) throw new Error('The specified error callback must be a function');
const path = this._dbPath();
const modifiers = this.query.getModifiers();
const modifiersString = this.query.getModifiersString();
this.log.debug('adding reference.on', path, modifiersString, eventName);
return this.db.on(path, modifiersString, modifiers, eventName, cb, errorCb);
this.log.debug('adding reference.on', path, modifiersString, eventType);
return this.db.on(path, modifiersString, modifiers, eventType, successCallback, failureCallback);
}
once(eventName: string = 'once', cb: (snapshot: Object) => void) {
/**
*
* @param eventType
* @param successCallback
* @param failureCallback
* @param context TODO
* @returns {Promise.<TResult>}
*/
once(eventType: string = 'value', successCallback: (snapshot: Object) => void, failureCallback: (error: Error) => void) {
const path = this._dbPath();
const modifiers = this.query.getModifiers();
const modifiersString = this.query.getModifiersString();
return promisify('onOnce', FirebaseDatabase)(path, modifiersString, modifiers, eventName)
return promisify('once', FirebaseDatabase)(path, modifiersString, modifiers, eventType)
.then(({ snapshot }) => new Snapshot(this, snapshot))
.then((snapshot) => {
if (isFunction(cb)) cb(snapshot);
if (isFunction(successCallback)) successCallback(snapshot);
return snapshot;
})
.catch((error) => {
const firebaseError = this.db._toFirebaseError(error);
if (isFunction(failureCallback)) failureCallback(firebaseError);
return Promise.reject(failureCallback);
});
}
off(eventName?: string = '', origCB?: () => any) {
off(eventType?: string = '', origCB?: () => any) {
const path = this._dbPath();
const modifiersString = this.query.getModifiersString();
this.log.debug('ref.off(): ', path, modifiersString, eventName);
return this.db.off(path, modifiersString, eventName, origCB);
this.log.debug('ref.off(): ', path, modifiersString, eventType);
return this.db.off(path, modifiersString, eventType, origCB);
}
/**