Skip to content

Commit

Permalink
refactor: add an example of fallback if error is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelfras committed May 9, 2024
1 parent 398d7f2 commit 8e05980
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@
*
* SPDX-License-Identifier: Apache-2.0
*/

export * from '../../model/error-action';
export * from './effects-error-handler.module';
export * from './effects-error-handler.service';
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { Action } from '@ngrx/store';
import { ErrorAction } from '../../../model';
import { EntityId, entityMeta, EntityMeta } from '../entity/entity.action';
import {
failMeta,
Expand Down Expand Up @@ -75,11 +76,27 @@ export class EntityLoadAction implements EntityLoaderAction {
}
}

export class EntityFailAction implements EntityLoaderAction {
export class EntityFailAction implements EntityLoaderAction, ErrorAction {
type = ENTITY_FAIL_ACTION;
readonly meta: EntityLoaderMeta;

error: any;

/**@deprecated */
constructor(entityType: string, id: EntityId);
// eslint-disable-next-line @typescript-eslint/unified-signatures
constructor(entityType: string, id: EntityId, error: any);
constructor(entityType: string, id: EntityId, error?: any) {
this.meta = entityFailMeta(entityType, id, error);
if (error) {
this.error = error;
} else {
// fallback needed only until we make the `error` property a required one
const stringId = Array.isArray(id) ? `[${id.join(',')}]` : id;
this.error = new Error(
`EntityFailAction - entityType: ${entityType}, id: ${stringId}`
);
}
}
}

Expand Down

0 comments on commit 8e05980

Please sign in to comment.