@uoa/error-pages
Version:
University of Auckland error page library for intercepting client/server errors & redirecting to custom error pages.
151 lines (106 loc) • 3.93 kB
Markdown
# Uoa Error Pages
University of Auckland error page library for intercepting client/server errors & redirecting to custom error pages.
## Usage
Install library using command
```
npm install @uoa/error-pages
```
In your project's main.ts import provideErrorPages:
`import { provideErrorPages } from '@uoa/error-pages';`
Include provideErrorPages in your providers[]:
```
bootstrapApplication(AppComponent, {
providers: [
...
provideErrorPages(),
...
]
}).catch((err) => console.log(err));
```
Create an error.routes.ts to define the error page child route
```
import { Routes } from '@angular/router';
import { ErrorPage } from '@uoa/error-pages';
export const errorRoutes: Routes = [
{
path: '',
component: ErrorPage,
},
];
```
Go to app.routes.ts of application and add error route to routes. Here is an example:
```
{
path: 'error/:errorCode',
loadChildren: () => import('./app/error-routing/error.routes').then((m) => m.errorRoutes),
}
```
If you want to handle specific error code for any endpoint inside project. import BypassErrorService to your Service
`import { BypassErrorService } from '@uoa/error-pages';`
and call following method before your api call:
`this._bypass.bypassError('${url}', [409, 401, 504, 404]);`
Library will skip mentioned error statuses for given end point.
Default Error codes are as follow:
```
clientErrorCodes = [400, 401, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 418, 429, 431];
serverErrorCodes = [500, 501, 502, 503, 504, 505, 506];
```
Error Template is defined for all the above codes.
## Override Error codes and Error Content
If you want to override default error codes and templates, include UoaErrorsConfig as a provider:
`{ provide: UoaErrorsConfig, useClass: AppErrorsConfig }`
You will need to create this AppErrorsConfig class, where you assign new values to existing objects or add new objects properties. Here is an example:
```
import { Injectable } from '@angular/core';
import { UoaErrorsConfig } from '@uoa/error-pages';
@Injectable({
providedIn: 'root',
})
export class AppErrorsConfig extends UoaErrorsConfig {
constructor() {
super();
this.serverErrorCodes = [501, 504, 505];
this.ErrorPageContent['ErrorCode400'] = { title: 'Custom title', content: 'Custom description' };
}
}
```
Similarly if need to change button's text on error page override variable in AppErrorsConfig class.
```
this.returnButtonText = 'Go To Home Page';
```
## Use Toaster instead of Error Page to show Errors
If you want to use toaster instead of Error page to show error message, firstly set config variable 'showErrorAsToast' in AppErrorsConfig class.
```
this.showErrorAsToast = true;
```
Afterwards include ErrorToastService as a provider:
`import { ErrorToastService } from '@uoa/error-pages';`
`{ provide: ErrorToastService, useClass: AppToastService },`
You will need to create AppToastService class. It is up to user to use ionic ToastController or different toast library.
AppToastService class implementation using Ionic ToastController:
```
import { Injectable } from '@angular/core';
import { ToastController } from '@ionic/angular';
import { ErrorToastService, ErrorContentDto } from '@uoa/error-pages';
@Injectable({
providedIn: 'root',
})
export class AppToastService implements ErrorToastService {
constructor(private _toastCtrl: ToastController) {}
async createToast(content: ErrorContentDto) {
const toast = await this._toastCtrl.create({
message: content.content,
header: content.title,
position: 'middle',
color: 'dark',
buttons: [
{
text: 'Ok',
role: 'cancel',
},
],
});
toast.present();
}
}
```