Search code examples
angulartypescriptrxjsrxjs-pipeable-operators

Looking for an RxJS operator


I am using an Angular service to allow the user to upload files.

The implementation of the service is working ; my question is about RxJS and its pipeable operators, but here is the service signature just in case :

askUserForFile(): Observable<File>;
toBase64(file: File): Observable<string>;
isFileValid(file: File, configuration?: { size?: number, extensions?: string | string[] }): boolean;

The call to this service is as follows :

  this.fileService
    .askUserForFile()
    .pipe(
      // this is the operator I'm looking for 
      unknownOperator(file => this.fileService.isFileValid(file, { extensions: ['txt'] }))
      mergeMap(file => {
        fichier.filename = file.name;
        return this.fileService.toBase64(file);
      }))
    .subscribe(base64 => {
      fichier.base64 = base64;
      // Rest of my code
    }, error => {/* error handling */});

I would like to find an operator in place of unknownOperator that would throw an error if the condition isn't met.

I've tried with

  • filter : if the condition isn't met, the code stops after it,
  • map : the code continues even if an error is thrown with throwError

I thought about using the following piping

.pipe(
  map(...),
  catchError(...),
  mergeMap(...)
)

Which I think might work, but I would like to find (if possible) an operator that shortens this piping.

Is it possible ? if not, is there a better piping ?


Solution

  • You can use just map but you have to throw an exception with throw keyword, not returning throwError because this just creates another Observable. You could also use mergeMap and then throwError would work but it's probably unnecessarily complicated.

    map(val => {
      if (val === 42) {
        throw new Error(`It's broken`);
      }
      return val;
    })
    

    oneliner:

    mergeMap(val => val === 42 ? throwError(new Error(`It's broken`)) : of(val))