Use when on throw on error to avoid context loss

This commit is contained in:
Matt Nadareski
2021-01-12 15:54:14 -08:00
parent 2e674e23ca
commit 3317aef790
20 changed files with 40 additions and 136 deletions

View File

@@ -96,15 +96,10 @@ namespace SabreTools.DatFiles.Formats
// Now process and add the rom
ParseAddHelper(rom, statsOnly);
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
string message = $"'{filename}' - There was an error parsing line {svr.LineNumber} '{svr.CurrentLine}'";
logger.Error(ex, message);
if (throwOnError)
{
svr.Dispose();
throw new Exception(message, ex);
}
}
}
@@ -171,10 +166,9 @@ namespace SabreTools.DatFiles.Formats
svw.Dispose();
fs.Dispose();
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}

View File

@@ -81,15 +81,10 @@ namespace SabreTools.DatFiles.Formats
break;
}
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
string message = $"'{filename}' - There was an error parsing line {cmpr.LineNumber} '{cmpr.CurrentLine}'";
logger.Error(ex, message);
if (throwOnError)
{
cmpr.Dispose();
throw new Exception(message, ex);
}
}
}
@@ -507,10 +502,9 @@ namespace SabreTools.DatFiles.Formats
cmpw.Dispose();
fs.Dispose();
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}

View File

@@ -64,15 +64,10 @@ namespace SabreTools.DatFiles.Formats
break;
}
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
string message = $"'{filename}' - There was an error parsing line {cmpr.LineNumber} '{cmpr.CurrentLine}'";
logger.Error(ex, message);
if (throwOnError)
{
cmpr.Dispose();
throw new Exception(message, ex);
}
}
}
@@ -334,10 +329,9 @@ namespace SabreTools.DatFiles.Formats
cmpw.Dispose();
fs.Dispose();
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}

View File

@@ -87,15 +87,10 @@ namespace SabreTools.DatFiles.Formats
// Now process and add the rom
ParseAddHelper(rom, statsOnly);
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
string message = $"'{filename}' - There was an error parsing line {svr.LineNumber} '{svr.CurrentLine}'";
logger.Error(ex, message);
if (throwOnError)
{
svr.Dispose();
throw new Exception(message, ex);
}
}
}
@@ -159,10 +154,9 @@ namespace SabreTools.DatFiles.Formats
svw.Dispose();
fs.Dispose();
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}

View File

@@ -88,15 +88,10 @@ namespace SabreTools.DatFiles.Formats
// Now process and add the rom
ParseAddHelper(rom, statsOnly);
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
string message = $"'{filename}' - There was an error parsing at position {sr.BaseStream.Position}";
logger.Error(ex, message);
if (throwOnError)
{
sr.Dispose();
throw new Exception(message, ex);
}
}
}
@@ -160,10 +155,9 @@ namespace SabreTools.DatFiles.Formats
svw.Dispose();
fs.Dispose();
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}

View File

@@ -244,15 +244,10 @@ namespace SabreTools.DatFiles.Formats
}
}
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
string message = $"'{filename}' - There was an error parsing at position {sr.BaseStream.Position}";
logger.Error(ex, message);
if (throwOnError)
{
sr.Dispose();
throw new Exception(message, ex);
}
}
}
}
@@ -323,10 +318,9 @@ namespace SabreTools.DatFiles.Formats
sw.Dispose();
fs.Dispose();
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}

View File

@@ -254,14 +254,9 @@ namespace SabreTools.DatFiles.Formats
}
}
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Warning(ex, $"Exception found while parsing '{filename}'");
if (throwOnError)
{
xtr.Dispose();
throw ex;
}
// For XML errors, just skip the affected node
xtr?.Read();
@@ -1416,10 +1411,9 @@ namespace SabreTools.DatFiles.Formats
xtw.Dispose();
fs.Dispose();
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}

View File

@@ -208,14 +208,9 @@ namespace SabreTools.DatFiles.Formats
}
}
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Warning(ex, $"Exception found while parsing '{filename}'");
if (throwOnError)
{
xtr.Dispose();
throw ex;
}
// For XML errors, just skip the affected node
xtr?.Read();
@@ -848,10 +843,9 @@ namespace SabreTools.DatFiles.Formats
xtw.Dispose();
fs.Dispose();
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}

View File

@@ -80,10 +80,9 @@ namespace SabreTools.DatFiles.Formats
sw.Dispose();
fs.Dispose();
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}

View File

@@ -78,14 +78,9 @@ namespace SabreTools.DatFiles.Formats
}
}
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Warning(ex, $"Exception found while parsing '{filename}'");
if (throwOnError)
{
xtr.Dispose();
throw ex;
}
// For XML errors, just skip the affected node
xtr?.Read();
@@ -729,10 +724,9 @@ namespace SabreTools.DatFiles.Formats
xtw.Dispose();
fs.Dispose();
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}

View File

@@ -94,14 +94,9 @@ namespace SabreTools.DatFiles.Formats
}
}
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Warning(ex, $"Exception found while parsing '{filename}'");
if (throwOnError)
{
xtr.Dispose();
throw ex;
}
// For XML errors, just skip the affected node
xtr?.Read();
@@ -610,10 +605,9 @@ namespace SabreTools.DatFiles.Formats
xtw.Dispose();
fs.Dispose();
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}

View File

@@ -77,15 +77,10 @@ namespace SabreTools.DatFiles.Formats
}
}
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
string message = $"'{filename}' - There was an error parsing line {ir.LineNumber} '{ir.CurrentLine}'";
logger.Error(ex, message);
if (throwOnError)
{
ir.Dispose();
throw new Exception(message, ex);
}
}
ir.Dispose();
@@ -424,10 +419,9 @@ namespace SabreTools.DatFiles.Formats
iw.Dispose();
fs.Dispose();
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}

View File

@@ -71,10 +71,9 @@ namespace SabreTools.DatFiles.Formats
}
}
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Warning($"Exception found while parsing '{filename}': {ex}");
if (throwOnError) throw ex;
}
jtr.Close();
@@ -404,10 +403,9 @@ namespace SabreTools.DatFiles.Formats
jtw.Close();
fs.Dispose();
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}

View File

@@ -76,14 +76,9 @@ namespace SabreTools.DatFiles.Formats
}
}
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Warning(ex, $"Exception found while parsing '{filename}'");
if (throwOnError)
{
xtr.Dispose();
throw ex;
}
// For XML errors, just skip the affected node
xtr?.Read();
@@ -253,10 +248,9 @@ namespace SabreTools.DatFiles.Formats
xtw.Dispose();
fs.Dispose();
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}

View File

@@ -104,15 +104,10 @@ namespace SabreTools.DatFiles.Formats
ParseAddHelper(datItem, statsOnly);
}
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
string message = $"'{filename}' - There was an error parsing line {svr.LineNumber} '{svr.CurrentLine}'";
logger.Error(ex, message);
if (throwOnError)
{
svr.Dispose();
throw new Exception(message, ex);
}
}
}
@@ -179,10 +174,9 @@ namespace SabreTools.DatFiles.Formats
svw.Dispose();
fs.Dispose();
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}

View File

@@ -144,14 +144,9 @@ namespace SabreTools.DatFiles.Formats
}
}
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Warning(ex, $"Exception found while parsing '{filename}'");
if (throwOnError)
{
xtr.Dispose();
throw ex;
}
// For XML errors, just skip the affected node
xtr?.Read();
@@ -657,10 +652,9 @@ namespace SabreTools.DatFiles.Formats
xtw.Dispose();
fs.Dispose();
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}

View File

@@ -79,10 +79,9 @@ namespace SabreTools.DatTools
if (cleaner?.KeepEmptyGames == false)
datFile.Items.ClearEmpty();
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}
@@ -168,10 +167,9 @@ namespace SabreTools.DatTools
}
}
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}
@@ -242,10 +240,9 @@ namespace SabreTools.DatTools
datFile.Items[key] = items;
}
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}
@@ -291,10 +288,9 @@ namespace SabreTools.DatTools
break;
}
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}
@@ -388,10 +384,9 @@ namespace SabreTools.DatTools
datFile.Items.AddRange(key, newItems);
});
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Warning(ex.ToString());
if (throwOnError) throw ex;
}
}

View File

@@ -108,10 +108,9 @@ namespace SabreTools.DatTools
var parsingDatFile = DatFile.Create(currentPathFormat, datFile, quotes);
parsingDatFile?.ParseFile(currentPath, indexId, keep, statsOnly: statsOnly, throwOnError: throwOnError);
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex, $"Error with file '{currentPath}'");
if (throwOnError) throw ex;
}
}

View File

@@ -83,18 +83,16 @@ namespace SabreTools.DatTools
{
DatFile.Create(datFormat, datFile, quotes)?.WriteToFile(outfile, ignoreblanks, throwOnError);
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex, $"Datfile {outfile} could not be written out");
if (throwOnError) throw ex;
}
});
}
catch (Exception ex)
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
if (throwOnError) throw ex;
return false;
}

View File

@@ -258,11 +258,10 @@ namespace SabreTools.Logging
_log.WriteLine((AppendPrefix ? $"{loglevel} - {DateTime.Now} - " : string.Empty) + output);
}
}
catch (Exception ex)
catch (Exception ex) when (ThrowOnError)
{
Console.WriteLine(ex);
Console.WriteLine("Could not write to log file!");
if (ThrowOnError) throw ex;
return;
}
}