[英]RxJS catchError operator does not catch an error on an observable created from a Promise
我正在使用rxjs 6.5.1构建ionic 4 / angular 8应用程序。
在这里,我有一个Promise,它被转换为可观察的东西,该东西可在管道中进一步使用,以便通过用于ionic的文件插件下载文件。 如代码底部所示,我创建了一个可观察someFlag
数组,以防someFlag
设置someFlag
。 这段代码的问题是,在将可观察对象推送到数组时,执行了getLocationForFile(fileName)
的代码,并且在控制台中出现以下错误:
ERROR Error: Uncaught (in promise): FileError: {"code":12,"message":"PATH_EXISTS_ERR"}
。 如果路径存在,则发生这种错误是非常好的。 问题在于,此错误未在getLocationForFile
方法的catchError运算符中捕获。 有趣的是,在案件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)
})
}
如果我这样重写createDirectory(dirName)
private createDirectory(dirName) {
return from(this.file.createDir(path, dirName)
.then(result => of(result))
.catch(error => throwError(error))
)
}
然后处理该错误,并且我没有得到Uncaugh in promise错误。
因此,一个问题是为什么createDirectory
方法中的catchError不能捕获将可观察对象推送到数组时发生的错误,而另一个是如何修复它(我不想使用上一个代码片段中的解决方法) 。
谢谢!
也许先修复您的诺言代码会解决您的问题
无需捕获,让错误流过
private createDirectory(dirName) {
return defer(()=>this.file.createDir(path, dirName))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.