@uoa/error-pages
v16.2.2
Published
Install library using command
Downloads
174
Readme
Uoa Error Pages
Usage
Install library using command
npm install @uoa/error-pages
In your app.module.ts import ErrorPagesModule, eg:
import { ErrorPagesModule } from '@uoa/error-pages';
Include ErrorPagesModule in your imports[].
Create an ErrorRoutingModule to define the error page child route
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { ErrorPagesModule, ErrorPage } from '@uoa/error-pages';
@NgModule({
imports: [ErrorPagesModule, RouterModule.forChild([{ path: '', component: ErrorPage }])],
exports: [RouterModule],
})
export class ErrorRoutingModule {}
Go to routing module of application and add error route to routes. Here is an example:
{
path: 'error/:errorCode',
loadChildren: () => import('./error-routing.module').then((m) => m.ErrorRoutingModule),
}
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 { UoaErrorsConfig } from '@uoa/error-pages';
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();
}
}