简体   繁体   English

RxJS catchError运算符未在Promise创建的可观察对象上捕获错误

[英]RxJS catchError operator does not catch an error on an observable created from a Promise

I am building an ionic 4 / angular 8 app using rxjs 6.5.1. 我正在使用rxjs 6.5.1构建ionic 4 / angular 8应用程序。

Here I have a Promise that is converted to an observable that is used further down the pipe in order to download a file via a file plugin for ionic. 在这里,我有一个Promise,它被转换为可观察的东西,该东西可在管道中进一步使用,以便通过用于ionic的文件插件下载文件。 As seen at the bottom of the code, I create an array of observables, which will be subscribed to in case someFlag is set. 如代码底部所示,我创建了一个可观察someFlag数组,以防someFlag设置someFlag The issue with this code is that at the point of pushing the observables to the array, the code in getLocationForFile(fileName) gets executed and I get the following error in the console: 这段代码的问题是,在将可观察对象推送到数组时,执行了getLocationForFile(fileName)的代码,并且在控制台中出现以下错误:

ERROR Error: Uncaught (in promise): FileError: {"code":12,"message":"PATH_EXISTS_ERR"} . ERROR Error: Uncaught (in promise): FileError: {"code":12,"message":"PATH_EXISTS_ERR"} Such error is perfectly fine to happen in case the path exists. 如果路径存在,则发生这种错误是非常好的。 The problem is that this error is not caught in the catchError operator of the getLocationForFile method. 问题在于,此错误未在getLocationForFile方法的catchError运算符中捕获。 The interesting part is that in case someFlag is set, the forkJoin will be triggered and the very same error will be then caught in the catchError in getLocationForFile(fileName), but the error of uncaught promise error will still be there. 有趣的是,在案件someFlag被设置,forkJoin将被触发,非常相同的错误将被然后夹在getLocationForFile(文件名)的catchError,但未被捕获的承诺错误的错误依然存在。

private createDirectory(dirName) {
    return from(this.file.createDir(path, dirName))
}

public getLocationForFile(fileName: string) {
    return  this.createDir().pipe(
      switchMap(dirEntry => of(dirEntry.nativeURL + fileName)),
      catchError(_ => of(""))
    )
}

public downloadFile(fileName) {
    return this.getLocationForFile(fileName).pipe(
        switchMap(fileLocation => return this.file.downloadFile(url, {}, {}, fileName)),
        catchError(error => of(handleError(error));
    )
}

let filesToDownload = [];

for(let fileName of fileNames) {
    filesToDownload.push(downloadFile(fileName))
}

if(someFlag){
    forkJoin(filesToDownload).subscribe(results => {
        handleResults(results)
    })
}

In case I rewrite the createDirectory(dirName) like this 如果我这样重写createDirectory(dirName)

private createDirectory(dirName) {
    return from(this.file.createDir(path, dirName)
    .then(result => of(result))
    .catch(error => throwError(error))
    )
}

then the error is handled and I do not get the Uncaugh in promise error. 然后处理该错误,并且我没有得到Uncaugh in promise错误。

So one question is why the catchError in the createDirectory method does not catch the error that occurs when I push the observable to the array and the other one is how to fix it (I do not want to use the workaround from the last code snippet). 因此,一个问题是为什么createDirectory方法中的catchError不能捕获将可观察对象推送到数组时发生的错误,而另一个是如何修复它(我不想使用上一个代码片段中的解决方法) 。

Thanks! 谢谢!

Maybe fix your promise code first will solve your problem 也许先修复您的诺言代码会解决您的问题

there is no need to catch, let the error flow through 无需捕获,让错误流过

private createDirectory(dirName) {
    return defer(()=>this.file.createDir(path, dirName))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM