{"id":1022,"date":"2023-06-01T05:00:00","date_gmt":"2023-06-01T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1022"},"modified":"2023-05-23T15:39:09","modified_gmt":"2023-05-23T22:39:09","slug":"how-to-create-a-generic-error-handler-with-angular","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/how-to-create-a-generic-error-handler-with-angular\/","title":{"rendered":"How to create a generic error handler with Angular?"},"content":{"rendered":"\n<p>Earlier in this newsletter, we covered <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/rxjs-tapobserver\/\" target=\"_blank\" rel=\"noopener\" title=\"\">how to handle errors with RxJs Observables<\/a>. What about errors in the rest of the application? Javascript has a primary <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/try...catch\" target=\"_blank\" rel=\"noopener\" title=\"\">try\/catch mechanism<\/a>, but this doesn&#8217;t help handle errors in a generic, unified way.<\/p>\n\n\n\n<p>Fortunately enough, Angular has an <a href=\"https:\/\/angular.io\/api\/core\/ErrorHandler\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>ErrorHandler<\/code><\/a> interface that can be implemented by global error handler services such as this one:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"695\" height=\"216\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-39.png\" alt=\"\" class=\"wp-image-1026\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-39.png 695w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/05\/image-39-300x93.png 300w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>That way, we can implement a catch-all method to deal with errors and display them to the user consistently (code example here). Even better, such a service could report errors to a server for further investigation and debugging from the dev team.<\/p>\n\n\n\n<p>Here is a link to my <a href=\"https:\/\/blog.angulartraining.com\/tutorial-custom-error-handling-with-angular-e0d612d67575\" target=\"_blank\" rel=\"noopener\" title=\"\">custom error-handling tutorial<\/a> to see a good example of how to implement a <code>ErroHandler<\/code> service.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Earlier in this newsletter, we covered how to handle errors with RxJs Observables. What about errors in the rest of the application? Javascript has a primary try\/catch mechanism, but this doesn&#8217;t help handle errors in a generic, unified way. Fortunately enough, Angular has an ErrorHandler interface that can be implemented by global error handler services [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,21],"tags":[],"class_list":["post-1022","post","type-post","status-publish","format-standard","hentry","category-angular","category-architecture"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1022","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1022"}],"version-history":[{"count":4,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1022\/revisions"}],"predecessor-version":[{"id":1027,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1022\/revisions\/1027"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}