import { Response } from '@angular/http' ; import { Observable } from 'rxjs' ; import { AngularFireList , AngularFireObject } from 'angularfire2/database' ; const extractError = ( error ) : string => { let errMsg ; if ( error instanceof Response ) { const body = error . json ( ) || '' ; const err = body . error || JSON . stringify ( body ) ; errMsg = `${ error . status } - ${ error . statusText || '' } ${ err }` ; } else { errMsg = error . message ? error . message : error . toString ( ) ; } console . error ( errMsg ) ; return errMsg ; } ; export abstract class BaseService { protected handlePromiseError ( error ) { return Promise . reject ( extractError ( error ) ) ; } protected handleObservableError ( error ) { return Observable . throw ( extractError ( error ) ) ; } mapListKeys < T > ( list ) { return list . snapshotChanges ( ) . map ( actions => actions . map ( action => ( { $key : action . key , ... action . payload . val ( ) } ) ) ) ; } mapObjectKey < T > ( object ) { return object . snapshotChanges ( ) . map ( action => ( { $key : action . key , ... action . payload . val ( ) } ) ) ; } }