SlideShare une entreprise Scribd logo
Decorator &
Interceptor
@cbalit
P.
LE CONTEXTE
SOURCES D’ERREURS
Application Serveur
runtime
http
GLOBALERRORHANDLER
Un service GlobalErrorHandler pour centraliser la stratégie
● message dans l’application
● trace local
● envoi au serveur ...
.factory('GlobalErrorHandler', function ($window) {
var service = {
error: function (error) {
},
ajaxError: function (rejection) {.
},
};
return service;
});
POUR LES APPELS HTTP
● Création d’un service Interceptor: objet implémentant 4 méthodes:
○ request
○ requestError
○ response
○ responseError
● Enregistrer notre interceptor dans le $httpProvider.interceptors
.factory('errorHttpInterceptor', function ($q, GlobalErrorHandler) {
return {
responseError: function responseError(rejection) {
GlobalErrorHandler.ajaxError(rejection);
return $q.reject(rejection);
}
};
})
.config(['$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push('errorHttpInterceptor');
}])
UN DECORATOR
“Surcharge un service tout en gardant la main sur l’ancienne implémentation”
❏ $provide.decorator
❏ dans un bloc config
❏ Ok pour l’injection de dépendances
❏ yo angular:decorator MyService
.config(function ($provide) {
$provide.decorator('MyService', function ($delegate) {
// decorate the $delegate
return $delegate;
});
});
POUR LE RUNTIME
On doit être notifier de toutes les erreurs pouvant se produire
● window.onerror
$provide.decorator('$window', function ($delegate,GlobalErrorHandler) {
$delegate.onerror = function (errorMsg, url, lineNumber) {
var error = new Error(errorMsg, url, lineNumber);
GlobalErrorHandler.error(error);
};
return $delegate;
});
POUR LE RUNTIME
On doit être notifier de toutes les erreurs pouvant se produire
● $exceptionHandler
$provide.decorator('$exceptionHandler', function (GlobalErrorHandler,$delegate) {
return function (exception, cause) {
GlobalErrorHandler.error(exception);
$delegate(exception, cause);
};
});
DÉMO
RÉFÉRENCES
● https://github.com/cbalit/angular-error
● https://docs.angularjs.org/api/auto/object/$provide
● https://docs.angularjs.org/api/ng/service/$http
● http://bahmutov.calepin.co/catch-all-errors-in-angular-app.html
http://sfeir.angularjs.expert

Contenu connexe

PDF
[FR] Injection de dépendances, Containers & PHP-DI
DOCX
tarea--ejercicios propuestos 4
PPTX
Gebeurtenis: Explorer 1
PPTX
Gebeurtenis
PDF
Formazione om new proposition
PPTX
Non verbal communication
PPS
Ob attitude
PPT
Nano technology
[FR] Injection de dépendances, Containers & PHP-DI
tarea--ejercicios propuestos 4
Gebeurtenis: Explorer 1
Gebeurtenis
Formazione om new proposition
Non verbal communication
Ob attitude
Nano technology

En vedette (11)

PPTX
Reported speech (tugas softskill)
PPTX
Curriculum vitae MARIA ELSA LOPEZ
PPTX
Best Diet
PPT
Integrative marketing
PPTX
Website designs
PPTX
Organización chart of coomeva
PPTX
Call sheet Powerpoint
PPTX
Soc 220 mass and movement
PPTX
Movement mazimizers for Cube Dwellers
PPTX
Resolution solutions
PPT
Fema
Reported speech (tugas softskill)
Curriculum vitae MARIA ELSA LOPEZ
Best Diet
Integrative marketing
Website designs
Organización chart of coomeva
Call sheet Powerpoint
Soc 220 mass and movement
Movement mazimizers for Cube Dwellers
Resolution solutions
Fema
Publicité

Similaire à Angular decorator, interceptor and error handler (20)

PDF
Introduction à Angularjs
PPTX
cours Angular Http pour debutants en informatique.pptx
PPTX
cours Angular Httpdhdhhnscnnnxnbcbbb.pptx
PPT
0554-programmation-web-ajax programmation web ajax
PPTX
Introduction à Angular JS
PDF
Monitoring d'applications/environnements PHP : APM et Pinba
PDF
Communications Réseaux et HTTP avec PHP
PDF
Node.js, le pavé dans la mare
PDF
Ou sont mes beans, contrats et workflows ? WOA et REST: Un changement de ment...
PDF
Open close principle, on a dit étendre, pas extends !
PDF
Partie 2: Angular
PDF
Android-Tp5 : web services
PDF
NodeJS for Mobile App
PDF
Mettez du temps réel dans votre Drupal avec Node JS
PDF
Gwt fast overview_v1
PPTX
HTML5 en projet
DOC
Projet base de donnée
PDF
Nouveautés php 7
ODP
Solution Linux 2009 - JavaScript
Introduction à Angularjs
cours Angular Http pour debutants en informatique.pptx
cours Angular Httpdhdhhnscnnnxnbcbbb.pptx
0554-programmation-web-ajax programmation web ajax
Introduction à Angular JS
Monitoring d'applications/environnements PHP : APM et Pinba
Communications Réseaux et HTTP avec PHP
Node.js, le pavé dans la mare
Ou sont mes beans, contrats et workflows ? WOA et REST: Un changement de ment...
Open close principle, on a dit étendre, pas extends !
Partie 2: Angular
Android-Tp5 : web services
NodeJS for Mobile App
Mettez du temps réel dans votre Drupal avec Node JS
Gwt fast overview_v1
HTML5 en projet
Projet base de donnée
Nouveautés php 7
Solution Linux 2009 - JavaScript
Publicité

Plus de Cyril Balit (6)

PDF
Polymer 1.0
PDF
Polymer
PDF
Devoxx 2014-webComponents
PDF
Paris Web 2014 - Outils de test Cross Browser
PPTX
Meetup angular rshop
PPTX
Je code donc je teste - Paris Web 2013
Polymer 1.0
Polymer
Devoxx 2014-webComponents
Paris Web 2014 - Outils de test Cross Browser
Meetup angular rshop
Je code donc je teste - Paris Web 2013

Dernier (16)

PPTX
CH1-RMELLOULI-Données des problèmes d'ordonnancement de la production.pptx
PPTX
mon_expose_de_geophysique_disposotif_de_wener.pptx
PDF
Proposition de contenu pouvant résoudre les problèmes détectés à partir des é...
PDF
TP de La Masse Volumique apparente et absolue
PDF
Arouna Toure - Senior Ingénieur Logiciel Et Chef De Produit
PDF
Expansion du Réseau de Gazoducs de Gaz Naturel au Brésil _ Analyse Technique ...
PDF
CHAPITRE 3 Typologie des réseaux [Enregistrement automatique] 4.pdf
PPTX
Introduction aux Systèmes temps réel.pptx
PDF
Visite de chantier – Projet de Polyclinique à Laghouat
PPTX
COURS DE PROSPECTION MINIERE UTMSIRI - Copie.pptx
PPTX
UMAPON Cours de traitement des minerais 2.pptx
PPTX
A Recurrent Neural Network (RNN)s a type of artificial neural network
PDF
TP L’analyse granulométrique par tamisage
PPTX
Logique séquentielle : les fondamentaux
PDF
FAQ_FORAGE_EAU_SUNRISE_ENGINEERING_GROUP_SARL2025.pdf
PDF
Regles sur la gestion de l’Eclairage public
CH1-RMELLOULI-Données des problèmes d'ordonnancement de la production.pptx
mon_expose_de_geophysique_disposotif_de_wener.pptx
Proposition de contenu pouvant résoudre les problèmes détectés à partir des é...
TP de La Masse Volumique apparente et absolue
Arouna Toure - Senior Ingénieur Logiciel Et Chef De Produit
Expansion du Réseau de Gazoducs de Gaz Naturel au Brésil _ Analyse Technique ...
CHAPITRE 3 Typologie des réseaux [Enregistrement automatique] 4.pdf
Introduction aux Systèmes temps réel.pptx
Visite de chantier – Projet de Polyclinique à Laghouat
COURS DE PROSPECTION MINIERE UTMSIRI - Copie.pptx
UMAPON Cours de traitement des minerais 2.pptx
A Recurrent Neural Network (RNN)s a type of artificial neural network
TP L’analyse granulométrique par tamisage
Logique séquentielle : les fondamentaux
FAQ_FORAGE_EAU_SUNRISE_ENGINEERING_GROUP_SARL2025.pdf
Regles sur la gestion de l’Eclairage public

Angular decorator, interceptor and error handler

  • 4. GLOBALERRORHANDLER Un service GlobalErrorHandler pour centraliser la stratégie ● message dans l’application ● trace local ● envoi au serveur ... .factory('GlobalErrorHandler', function ($window) { var service = { error: function (error) { }, ajaxError: function (rejection) {. }, }; return service; });
  • 5. POUR LES APPELS HTTP ● Création d’un service Interceptor: objet implémentant 4 méthodes: ○ request ○ requestError ○ response ○ responseError ● Enregistrer notre interceptor dans le $httpProvider.interceptors .factory('errorHttpInterceptor', function ($q, GlobalErrorHandler) { return { responseError: function responseError(rejection) { GlobalErrorHandler.ajaxError(rejection); return $q.reject(rejection); } }; }) .config(['$httpProvider', function ($httpProvider) { $httpProvider.interceptors.push('errorHttpInterceptor'); }])
  • 6. UN DECORATOR “Surcharge un service tout en gardant la main sur l’ancienne implémentation” ❏ $provide.decorator ❏ dans un bloc config ❏ Ok pour l’injection de dépendances ❏ yo angular:decorator MyService .config(function ($provide) { $provide.decorator('MyService', function ($delegate) { // decorate the $delegate return $delegate; }); });
  • 7. POUR LE RUNTIME On doit être notifier de toutes les erreurs pouvant se produire ● window.onerror $provide.decorator('$window', function ($delegate,GlobalErrorHandler) { $delegate.onerror = function (errorMsg, url, lineNumber) { var error = new Error(errorMsg, url, lineNumber); GlobalErrorHandler.error(error); }; return $delegate; });
  • 8. POUR LE RUNTIME On doit être notifier de toutes les erreurs pouvant se produire ● $exceptionHandler $provide.decorator('$exceptionHandler', function (GlobalErrorHandler,$delegate) { return function (exception, cause) { GlobalErrorHandler.error(exception); $delegate(exception, cause); }; });
  • 10. RÉFÉRENCES ● https://github.com/cbalit/angular-error ● https://docs.angularjs.org/api/auto/object/$provide ● https://docs.angularjs.org/api/ng/service/$http ● http://bahmutov.calepin.co/catch-all-errors-in-angular-app.html