├── .browserslistrc ├── .editorconfig ├── .gitignore ├── README.md ├── angular.json ├── cover-image.png ├── karma.conf.js ├── package-lock.json ├── package.json ├── src ├── app │ ├── app.component.html │ ├── app.component.scss │ ├── app.component.ts │ ├── app.module.ts │ ├── core │ │ ├── core.module.ts │ │ └── errors │ │ │ ├── global-error-handler.ts │ │ │ └── http-loading.interceptor.ts │ ├── material.module.ts │ └── shared │ │ ├── errors │ │ ├── error-dialog.service.ts │ │ └── error-dialog │ │ │ ├── error-dialog.component.html │ │ │ ├── error-dialog.component.scss │ │ │ └── error-dialog.component.ts │ │ ├── loading │ │ ├── loading-dialog.service.ts │ │ └── loading-dialog │ │ │ ├── loading-dialog.component.html │ │ │ ├── loading-dialog.component.scss │ │ │ └── loading-dialog.component.ts │ │ └── shared.module.ts ├── assets │ └── .gitkeep ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── favicon.ico ├── index.html ├── main.ts ├── polyfills.ts ├── styles.scss └── test.ts ├── tsconfig.app.json ├── tsconfig.json └── tsconfig.spec.json /.browserslistrc: -------------------------------------------------------------------------------- 1 | # This file is used by the build system to adjust CSS and JS output to support the specified browsers below. 2 | # For additional information regarding the format and rule options, please see: 3 | # https://github.com/browserslist/browserslist#queries 4 | 5 | # For the full list of supported browsers by the Angular framework, please see: 6 | # https://angular.io/guide/browser-support 7 | 8 | # You can see what browsers were selected by your queries by running: 9 | # npx browserslist 10 | 11 | last 1 Chrome version 12 | last 1 Firefox version 13 | last 2 Edge major versions 14 | last 2 Safari major versions 15 | last 2 iOS major versions 16 | Firefox ESR 17 | not IE 11 # Angular supports IE 11 only as an opt-in. To opt-in, remove the 'not' prefix on this line. 18 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see https://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.ts] 12 | quote_type = single 13 | 14 | [*.md] 15 | max_line_length = off 16 | trim_trailing_whitespace = false 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | # Only exists if Bazel was run 8 | /bazel-out 9 | 10 | # dependencies 11 | /node_modules 12 | 13 | # profiling files 14 | chrome-profiler-events*.json 15 | 16 | # IDEs and editors 17 | /.idea 18 | .project 19 | .classpath 20 | .c9/ 21 | *.launch 22 | .settings/ 23 | *.sublime-workspace 24 | 25 | # IDE - VSCode 26 | .vscode/* 27 | !.vscode/settings.json 28 | !.vscode/tasks.json 29 | !.vscode/launch.json 30 | !.vscode/extensions.json 31 | .history/* 32 | 33 | # misc 34 | /.sass-cache 35 | /connect.lock 36 | /coverage 37 | /libpeerconnection.log 38 | npm-debug.log 39 | yarn-error.log 40 | testem.log 41 | /typings 42 | 43 | # System Files 44 | .DS_Store 45 | Thumbs.db 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Angular Global Error Handling 2 | 3 |

Learn how to automatically catch all errors in a web application written in Angular and process them accordingly

4 | 5 | 📖 Read more on Medium 6 | 7 | Global Error Handling Cover Image 8 | 9 | ## Development server 10 | 11 | Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files. 12 | 13 | ## Code scaffolding 14 | 15 | Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`. 16 | 17 | ## Build 18 | 19 | Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. 20 | 21 | ## Running unit tests 22 | 23 | Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io). 24 | 25 | ## Running end-to-end tests 26 | 27 | Run `ng e2e` to execute the end-to-end tests via a platform of your choice. To use this command, you need to first add a package that implements end-to-end testing capabilities. 28 | 29 | ## Further help 30 | 31 | To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI Overview and Command Reference](https://angular.io/cli) page. 32 | -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "angular-global-error-handling": { 7 | "projectType": "application", 8 | "schematics": { 9 | "@schematics/angular:component": { 10 | "style": "scss" 11 | }, 12 | "@schematics/angular:application": { 13 | "strict": true 14 | } 15 | }, 16 | "root": "", 17 | "sourceRoot": "src", 18 | "prefix": "app", 19 | "architect": { 20 | "build": { 21 | "builder": "@angular-devkit/build-angular:browser", 22 | "options": { 23 | "outputPath": "dist/angular-global-error-handling", 24 | "index": "src/index.html", 25 | "main": "src/main.ts", 26 | "polyfills": "src/polyfills.ts", 27 | "tsConfig": "tsconfig.app.json", 28 | "inlineStyleLanguage": "scss", 29 | "assets": [ 30 | "src/favicon.ico", 31 | "src/assets" 32 | ], 33 | "styles": [ 34 | "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css", 35 | "src/styles.scss" 36 | ], 37 | "scripts": [] 38 | }, 39 | "configurations": { 40 | "production": { 41 | "budgets": [ 42 | { 43 | "type": "initial", 44 | "maximumWarning": "500kb", 45 | "maximumError": "1mb" 46 | }, 47 | { 48 | "type": "anyComponentStyle", 49 | "maximumWarning": "2kb", 50 | "maximumError": "4kb" 51 | } 52 | ], 53 | "fileReplacements": [ 54 | { 55 | "replace": "src/environments/environment.ts", 56 | "with": "src/environments/environment.prod.ts" 57 | } 58 | ], 59 | "outputHashing": "all" 60 | }, 61 | "development": { 62 | "buildOptimizer": false, 63 | "optimization": false, 64 | "vendorChunk": true, 65 | "extractLicenses": false, 66 | "sourceMap": true, 67 | "namedChunks": true 68 | } 69 | }, 70 | "defaultConfiguration": "production" 71 | }, 72 | "serve": { 73 | "builder": "@angular-devkit/build-angular:dev-server", 74 | "configurations": { 75 | "production": { 76 | "browserTarget": "angular-global-error-handling:build:production" 77 | }, 78 | "development": { 79 | "browserTarget": "angular-global-error-handling:build:development" 80 | } 81 | }, 82 | "defaultConfiguration": "development" 83 | }, 84 | "extract-i18n": { 85 | "builder": "@angular-devkit/build-angular:extract-i18n", 86 | "options": { 87 | "browserTarget": "angular-global-error-handling:build" 88 | } 89 | }, 90 | "test": { 91 | "builder": "@angular-devkit/build-angular:karma", 92 | "options": { 93 | "main": "src/test.ts", 94 | "polyfills": "src/polyfills.ts", 95 | "tsConfig": "tsconfig.spec.json", 96 | "karmaConfig": "karma.conf.js", 97 | "inlineStyleLanguage": "scss", 98 | "assets": [ 99 | "src/favicon.ico", 100 | "src/assets" 101 | ], 102 | "styles": [ 103 | "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css", 104 | "src/styles.scss" 105 | ], 106 | "scripts": [] 107 | } 108 | } 109 | } 110 | } 111 | }, 112 | "defaultProject": "angular-global-error-handling" 113 | } 114 | -------------------------------------------------------------------------------- /cover-image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PKief/angular-global-error-handling/7b70d873ca3d2cb589ee961e3da672273a4892c9/cover-image.png -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/1.0/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular-devkit/build-angular'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage'), 13 | require('@angular-devkit/build-angular/plugins/karma') 14 | ], 15 | client: { 16 | jasmine: { 17 | // you can add configuration options for Jasmine here 18 | // the possible options are listed at https://jasmine.github.io/api/edge/Configuration.html 19 | // for example, you can disable the random execution with `random: false` 20 | // or set a specific seed with `seed: 4321` 21 | }, 22 | clearContext: false // leave Jasmine Spec Runner output visible in browser 23 | }, 24 | jasmineHtmlReporter: { 25 | suppressAll: true // removes the duplicated traces 26 | }, 27 | coverageReporter: { 28 | dir: require('path').join(__dirname, './coverage/angular-global-error-handling'), 29 | subdir: '.', 30 | reporters: [ 31 | { type: 'html' }, 32 | { type: 'text-summary' } 33 | ] 34 | }, 35 | reporters: ['progress', 'kjhtml'], 36 | port: 9876, 37 | colors: true, 38 | logLevel: config.LOG_INFO, 39 | autoWatch: true, 40 | browsers: ['Chrome'], 41 | singleRun: false, 42 | restartOnFileChange: true 43 | }); 44 | }; 45 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-global-error-handling", 3 | "version": "0.0.0", 4 | "scripts": { 5 | "ng": "ng", 6 | "start": "ng serve", 7 | "build": "ng build", 8 | "watch": "ng build --watch --configuration development", 9 | "test": "ng test" 10 | }, 11 | "private": true, 12 | "dependencies": { 13 | "@angular/animations": "~12.2.0", 14 | "@angular/cdk": "^12.2.5", 15 | "@angular/common": "~12.2.0", 16 | "@angular/compiler": "~12.2.0", 17 | "@angular/core": "~12.2.0", 18 | "@angular/forms": "~12.2.0", 19 | "@angular/material": "^12.2.5", 20 | "@angular/platform-browser": "~12.2.0", 21 | "@angular/platform-browser-dynamic": "~12.2.0", 22 | "@angular/router": "~12.2.0", 23 | "rxjs": "~6.6.0", 24 | "tslib": "^2.3.0", 25 | "zone.js": "~0.11.4" 26 | }, 27 | "devDependencies": { 28 | "@angular-devkit/build-angular": "~12.2.5", 29 | "@angular/cli": "~12.2.5", 30 | "@angular/compiler-cli": "~12.2.0", 31 | "@types/jasmine": "~3.8.0", 32 | "@types/node": "^12.11.1", 33 | "jasmine-core": "~3.8.0", 34 | "karma": "~6.3.0", 35 | "karma-chrome-launcher": "~3.1.0", 36 | "karma-coverage": "~2.0.3", 37 | "karma-jasmine": "~4.0.0", 38 | "karma-jasmine-html-reporter": "~1.7.0", 39 | "typescript": "~4.3.5" 40 | } 41 | } -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 | Error Interceptor Demo 2 | 3 |
4 | 7 | 10 | 13 |
14 | -------------------------------------------------------------------------------- /src/app/app.component.scss: -------------------------------------------------------------------------------- 1 | .content-container { 2 | margin: 1rem; 3 | display: flex; 4 | flex-direction: column; 5 | 6 | button { 7 | margin-top: 1rem; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from '@angular/core'; 2 | import { HttpClient } from '@angular/common/http'; 3 | 4 | /** 5 | * @title Basic progress-spinner 6 | */ 7 | @Component({ 8 | selector: 'app', 9 | templateUrl: 'app.component.html', 10 | styleUrls: ['./app.component.scss'], 11 | }) 12 | export class AppComponent { 13 | constructor(private http: HttpClient) {} 14 | 15 | localError() { 16 | throw Error('The app component has thrown an error!'); 17 | } 18 | 19 | failingRequest() { 20 | this.http.get('https://httpstat.us/404?sleep=2000').toPromise(); 21 | } 22 | 23 | successfulRequest() { 24 | this.http.get('https://httpstat.us/200?sleep=2000').toPromise(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { HttpClientModule } from '@angular/common/http'; 2 | import { NgModule } from '@angular/core'; 3 | import { BrowserModule } from '@angular/platform-browser'; 4 | import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; 5 | import { AppComponent } from './app.component'; 6 | import { CoreModule } from './core/core.module'; 7 | import { MaterialModule } from './material.module'; 8 | import { SharedModule } from './shared/shared.module'; 9 | 10 | @NgModule({ 11 | imports: [ 12 | BrowserModule, 13 | BrowserAnimationsModule, 14 | HttpClientModule, 15 | MaterialModule, 16 | CoreModule, 17 | SharedModule, 18 | ], 19 | entryComponents: [AppComponent], 20 | declarations: [AppComponent], 21 | bootstrap: [AppComponent], 22 | }) 23 | export class AppModule {} 24 | -------------------------------------------------------------------------------- /src/app/core/core.module.ts: -------------------------------------------------------------------------------- 1 | import { CommonModule } from '@angular/common'; 2 | import { HTTP_INTERCEPTORS } from '@angular/common/http'; 3 | import { ErrorHandler, NgModule } from '@angular/core'; 4 | import { GlobalErrorHandler } from './errors/global-error-handler'; 5 | import { HttpLoadingInterceptor } from './errors/http-loading.interceptor'; 6 | 7 | @NgModule({ 8 | declarations: [], 9 | imports: [CommonModule], 10 | providers: [ 11 | { 12 | provide: ErrorHandler, 13 | useClass: GlobalErrorHandler, 14 | }, 15 | { 16 | provide: HTTP_INTERCEPTORS, 17 | useClass: HttpLoadingInterceptor, 18 | multi: true, 19 | }, 20 | ], 21 | }) 22 | export class CoreModule {} 23 | -------------------------------------------------------------------------------- /src/app/core/errors/global-error-handler.ts: -------------------------------------------------------------------------------- 1 | import { HttpErrorResponse } from '@angular/common/http'; 2 | import { ErrorHandler, Injectable, NgZone } from '@angular/core'; 3 | import { ErrorDialogService } from '../../shared/errors/error-dialog.service'; 4 | 5 | @Injectable() 6 | export class GlobalErrorHandler implements ErrorHandler { 7 | constructor( 8 | private errorDialogService: ErrorDialogService, 9 | private zone: NgZone 10 | ) {} 11 | 12 | handleError(error: any) { 13 | // Check if it's an error from an HTTP response 14 | if (!(error instanceof HttpErrorResponse)) { 15 | error = error.rejection; // get the error object 16 | } 17 | this.zone.run(() => 18 | this.errorDialogService.openDialog( 19 | error?.message || 'Undefined client error', 20 | error?.status 21 | ) 22 | ); 23 | 24 | console.error('Error from global error handler', error); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/app/core/errors/http-loading.interceptor.ts: -------------------------------------------------------------------------------- 1 | import { 2 | HttpHandler, 3 | HttpRequest, 4 | HttpEvent, 5 | HttpInterceptor, 6 | } from '@angular/common/http'; 7 | import { Observable } from 'rxjs'; 8 | import { finalize } from 'rxjs/operators'; 9 | import { LoadingDialogService } from '../../shared/loading/loading-dialog.service'; 10 | import { Injectable } from '@angular/core'; 11 | 12 | @Injectable() 13 | export class HttpLoadingInterceptor implements HttpInterceptor { 14 | constructor(private loadingDialogService: LoadingDialogService) {} 15 | 16 | intercept( 17 | request: HttpRequest, 18 | next: HttpHandler 19 | ): Observable> { 20 | this.loadingDialogService.openDialog(); 21 | return next.handle(request).pipe( 22 | finalize(() => { 23 | this.loadingDialogService.hideDialog(); 24 | }) 25 | ) as Observable>; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/app/material.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { A11yModule } from '@angular/cdk/a11y'; 3 | import { ClipboardModule } from '@angular/cdk/clipboard'; 4 | import { DragDropModule } from '@angular/cdk/drag-drop'; 5 | import { PortalModule } from '@angular/cdk/portal'; 6 | import { ScrollingModule } from '@angular/cdk/scrolling'; 7 | import { CdkStepperModule } from '@angular/cdk/stepper'; 8 | import { CdkTableModule } from '@angular/cdk/table'; 9 | import { CdkTreeModule } from '@angular/cdk/tree'; 10 | import { MatAutocompleteModule } from '@angular/material/autocomplete'; 11 | import { MatBadgeModule } from '@angular/material/badge'; 12 | import { MatBottomSheetModule } from '@angular/material/bottom-sheet'; 13 | import { MatButtonModule } from '@angular/material/button'; 14 | import { MatButtonToggleModule } from '@angular/material/button-toggle'; 15 | import { MatCardModule } from '@angular/material/card'; 16 | import { MatCheckboxModule } from '@angular/material/checkbox'; 17 | import { MatChipsModule } from '@angular/material/chips'; 18 | import { MatStepperModule } from '@angular/material/stepper'; 19 | import { MatDatepickerModule } from '@angular/material/datepicker'; 20 | import { MatDialogModule } from '@angular/material/dialog'; 21 | import { MatDividerModule } from '@angular/material/divider'; 22 | import { MatExpansionModule } from '@angular/material/expansion'; 23 | import { MatGridListModule } from '@angular/material/grid-list'; 24 | import { MatIconModule } from '@angular/material/icon'; 25 | import { MatInputModule } from '@angular/material/input'; 26 | import { MatListModule } from '@angular/material/list'; 27 | import { MatMenuModule } from '@angular/material/menu'; 28 | import { MatNativeDateModule, MatRippleModule } from '@angular/material/core'; 29 | import { MatPaginatorModule } from '@angular/material/paginator'; 30 | import { MatProgressBarModule } from '@angular/material/progress-bar'; 31 | import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; 32 | import { MatRadioModule } from '@angular/material/radio'; 33 | import { MatSelectModule } from '@angular/material/select'; 34 | import { MatSidenavModule } from '@angular/material/sidenav'; 35 | import { MatSliderModule } from '@angular/material/slider'; 36 | import { MatSlideToggleModule } from '@angular/material/slide-toggle'; 37 | import { MatSnackBarModule } from '@angular/material/snack-bar'; 38 | import { MatSortModule } from '@angular/material/sort'; 39 | import { MatTableModule } from '@angular/material/table'; 40 | import { MatTabsModule } from '@angular/material/tabs'; 41 | import { MatToolbarModule } from '@angular/material/toolbar'; 42 | import { MatTooltipModule } from '@angular/material/tooltip'; 43 | import { MatTreeModule } from '@angular/material/tree'; 44 | import { OverlayModule } from '@angular/cdk/overlay'; 45 | 46 | @NgModule({ 47 | exports: [ 48 | A11yModule, 49 | ClipboardModule, 50 | CdkStepperModule, 51 | CdkTableModule, 52 | CdkTreeModule, 53 | DragDropModule, 54 | MatAutocompleteModule, 55 | MatBadgeModule, 56 | MatBottomSheetModule, 57 | MatButtonModule, 58 | MatButtonToggleModule, 59 | MatCardModule, 60 | MatCheckboxModule, 61 | MatChipsModule, 62 | MatStepperModule, 63 | MatDatepickerModule, 64 | MatDialogModule, 65 | MatDividerModule, 66 | MatExpansionModule, 67 | MatGridListModule, 68 | MatIconModule, 69 | MatInputModule, 70 | MatListModule, 71 | MatMenuModule, 72 | MatNativeDateModule, 73 | MatPaginatorModule, 74 | MatProgressBarModule, 75 | MatProgressSpinnerModule, 76 | MatRadioModule, 77 | MatRippleModule, 78 | MatSelectModule, 79 | MatSidenavModule, 80 | MatSliderModule, 81 | MatSlideToggleModule, 82 | MatSnackBarModule, 83 | MatSortModule, 84 | MatTableModule, 85 | MatTabsModule, 86 | MatToolbarModule, 87 | MatTooltipModule, 88 | MatTreeModule, 89 | OverlayModule, 90 | PortalModule, 91 | ScrollingModule, 92 | ], 93 | }) 94 | export class MaterialModule {} 95 | -------------------------------------------------------------------------------- /src/app/shared/errors/error-dialog.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { MatDialog } from '@angular/material/dialog'; 3 | import { ErrorDialogComponent } from './error-dialog/error-dialog.component'; 4 | 5 | @Injectable() 6 | export class ErrorDialogService { 7 | private opened = false; 8 | 9 | constructor(private dialog: MatDialog) {} 10 | 11 | openDialog(message: string, status?: number): void { 12 | if (!this.opened) { 13 | this.opened = true; 14 | const dialogRef = this.dialog.open(ErrorDialogComponent, { 15 | data: { message, status }, 16 | maxHeight: '100%', 17 | width: '540px', 18 | maxWidth: '100%', 19 | disableClose: true, 20 | hasBackdrop: true, 21 | }); 22 | 23 | dialogRef.afterClosed().subscribe(() => { 24 | this.opened = false; 25 | }); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/app/shared/errors/error-dialog/error-dialog.component.html: -------------------------------------------------------------------------------- 1 |

An error has occurred!

2 |
3 |

4 | Status Code: {{ data.status }} 5 |

6 |

Something went wrong!

7 |

8 | {{ data?.message }} 9 |

10 |
11 |
12 | 13 |
14 | -------------------------------------------------------------------------------- /src/app/shared/errors/error-dialog/error-dialog.component.scss: -------------------------------------------------------------------------------- 1 | .error-message { 2 | background: #eee; 3 | padding: 1rem; 4 | font-family: monospace; 5 | word-break: break-all; 6 | } 7 | -------------------------------------------------------------------------------- /src/app/shared/errors/error-dialog/error-dialog.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit, Inject } from '@angular/core'; 2 | import { MAT_DIALOG_DATA } from '@angular/material/dialog'; 3 | 4 | @Component({ 5 | selector: 'app-error-dialog', 6 | templateUrl: './error-dialog.component.html', 7 | styleUrls: ['./error-dialog.component.scss'], 8 | }) 9 | export class ErrorDialogComponent { 10 | constructor( 11 | @Inject(MAT_DIALOG_DATA) 12 | public data: { message: string; status?: number } 13 | ) {} 14 | } 15 | -------------------------------------------------------------------------------- /src/app/shared/loading/loading-dialog.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { MatDialog, MatDialogRef } from '@angular/material/dialog'; 3 | import { LoadingDialogComponent } from './loading-dialog/loading-dialog.component'; 4 | 5 | @Injectable() 6 | export class LoadingDialogService { 7 | private opened = false; 8 | private dialogRef!: MatDialogRef; 9 | 10 | constructor(private dialog: MatDialog) {} 11 | 12 | openDialog(): void { 13 | if (!this.opened) { 14 | this.opened = true; 15 | this.dialogRef = this.dialog.open(LoadingDialogComponent, { 16 | data: undefined, 17 | maxHeight: '100%', 18 | width: '400px', 19 | maxWidth: '100%', 20 | disableClose: true, 21 | hasBackdrop: true, 22 | }); 23 | 24 | this.dialogRef.afterClosed().subscribe(() => { 25 | this.opened = false; 26 | }); 27 | } 28 | } 29 | 30 | hideDialog() { 31 | this.dialogRef.close(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/app/shared/loading/loading-dialog/loading-dialog.component.html: -------------------------------------------------------------------------------- 1 |

Loading data

2 |
Please have a moment of patience
3 |
4 |
5 | 6 |
7 |
8 | -------------------------------------------------------------------------------- /src/app/shared/loading/loading-dialog/loading-dialog.component.scss: -------------------------------------------------------------------------------- 1 | .mat-dialog-actions { 2 | margin-top: -2rem !important; 3 | padding-top: 2rem !important; 4 | } 5 | 6 | .loading-spinner { 7 | text-align: center; 8 | margin: 3rem auto; 9 | } 10 | -------------------------------------------------------------------------------- /src/app/shared/loading/loading-dialog/loading-dialog.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-loading-dialog', 5 | templateUrl: './loading-dialog.component.html', 6 | styleUrls: ['./loading-dialog.component.scss'], 7 | }) 8 | export class LoadingDialogComponent { 9 | constructor() {} 10 | } 11 | -------------------------------------------------------------------------------- /src/app/shared/shared.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { LoadingDialogComponent } from './loading/loading-dialog/loading-dialog.component'; 3 | import { ErrorDialogComponent } from './errors/error-dialog/error-dialog.component'; 4 | import { CommonModule } from '@angular/common'; 5 | import { RouterModule } from '@angular/router'; 6 | import { ErrorDialogService } from './errors/error-dialog.service'; 7 | import { LoadingDialogService } from './loading/loading-dialog.service'; 8 | import { MaterialModule } from '../material.module'; 9 | 10 | const sharedComponents = [LoadingDialogComponent, ErrorDialogComponent]; 11 | 12 | @NgModule({ 13 | declarations: [...sharedComponents], 14 | imports: [CommonModule, RouterModule, MaterialModule], 15 | exports: [...sharedComponents], 16 | providers: [ErrorDialogService, LoadingDialogService], 17 | entryComponents: [...sharedComponents], 18 | }) 19 | export class SharedModule {} 20 | -------------------------------------------------------------------------------- /src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PKief/angular-global-error-handling/7b70d873ca3d2cb589ee961e3da672273a4892c9/src/assets/.gitkeep -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // This file can be replaced during build by using the `fileReplacements` array. 2 | // `ng build` replaces `environment.ts` with `environment.prod.ts`. 3 | // The list of file replacements can be found in `angular.json`. 4 | 5 | export const environment = { 6 | production: false 7 | }; 8 | 9 | /* 10 | * For easier debugging in development mode, you can import the following file 11 | * to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`. 12 | * 13 | * This import should be commented out in production mode because it will have a negative impact 14 | * on performance if an error is thrown. 15 | */ 16 | // import 'zone.js/plugins/zone-error'; // Included with Angular CLI. 17 | -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PKief/angular-global-error-handling/7b70d873ca3d2cb589ee961e3da672273a4892c9/src/favicon.ico -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Angular Global Error Handling 6 | 7 | 8 | 9 | 10 | 14 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 3 | 4 | import { AppModule } from './app/app.module'; 5 | import { environment } from './environments/environment'; 6 | 7 | if (environment.production) { 8 | enableProdMode(); 9 | } 10 | 11 | platformBrowserDynamic().bootstrapModule(AppModule) 12 | .catch(err => console.error(err)); 13 | -------------------------------------------------------------------------------- /src/polyfills.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file includes polyfills needed by Angular and is loaded before the app. 3 | * You can add your own extra polyfills to this file. 4 | * 5 | * This file is divided into 2 sections: 6 | * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. 7 | * 2. Application imports. Files imported after ZoneJS that should be loaded before your main 8 | * file. 9 | * 10 | * The current setup is for so-called "evergreen" browsers; the last versions of browsers that 11 | * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), 12 | * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/guide/browser-support 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** 22 | * IE11 requires the following for NgClass support on SVG elements 23 | */ 24 | // import 'classlist.js'; // Run `npm install --save classlist.js`. 25 | 26 | /** 27 | * Web Animations `@angular/platform-browser/animations` 28 | * Only required if AnimationBuilder is used within the application and using IE/Edge or Safari. 29 | * Standard animation support in Angular DOES NOT require any polyfills (as of Angular 6.0). 30 | */ 31 | // import 'web-animations-js'; // Run `npm install --save web-animations-js`. 32 | 33 | /** 34 | * By default, zone.js will patch all possible macroTask and DomEvents 35 | * user can disable parts of macroTask/DomEvents patch by setting following flags 36 | * because those flags need to be set before `zone.js` being loaded, and webpack 37 | * will put import in the top of bundle, so user need to create a separate file 38 | * in this directory (for example: zone-flags.ts), and put the following flags 39 | * into that file, and then add the following code before importing zone.js. 40 | * import './zone-flags'; 41 | * 42 | * The flags allowed in zone-flags.ts are listed here. 43 | * 44 | * The following flags will work for all browsers. 45 | * 46 | * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame 47 | * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick 48 | * (window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames 49 | * 50 | * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js 51 | * with the following flag, it will bypass `zone.js` patch for IE/Edge 52 | * 53 | * (window as any).__Zone_enable_cross_context_check = true; 54 | * 55 | */ 56 | 57 | /*************************************************************************************************** 58 | * Zone JS is required by default for Angular itself. 59 | */ 60 | import 'zone.js'; // Included with Angular CLI. 61 | 62 | 63 | /*************************************************************************************************** 64 | * APPLICATION IMPORTS 65 | */ 66 | -------------------------------------------------------------------------------- /src/styles.scss: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ 2 | 3 | html, body { height: 100%; } 4 | body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; } 5 | -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | 3 | import 'zone.js/testing'; 4 | import { getTestBed } from '@angular/core/testing'; 5 | import { 6 | BrowserDynamicTestingModule, 7 | platformBrowserDynamicTesting 8 | } from '@angular/platform-browser-dynamic/testing'; 9 | 10 | declare const require: { 11 | context(path: string, deep?: boolean, filter?: RegExp): { 12 | keys(): string[]; 13 | (id: string): T; 14 | }; 15 | }; 16 | 17 | // First, initialize the Angular testing environment. 18 | getTestBed().initTestEnvironment( 19 | BrowserDynamicTestingModule, 20 | platformBrowserDynamicTesting(), 21 | { teardown: { destroyAfterEach: true }}, 22 | ); 23 | 24 | // Then we find all the tests. 25 | const context = require.context('./', true, /\.spec\.ts$/); 26 | // And load the modules. 27 | context.keys().map(context); 28 | -------------------------------------------------------------------------------- /tsconfig.app.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "extends": "./tsconfig.json", 4 | "compilerOptions": { 5 | "outDir": "./out-tsc/app", 6 | "types": [] 7 | }, 8 | "files": [ 9 | "src/main.ts", 10 | "src/polyfills.ts" 11 | ], 12 | "include": [ 13 | "src/**/*.d.ts" 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "compileOnSave": false, 4 | "compilerOptions": { 5 | "baseUrl": "./", 6 | "outDir": "./dist/out-tsc", 7 | "forceConsistentCasingInFileNames": true, 8 | "strict": true, 9 | "noImplicitReturns": true, 10 | "noFallthroughCasesInSwitch": true, 11 | "sourceMap": true, 12 | "declaration": false, 13 | "downlevelIteration": true, 14 | "experimentalDecorators": true, 15 | "moduleResolution": "node", 16 | "importHelpers": true, 17 | "target": "es2017", 18 | "module": "es2020", 19 | "lib": [ 20 | "es2018", 21 | "dom" 22 | ] 23 | }, 24 | "angularCompilerOptions": { 25 | "enableI18nLegacyMessageIdFormat": false, 26 | "strictInjectionParameters": true, 27 | "strictInputAccessModifiers": true, 28 | "strictTemplates": true 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "extends": "./tsconfig.json", 4 | "compilerOptions": { 5 | "outDir": "./out-tsc/spec", 6 | "types": [ 7 | "jasmine" 8 | ] 9 | }, 10 | "files": [ 11 | "src/test.ts", 12 | "src/polyfills.ts" 13 | ], 14 | "include": [ 15 | "src/**/*.spec.ts", 16 | "src/**/*.d.ts" 17 | ] 18 | } 19 | --------------------------------------------------------------------------------