-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stark-core): implementation of a custom error handler
- Loading branch information
Showing
16 changed files
with
286 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Stark Default Error Handling | ||
|
||
Stark provides its own error handler, StarkErrorHandler, that overrides [ Angular's default ErrorHandler](https://angular.io/api/core/ErrorHandler). | ||
|
||
This handler will dispatch a StarkUnhandledError action to the application Store which you can treat as you want. | ||
|
||
## StarkErrorHandlingModule | ||
|
||
If you want to use this handler, you simply have to import StarkErrorHandlingModule from stark-core in your `app.module.ts` file. | ||
|
||
```typescript | ||
import {StarkErrorHandlingModule} from "@nationalbankbelgium/stark-core"; | ||
|
||
@NgModule({ | ||
bootstrap: ... | ||
declarations: ... | ||
imports: [ | ||
StarkErrorHandlingModule.forRoot(), | ||
... | ||
] | ||
}) | ||
``` | ||
|
||
## Effects definition | ||
|
||
To define what to do when an error happens, you can simply create a new effects file, like in the following example: | ||
|
||
```typescript | ||
... | ||
import {StarkErrorHandlingActionTypes, StarkUnhandledError} from "@nationalbankbelgium/stark-core"; | ||
|
||
/** | ||
* This class is used to determine what to do with an error | ||
*/ | ||
@Injectable() | ||
export class StarkErrorHandlingEffects { | ||
|
||
public constructor( | ||
private actions$: Actions | ||
) {} | ||
|
||
@Effect() | ||
public doSomething(): Observable<void> { | ||
return this.actions$.pipe( | ||
ofType<StarkUnhandledError>(StarkErrorHandlingActionTypes.UNHANDLED_ERROR), | ||
map((action: StarkUnhandledError) => { | ||
//DO SOMETHING | ||
}) | ||
); | ||
} | ||
} | ||
``` | ||
|
||
If you do create that file, don't forget to import it in your `app.module.ts` file: | ||
|
||
```typescript | ||
... | ||
import { StarkErrorHandlingEffects } from "./shared/effects/stark-error-handler.effects"; | ||
|
||
@NgModule({ | ||
bootstrap: ... | ||
declarations: ... | ||
imports: [ | ||
... | ||
EffectsModule.forRoot([StarkErrorHandlingEffects]), | ||
... | ||
] | ||
}) | ||
``` |
2 changes: 1 addition & 1 deletion
2
packages/stark-core/src/common/store/stark-core-application-state.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { StarkLoggingState } from "../../modules/logging/reducers"; | ||
import { StarkSessionState } from "../../modules/session/reducers"; | ||
import { StarkSettingsState } from "../../modules/settings/reducers"; | ||
|
||
/** | ||
* Interface defining the shape of the application state of Stark Core (i.e., what's stored in Redux by Stark) | ||
*/ | ||
export interface StarkCoreApplicationState extends StarkLoggingState, StarkSessionState, StarkSettingsState { | ||
// FIXME: still needed? | ||
// starkApplicationMetadata: StarkApplicationMetadata; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./error-handling/actions"; | ||
export * from "./error-handling/handlers"; | ||
export * from "./error-handling/error-handling.module"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./actions/error-handling.actions"; |
28 changes: 28 additions & 0 deletions
28
packages/stark-core/src/modules/error-handling/actions/error-handling.actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Action } from "@ngrx/store"; | ||
|
||
/** | ||
* All the StarkErrorHandling action types | ||
*/ | ||
export enum StarkErrorHandlingActionTypes { | ||
UNHANDLED_ERROR = "[StarkErrorHandling] Unhandled Error" | ||
} | ||
|
||
/** | ||
* Action that requires to display an error message as a toast notification | ||
* @returns The created action object | ||
*/ | ||
export class StarkUnhandledError implements Action { | ||
/** | ||
* The type of action | ||
* @link StarkErrorHandlingActionTypes | ||
*/ | ||
public readonly type: StarkErrorHandlingActionTypes.UNHANDLED_ERROR = StarkErrorHandlingActionTypes.UNHANDLED_ERROR; | ||
|
||
/** | ||
* Class constructor | ||
* @param error - the error to display | ||
*/ | ||
public constructor(public error: any) {} | ||
} | ||
|
||
export type StarkErrorHandlingActions = StarkUnhandledError; |
38 changes: 38 additions & 0 deletions
38
packages/stark-core/src/modules/error-handling/error-handling.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { ErrorHandler, ModuleWithProviders, NgModule, Optional, SkipSelf } from "@angular/core"; | ||
import { StarkErrorHandler } from "./handlers/error-handler"; | ||
|
||
@NgModule({}) | ||
export class StarkErrorHandlingModule { | ||
/** | ||
* Instantiates the services only once since they should be singletons | ||
* so the forRoot() should be called only by the AppModule | ||
* @link https://angular.io/guide/singleton-services#forroot | ||
* @returns a module with providers | ||
*/ | ||
public static forRoot(): ModuleWithProviders { | ||
return { | ||
ngModule: StarkErrorHandlingModule, | ||
providers: [ | ||
{ | ||
provide: ErrorHandler, | ||
useClass: StarkErrorHandler | ||
} | ||
] | ||
}; | ||
} | ||
|
||
/** | ||
* Prevents this module from being re-imported | ||
* @link https://angular.io/guide/singleton-services#prevent-reimport-of-the-coremodule | ||
* @param parentModule - the parent module | ||
*/ | ||
public constructor( | ||
@Optional() | ||
@SkipSelf() | ||
parentModule: StarkErrorHandlingModule | ||
) { | ||
if (parentModule) { | ||
throw new Error("StarkErrorHandlingModule is already loaded. Import it in the AppModule only"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./handlers/error-handler"; |
48 changes: 48 additions & 0 deletions
48
packages/stark-core/src/modules/error-handling/handlers/error-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { ErrorHandler, Injectable, Injector } from "@angular/core"; | ||
import { Store } from "@ngrx/store"; | ||
import { StarkCoreApplicationState } from "../../../common/store"; | ||
import { StarkUnhandledError } from "../actions"; | ||
import { STARK_LOGGING_SERVICE, StarkLoggingService } from "../../logging/services"; | ||
|
||
@Injectable() | ||
export class StarkErrorHandler implements ErrorHandler { | ||
private _starkLoggingService: StarkLoggingService; | ||
private _applicationStore: Store<StarkCoreApplicationState>; | ||
|
||
public constructor(private injector: Injector) {} | ||
|
||
/** | ||
* Thie method will dispatch an error method, which the user can then handle | ||
* @param error the encountered error | ||
*/ | ||
public handleError(error: any): void { | ||
this.starkLoggingService.error("StarkErrorHandler: an error has occurred : ", error); | ||
this.applicationStore.dispatch(new StarkUnhandledError(error)); | ||
} | ||
|
||
/** | ||
* Gets the StarkLoggingService from the Injector. | ||
* @throws When the service is not found (the StarkLoggingService is not provided in the app). | ||
*/ | ||
private get starkLoggingService(): StarkLoggingService { | ||
if (typeof this._starkLoggingService === "undefined") { | ||
this._starkLoggingService = this.injector.get<StarkLoggingService>(STARK_LOGGING_SERVICE); | ||
return this._starkLoggingService; | ||
} | ||
|
||
return this._starkLoggingService; | ||
} | ||
|
||
/** | ||
* Gets the Application Store from the Injector. | ||
* @throws When the Store is not found (the NGRX Store module is not imported in the app). | ||
*/ | ||
private get applicationStore(): Store<StarkCoreApplicationState> { | ||
if (typeof this._applicationStore === "undefined") { | ||
this._applicationStore = this.injector.get<Store<StarkCoreApplicationState>>(Store); | ||
return this._applicationStore; | ||
} | ||
|
||
return this._applicationStore; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.