Angular & TypeScript: Class instance check doesn't work -
i'm trying check type of error being returned web service. problem instanceof check doesn't work. none of if work.
component class method triggers call ws:
public performsearch(keyword: string) { this.searchservice.searchwithquery(keyword) .subscribe( response => { this.searchresults = json.parse(response._body); }, (error: apperror) => { console.log('error instance'); console.log(error); if (error instanceof notfounderror) { this.snackbar.open('404 not found', 'x', { duration: 5000, }); } if (error instanceof accessdeniederror) { this.snackbar.open('401 access denied', 'x', { duration: 5000, }); } if (error instanceof badinputerror) { this.snackbar.open('400 bad input', 'x', { duration: 5000, }); } if (error instanceof apperror) { this.snackbar.open('general error', 'x', { duration: 5000, }); } }); } search.data.service class extending base service class:
searchwithquery(query: string) { let headers = new headers(); headers.append('authorization', 'bearer ' + this.tokenvalue); return this.http.get(this.gatewayurl + this.servicename + this.withquery + query, { headers: headers }) .catch(this.handleerror); } private handleerror(error: response): errorobservable { if (error.status === 404) return observable.throw(new notfounderror(error.json())); if (error.status === 400) return observable.throw(new badinputerror(error.json())); if (error.status === 401) { return observable.throw(new accessdeniederror(error.json())); } return observable.throw(new apperror(error.json())); } apperror.ts class
export class apperror { constructor(public originalerror?: any) { } } and rest of error classes extend it, f.e.:
export class accessdeniederror extends apperror { } when trying log error of apperror in console odd:
typeerror: __webpack_imported_module_2_rxjs_observable__.observable.throw not function @ catchsubscriber.webpackjsonp.../../../../../src/app/common/services/search.data.service.ts.searchdataservice.handleerror [as selector] (search.data.service.ts:52) @ catchsubscriber.webpackjsonp.../../../../rxjs/operator/catch.js.catchsubscriber.error (catch.js:104) @ xmlhttprequest.onload (http.es5.js:1231) @ zonedelegate.webpackjsonp.../../../../zone.js/dist/zone.js.zonedelegate.invoketask (zone.js:424) @ object.oninvoketask (core.es5.js:3881) @ zonedelegate.webpackjsonp.../../../../zone.js/dist/zone.js.zonedelegate.invoketask (zone.js:423) @ zone.webpackjsonp.../../../../zone.js/dist/zone.js.zone.runtask (zone.js:191) @ zonetask.webpackjsonp.../../../../zone.js/dist/zone.js.zonetask.invoketask [as invoke] (zone.js:498) @ invoketask (zone.js:1370) @ xmlhttprequest.globalzoneawarecallback (zone.js:1388)
as error message says, error none of above typeerror, , caused fact there's no observable.throw method.
unless entire rxjs package imported import 'rxjs/rx', throw should imported explicitly, other rxjs operator or observable method:
import 'rxjs/add/observable/throw'; this kind of errors supposed detected during typescript compilation, there no need debug them @ runtime.
Comments
Post a Comment