A
A
Al2021-04-20 18:43:05
Angular
Al, 2021-04-20 18:43:05

How to more optimally handle the error of getting an entity in state NGXS?

Let's say there is some state:

@State<Some>({
  name: "some",
})
@Injectable()
export class SomeState {

  constructor(
    private readonly someService: SomeService) {
  }

  @Action(GetSomeAction)
  getPage(ctx: StateContext<Some>, action: GetSomeAction) {
    return this.someService.getSome(action.params).pipe(
      catchError((error: HttpErrorResponse) => {
       // так делать не хочется
        ctx.dispatch(new SomeErrorAction(error));
        return throwError(error);
      }),
      tap(res => {
         // set state
      })
    );
  }

}


Suppose there is such a subscription to an entity:
@Select(SomeState) private readonly some$: Observable<Some>;

some$.subscribe(some=> this.onSomeLoad(some), err => // тут хочется обработать ошибку, но это не работает);


So, the problem is that it is not clear how to process an error within one state, and is it possible to do so? In order not to write another state with an error in obtaining this entity and without making a special action for this, which we throw in case of an error in obtaining an object via http, as in the example above.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question