Skip to content

Commit

Permalink
feat(stark-core): update error handling actions style
Browse files Browse the repository at this point in the history
Adapt showcase code

BREAKING CHANGE:
Due to an improvement on how actions are defined, the enum `StarkErrorHandlingActionsTypes`
became obsolete so it has been removed.

As a result, the following actions have been changed:
- `StarkUnhandledError(public error: any)`
  -> `StarkErrorHandlingActions.unhandledError({ error: any })`

And also the previous union type has been replaced:
`StarkErrorHandlingActions` -> `StarkErrorHandlingActions.Types`.

Change in effect:

```typescript
// Before
@effect({ dispatch: false })
public starkUnhandledError$(): Observable<void> {
    return this.actions$.pipe(
        ofType<StarkUnhandledError>(StarkErrorHandlingActionTypes.UNHANDLED_ERROR),
        map((action: StarkUnhandledError) => {
            // some logic
        })
    );
}

// After
public starkUnhandledError$ = createEffect(
    () => this.actions$.pipe(
        ofType(StarkErrorHandlingActions.unhandledError),
        map((action) => {
            // some logic
        })
    ),
    { dispatch: false }
);
```

Change in `action` usage:

```typescript
// Before
this.store.dispatch(new StarkUnhandledError(error));

// After
this.store.dispatch(StarkErrorHandlingActions.unhandledError({ error: error }));
```
  • Loading branch information
SuperITMan committed May 3, 2021
1 parent 8418efc commit 727c244
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 46 deletions.
3 changes: 2 additions & 1 deletion packages/stark-core/src/modules/error-handling/actions.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./actions/error-handling.actions";
import * as StarkErrorHandlingActions from "./actions/error-handling.actions";
export { StarkErrorHandlingActions };
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
import { Action } from "@ngrx/store";
import { createAction, props, union } from "@ngrx/store";

/**
* All the StarkErrorHandling action types
* Action that requires to display an error message as a toast notification
*
* Parameter:
* - error - The error to display
*/
export enum StarkErrorHandlingActionTypes {
UNHANDLED_ERROR = "[StarkErrorHandling] Unhandled Error"
}
export const unhandledError = createAction("[StarkErrorHandling] Unhandled Error", props<{ error: any }>());

/**
* Action that requires to display an error message as a toast notification
* @returns The created action object
* @ignore
*/
export class StarkUnhandledError implements Action {
/**
* The type of action
*/
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;
const all = union({ unhandledError });
export type Types = typeof all;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ErrorHandler, Injectable, Injector } from "@angular/core";
import { Store } from "@ngrx/store";
import { StarkCoreApplicationState } from "../../../common/store";
import { StarkUnhandledError } from "../actions";
import { StarkErrorHandlingActions } from "../actions";
import { STARK_LOGGING_SERVICE, StarkLoggingService } from "../../logging/services";

/**
Expand All @@ -28,12 +28,12 @@ export class StarkErrorHandler implements ErrorHandler {
public constructor(private injector: Injector) {}

/**
* Dispatches an {@link StarkUnhandledError} action which the user can then handle
* Dispatches an {@link StarkErrorHandlingActions.unhandledError} action 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));
this.applicationStore.dispatch(StarkErrorHandlingActions.unhandledError({ error }));
}

/**
Expand Down
42 changes: 21 additions & 21 deletions showcase/src/app/shared/effects/stark-error-handling.effects.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Injectable, Injector, NgZone } from "@angular/core";
import { Actions, Effect, ofType } from "@ngrx/effects";
import { Actions, createEffect, ofType } from "@ngrx/effects";
import { map } from "rxjs/operators";
import { Observable } from "rxjs";
import { STARK_TOAST_NOTIFICATION_SERVICE, StarkMessageType, StarkToastNotificationService } from "@nationalbankbelgium/stark-ui";
import { StarkErrorHandlingActionTypes, StarkUnhandledError } from "@nationalbankbelgium/stark-core";
import { StarkErrorHandlingActions } from "@nationalbankbelgium/stark-core";
import uniqueId from "lodash-es/uniqueId";

/**
Expand All @@ -21,24 +20,25 @@ export class StarkErrorHandlingEffects {
*/
public constructor(private actions$: Actions, private injector: Injector, private zone: NgZone) {}

@Effect({ dispatch: false })
public starkUnhandledError$(): Observable<void> {
return this.actions$.pipe(
ofType<StarkUnhandledError>(StarkErrorHandlingActionTypes.UNHANDLED_ERROR),
map((action: StarkUnhandledError) => {
this.zone.run(() => {
this.toastNotificationService
.show({
id: uniqueId(),
type: StarkMessageType.ERROR,
key: action.error.toString(),
code: "Unhandled error - no code"
})
.subscribe();
});
})
);
}
public starkUnhandledError$ = createEffect(
() =>
this.actions$.pipe(
ofType(StarkErrorHandlingActions.unhandledError),
map((action) => {
this.zone.run(() => {
this.toastNotificationService
.show({
id: uniqueId(),
type: StarkMessageType.ERROR,
key: action.error.toString(),
code: "Unhandled error - no code"
})
.subscribe();
});
})
),
{ dispatch: false }
);

/**
* Gets the StarkToastNotificationService from the Injector.
Expand Down

0 comments on commit 727c244

Please sign in to comment.