Options
All
  • Public
  • Public/Protected
  • All
Menu

Module ngx-customapp-errors

Install

yarn add ngx-customapp-errors

Write the config ErrorsConfig.

Import ErrorsModule.forRoot(config) once in your app.

If you wish to report unhandled errors, add

  {provide: ErrorHandler, useClass: ErrorsHandler}

to the app.module providers array.

Use ErrorsService, available across the app, to report and transform errors.

Stick to the conventions, described below.

Convention: all errors, thrown by the request function (an http request or a socket request), have a string type and are a human-readable messages, understandable by the user. To do so, pipe request observable through ErrorsService.toUserError. All errors should be reported to the server. Errors, thrown by the request function, are handled using ErrorsService.reportError

    return this.http.post(
endpoint,
request
).pipe(
catchError(this.errorsService.reportError),
catchError(this.errorsService.toUserError),
)

Another convention is to handle errors, thrown by the request functions, inside effects, and map them into NgRx actions. Example:

OrdersEffects

  getOrders$ = createEffect(() => this.actions$.pipe(
ofType(getOrdersCurrent),
mergeMap(() =>
this.ordersService
.getOrdersListProcessing()
.pipe(
map(ordersListResponse =>
getOrdersCurrentSucceed({
ordersListResponse
})
),
catchError(error => of(
// error here is human-readable and can be displayed to the user
getOrdersCurrentErrored({error})
))
)
)
))

And then display the error inside an effect or inside a component.

  displayError$ = createEffect(() => this.actions$.pipe(
ofType(getOrdersCurrentErrored),
tap(({error}) => {
this.notifyService.displayError(error);
})
), {dispatch: false})

For errors, that were not handled inside the observable, custom error handler ErrorsHandler is written. It catches Every error in the app and send the report.

Index

Type Aliases

ContextError: { error: string; time: number; url: string }

Type declaration

  • error: string
  • time: number
  • url: string
ErrorsText: Record<LocaleId, Record<NormalizedError, HumanReadableError>>

A dictionary of human-readable errors, grouped locales.

HumanReadableError: string

Translated human-readable error with hints to the user.

LocaleId: string

The locale id, provided by LOCALE_ID injection token, according to the angular i18n

NormalizedError: string

Lowercase ASCII error message, identifying the error. For example 'not found', 'service unavailable', etc.

Variables

ERRORS_CONFIG: InjectionToken<ErrorsConfig> = ...
enUsCommonErrors: Record<NormalizedError, HumanReadableError> = ...
ruRuCommonErrors: Record<NormalizedError, HumanReadableError> = ...

Functions

  • assert(v: any, msg: string): asserts v
  • stringifyError(error: any): string
  • unreachable<T>(msg: string): T
  • Used to mark unreachable code statements. For example:

    function boatCapacity(type: 'long' | 'short'): number {
    if (type === 'long') {
    return 16
    } else if (type === 'short') {
    return 4
    } else {
    return unreachable('unknown boat type')
    }
    }

    Type Parameters

    • T

    Parameters

    • msg: string

      a error message to be thrown

    Returns T

Generated using TypeDoc