UNPKG

ehandler

Version:

Exception handler for Angular Applications

2 lines 5.31 kB
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports,require("@angular/core"),require("@angular/common/http"),require("rxjs/operators"),require("toastr"),require("@angular/platform-browser/animations")):"function"==typeof define&&define.amd?define("ehandler",["exports","@angular/core","@angular/common/http","rxjs/operators","toastr","@angular/platform-browser/animations"],o):o(e.ehandler={},e.ng.core,e.ng.common.http,e.rxjs.operators,null,e.ng.platformBrowser.animations)}(this,function(e,t,s,i,r,n){"use strict";var a=function(){function e(e,o){this.http=e,this.config=o}return e.prototype.sendMessage=function(e){this.http.post(this.config.hook,e,{observe:"response",responseType:"text"}).subscribe(function(e){console.info("Message sent")})},e.prototype.exceptionNotify=function(e,o){var t=this;void 0===o&&(o="");var r={text:"--------------------------[ "+(e.status?e.status:"Application Error")+" ]--------------------------",attachments:[{author_name:"Cause: "+o+"\nURL: "+window.location.href,title:"Message: "+e.message,text:"Date: "+new Date+"\nUserAgent: "+navigator.userAgent+"\n---------------------------------------------------------"}]};if(!this.bugged){var n=new s.HttpHeaders({"Content-Type":"text/plain"});this.http.post(this.config.hook,JSON.stringify(r),{observe:"response",responseType:"text",headers:n}).pipe(i.catchError(function(e){throw t.bugged=!0,console.log("Hook stopped working"),e})).subscribe(function(e){console.log("Exception Sent")})}},e.decorators=[{type:t.Injectable}],e.ctorParameters=function(){return[{type:s.HttpClient},{type:undefined,decorators:[{type:t.Inject,args:["configurations"]}]}]},e}(),c=function(){function e(e,o,t){this.injector=e,this.config=o,this.hook=t}return e.prototype.handleError=function(e){var o,t=new Date;if(e instanceof s.HttpErrorResponse){switch(o=this.config.message.statusException,e.status){case 400:case 401:this.logout();break;case 500:o=this.config.message.serverException}this.statusMessage(e,t)}else this.commonMessage(e,t),o=this.config.message.appException;this.errorMSG(o)},e.prototype.logout=function(e){var o=this;r.error(this.config.message.unAuthenticated),e&&r.error(e),setTimeout(function(){window.location.replace(o.config.logout)},5e3)},e.prototype.errorMSG=function(e,o){void 0===o&&(o=null),r.error(e,o)},e.prototype.statusMessage=function(e,o){void 0===o&&(o=new Date),console.error("--------------------------------------------\n","Backend returned status code: "+e.status+"\n","Response body ehandler:"+e.message+"\n",o+"\n","--------------------------------------------"),this.hook.exceptionNotify(e.message,"----HTTP Error----")},e.prototype.commonMessage=function(e,o){void 0===o&&(o=new Date),console.error("--------------------------------------------\n","An error occurred ehandler:"+e.message+"\n",o+"\n","--------------------------------------------"),this.hook.exceptionNotify(e.message,"----Common Error----")},e.decorators=[{type:t.Injectable,args:[{providedIn:"root"}]}],e.ctorParameters=function(){return[{type:t.Injector},{type:undefined,decorators:[{type:t.Inject,args:["configurations"]}]},{type:a}]},e.ngInjectableDef=t.defineInjectable({factory:function(){return new e(t.inject(t.INJECTOR),t.inject("configurations"),t.inject(a))},token:e,providedIn:"root"}),e}(),u=function(){function e(e,o){this.config=e,this.eh=o}return e.prototype.intercept=function(e,o){var n=this;return o.handle(e).pipe(i.map(function(e){if(e instanceof s.HttpResponse){var o=e.body;if(o)if(o.exception&&o.exception.code)try{if(n.config&&n.config.logoutCodes&&-1!==n.config.logoutCodes.indexOf(o.exception.code))n.eh.logout();else{var t={code:o.exception.code,message:o.exception.message};n.eh.errorMSG(t.message,t.code),n.eh.statusMessage(t)}}catch(r){console.log("There was an error while trying to send message")}else if(o.error&&o.error.code)try{if(n.config&&n.config.logoutCodes&&-1!==n.config.logoutCodes.indexOf(o.error.code))n.eh.logout();else{t={code:o.error.code,message:o.error.message};n.eh.errorMSG(t.message,t.code),n.eh.statusMessage(t)}}catch(r){console.log("There was an error while trying to send message")}}return e}),i.catchError(function(e){throw n.eh.handleError(e),e}),i.finalize(function(){}))},e.decorators=[{type:t.Injectable}],e.ctorParameters=function(){return[{type:undefined,decorators:[{type:t.Inject,args:["configurations"]}]},{type:c}]},e}(),p={message:{unAuthenticated:"Xahiş olunur sistemə daxil olun!",statusException:"Status xətası",serverException:"Server xətası",appException:"Sistem xətası"},logout:"/logout",logoutCodes:[401,1005,1006,1014,1015,1016,1017,1401],hook:"https://hooks.slack.com/services/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"},o=function(){function o(){}return o.forRoot=function(e){return void 0===e&&(e=p),{ngModule:o,providers:[{provide:"configurations",useValue:e}]}},o.forChild=function(){return{ngModule:o}},o.decorators=[{type:t.NgModule,args:[{imports:[s.HttpClientModule,n.BrowserAnimationsModule],providers:[{provide:t.ErrorHandler,useClass:c},{provide:s.HTTP_INTERCEPTORS,useClass:u,multi:!0},a],declarations:[],exports:[]}]}],o}();e.EHandlerService=c,e.EIntercepterService=u,e.InitialConfig=p,e.EHandlerModule=o,e.ɵb=a,Object.defineProperty(e,"__esModule",{value:!0})}); //# sourceMappingURL=ehandler.umd.min.js.map