c# - Flattening of AggregateExceptions for Processing -
i'm running few issues call flatten
on aggregateexception
, inside there still aggregateexception
! means being propagated chain , being rolled aggregateexception
. there way recursively flatten inner aggregateexceptions? usually, i'll use handle delegate process these, returns false if there inner aggregateexceeption. not handling these properly?
edit: since calling flatten, appears issue it's not being caught until way later in callstack. here code i'm calling flatten(). use in stack trace method called writeexceptionrecord(string, fileinfo):
do { try { using (var stream = file.open(filemode.append, fileaccess.write, fileshare.none)) { using (streamwriter writer = new streamwriter(stream)) { await writer.writelineasync(data); } } } catch (aggregateexception ex) { ex.flatten().handle((x) => { if (x ioexception) { retryneeded = true; retryleft--; thread.sleep(500); return true; } logger.errorexception("could not write exception file: " + data, ex); return false; }); } } while (retryneeded && retryleft > 0);
however, stack trace shows it's not being caught here. instead it's being caught way later call stack. below trace identifying information removed security reasons:
system.aggregateexception: 1 or more errors occurred. ---> system.aggregateexception: 1 or more errors occurred. ---> system.io.ioexception: process cannot access file 'x:\production\productionbatches\dataentry\j\pd\exception.csv' because being used process. @ system.io.__error.winioerror(int32 errorcode, string maybefullpath) @ system.io.filestream.init(string path, filemode mode, fileaccess access, int32 rights, boolean userights, fileshare share, int32 buffersize, fileoptions options, security_attributes secattrs, string msgpath, boolean bfromproxy, boolean uselongpath, boolean checkhost) @ system.io.filestream..ctor(string path, filemode mode, fileaccess access, fileshare share) @ system.io.fileinfo.open(filemode mode, fileaccess access, fileshare share) @ pdi.loadfileprocessing.<writeexceptionrecord>d__21.movenext() in c:\users\xyz\development\pdi\pdi\loadfileprocessing.cs:line 328 --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter.getresult() @ pdi.loadfileprocessing.<exceptionrecordprocessing>d__17.movenext() in c:\users\xyz\development\pdi\pdi\loadfileprocessing.cs:line 316 --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter.getresult() @ pdi.processpipeline.<>c__displayclass9.<<processbatch>b__2>d__13.movenext() in c:\users\xyz\development\pdi\pdi\processpipeline.cs:line 61 --- end of inner exception stack trace --- --- end of inner exception stack trace --- ---> (inner exception #0) system.aggregateexception: 1 or more errors occurred. ---> system.io.ioexception: process cannot access file 'x:\production\productionbatches\dataentry\j\pd\exception.csv' because being used process. @ system.io.__error.winioerror(int32 errorcode, string maybefullpath) @ system.io.filestream.init(string path, filemode mode, fileaccess access, int32 rights, boolean userights, fileshare share, int32 buffersize, fileoptions options, security_attributes secattrs, string msgpath, boolean bfromproxy, boolean uselongpath, boolean checkhost) @ system.io.filestream..ctor(string path, filemode mode, fileaccess access, fileshare share) @ system.io.fileinfo.open(filemode mode, fileaccess access, fileshare share) @ peopledocimporter.loadfileprocessing.<writeexceptionrecord>d__21.movenext() in c:\users\xyz\development\pdi\pdi\loadfileprocessing.cs:line 328 --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter.getresult() @ pdi.loadfileprocessing.<exceptionrecordprocessing>d__17.movenext() in c:\users\xyz\development\pdi\pdi\loadfileprocessing.cs:line 316 --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter.getresult() @ pdi.processpipeline.<>c__displayclass9.<<processbatch>b__2>d__13.movenext() in c:\users\xyz\development\pdi\pdi\processpipeline.cs:line 61 --- end of inner exception stack trace --- ---> (inner exception #0) system.io.ioexception: process cannot access file 'x:\production\productionbatches\dataentry\j\pd\exception.csv' because being used process. @ system.io.__error.winioerror(int32 errorcode, string maybefullpath) @ system.io.filestream.init(string path, filemode mode, fileaccess access, int32 rights, boolean userights, fileshare share, int32 buffersize, fileoptions options, security_attributes secattrs, string msgpath, boolean bfromproxy, boolean uselongpath, boolean checkhost) @ system.io.filestream..ctor(string path, filemode mode, fileaccess access, fileshare share) @ system.io.fileinfo.open(filemode mode, fileaccess access, fileshare share) @ pdi.loadfileprocessing.<writeexceptionrecord>d__21.movenext() in c:\users\xyz\development\pdi\pdi\loadfileprocessing.cs:line 328 --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter.getresult() @ pdi.loadfileprocessing.<exceptionrecordprocessing>d__17.movenext() in c:\users\xyz\development\pdi\pdi\loadfileprocessing.cs:line 316 --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter.getresult() @ pdi.processpipeline.<>c__displayclass9.<<processbatch>b__2>d__13.movenext() in c:\users\xyz\development\pdi\pdi\processpipeline.cs:line 61<--- <--- system.aggregateexception: 1 or more errors occurred. ---> system.aggregateexception: 1 or more errors occurred. ---> system.io.ioexception: process cannot access file 'x:\production\productionbatches\dataentry\j\pd\exception.csv' because being used process. @ system.io.__error.winioerror(int32 errorcode, string maybefullpath) @ system.io.filestream.init(string path, filemode mode, fileaccess access, int32 rights, boolean userights, fileshare share, int32 buffersize, fileoptions options, security_attributes secattrs, string msgpath, boolean bfromproxy, boolean uselongpath, boolean checkhost) @ system.io.filestream..ctor(string path, filemode mode, fileaccess access, fileshare share) @ system.io.fileinfo.open(filemode mode, fileaccess access, fileshare share) @ pdi.loadfileprocessing.<writeexceptionrecord>d__21.movenext() in c:\users\xyz\development\pdi\pdi\loadfileprocessing.cs:line 328 --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter.getresult() @ pdi.loadfileprocessing.<exceptionrecordprocessing>d__17.movenext() in c:\users\xyz\development\pdi\pdi\loadfileprocessing.cs:line 316 --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter.getresult() @ pdi.processpipeline.<>c__displayclass9.<<processbatch>b__2>d__13.movenext() in c:\users\xyz\development\pdi\pdi\processpipeline.cs:line 61 --- end of inner exception stack trace --- ---> (inner exception #0) system.aggregateexception: 1 or more errors occurred. ---> system.io.ioexception: process cannot access file 'x:\j\pd\exception.csv' because being used process. @ system.io.__error.winioerror(int32 errorcode, string maybefullpath) @ system.io.filestream.init(string path, filemode mode, fileaccess access, int32 rights, boolean userights, fileshare share, int32 buffersize, fileoptions options, security_attributes secattrs, string msgpath, boolean bfromproxy, boolean uselongpath, boolean checkhost) @ system.io.filestream..ctor(string path, filemode mode, fileaccess access, fileshare share) @ system.io.fileinfo.open(filemode mode, fileaccess access, fileshare share) @ pdi.loadfileprocessing.<writeexceptionrecord>d__21.movenext() in c:\users\xyz\development\pdi\pdi\loadfileprocessing.cs:line 328 --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter.getresult() @ pdi.loadfileprocessing.<exceptionrecordprocessing>d__17.movenext() in c:\users\xyz\development\pdi\pdi\loadfileprocessing.cs:line 316 --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter.getresult() @ pdi.processpipeline.<>c__displayclass9.<<processbatch>b__2>d__13.movenext() in c:\users\xyz\development\pdi\pdi\processpipeline.cs:line 61 --- end of inner exception stack trace --- ---> (inner exception #0) system.io.ioexception: process cannot access file 'x:\production\productionbatches\dataentry\j\pd\exception.csv' because being used process. @ system.io.__error.winioerror(int32 errorcode, string maybefullpath) @ system.io.filestream.init(string path, filemode mode, fileaccess access, int32 rights, boolean userights, fileshare share, int32 buffersize, fileoptions options, security_attributes secattrs, string msgpath, boolean bfromproxy, boolean uselongpath, boolean checkhost) @ system.io.filestream..ctor(string path, filemode mode, fileaccess access, fileshare share) @ system.io.fileinfo.open(filemode mode, fileaccess access, fileshare share) @ pdi.loadfileprocessing.<writeexceptionrecord>d__21.movenext() in c:\users\xyz\development\pdi\pdi\loadfileprocessing.cs:line 328 --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter.getresult() @ pdi.loadfileprocessing.<exceptionrecordprocessing>d__17.movenext() in c:\users\xyz\development\pdi\pdi\loadfileprocessing.cs:line 316 --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter.getresult() @ pdi.processpipeline.<>c__displayclass9.<<processbatch>b__2>d__13.movenext() in c:\users\xyz\development\pdi\pdi\processpipeline.cs:line 61<---
by way: being called tpl-dataflow blocks.
yes, there's you're asking for:
aggreggateexception.flatten()
will go through , compress down single aggregateexception. can use loop through inner exceptions this:
try { // dangerous } catch (aggregateexception ae) { foreach(var innerexception in ae.flatten().innerexceptions) { // handle error } }
msdn link: http://msdn.microsoft.com/en-us/library/system.aggregateexception.flatten.aspx
Comments
Post a Comment