diff --git a/pwiz_tools/Skyline/Model/SmallMoleculeTransitionListReader.cs b/pwiz_tools/Skyline/Model/SmallMoleculeTransitionListReader.cs index 6a39469779..dbbd19f317 100644 --- a/pwiz_tools/Skyline/Model/SmallMoleculeTransitionListReader.cs +++ b/pwiz_tools/Skyline/Model/SmallMoleculeTransitionListReader.cs @@ -254,6 +254,15 @@ private bool ErrorAddingToExistingMoleculeGroup(ref SrmDocument document, Parsed return false; } + // Check for the various kinds of exceptions that may be thrown in the course of + // parsing sometimes-wonky user data + private bool IsParserException(Exception exception) + { + return exception is InvalidOperationException + || exception is InvalidDataException + || exception is ArgumentException; + } + // Returns true on error private bool ErrorFindingTransitionGroupForPrecursor(ref SrmDocument document, ParsedIonInfo precursor, Row row, PeptideGroupDocNode pepGroup, Adduct adduct, double precursorMonoMz, double precursorAverageMz, @@ -296,13 +305,9 @@ private bool ErrorFindingTransitionGroupForPrecursor(ref SrmDocument document, P _firstAddedPathPepGroup = _firstAddedPathPepGroup ?? pathGroup; } } - catch (InvalidDataException x) - { - errmsg = x.Message; - } - catch (InvalidOperationException x) // Adduct handling code can throw these + catch (Exception exception) when (IsParserException(exception)) { - errmsg = x.Message; + errmsg = exception.Message; } if (errmsg != null) @@ -1357,7 +1362,7 @@ private ParsedIonInfo ReadPrecursorOrProductColumns(SrmDocument document, adduct = DetermineAdductFromFormulaChargeAndMz(formula, charge.Value, mz); } } - catch (Exception e) + catch (Exception e) when (IsParserException(e)) { ShowTransitionError(new PasteError { @@ -1511,13 +1516,9 @@ private ParsedIonInfo ReadPrecursorOrProductColumns(SrmDocument document, } } } - catch (InvalidDataException x) + catch (Exception exception) when (IsParserException(exception)) { - massErrMsg = x.Message; - } - catch (InvalidOperationException x) // Adduct handling code can throw these - { - massErrMsg = x.Message; + massErrMsg = exception.Message; } if (massErrMsg != null) { @@ -1704,7 +1705,7 @@ private bool ProcessAdduct(Row row, int indexAdduct, int indexFormula, bool getP adduct = Adduct.FromStringAssumeChargeOnly(adductText); IonInfo.ApplyAdductToFormula(formula ?? string.Empty, adduct); // Just to see if it throws } - catch (Exception x) when ((x is InvalidOperationException) || (x is ArgumentException)) + catch (Exception x) when (IsParserException(x)) { ShowTransitionError(new PasteError { @@ -1831,7 +1832,7 @@ private PeptideDocNode GetMoleculePeptide(SrmDocument document, Row row, Peptide molecule = new CustomMolecule(parsedIonInfo.MonoMass, parsedIonInfo.AverageMass, shortName, parsedIonInfo.MoleculeID.AccessionNumbers); } } - catch (ArgumentException e) + catch (Exception e) when (IsParserException(e)) { ShowTransitionError(new PasteError { @@ -1849,7 +1850,7 @@ private PeptideDocNode GetMoleculePeptide(SrmDocument document, Row row, Peptide return null; return new PeptideDocNode(pep, document.Settings, null, null, parsedIonInfo.ExplicitRetentionTime, new[] { tranGroup }, false); } - catch (InvalidOperationException e) + catch (Exception e) when (IsParserException(e)) { ShowTransitionError(new PasteError { @@ -1908,11 +1909,7 @@ private TransitionGroupDocNode GetMoleculeTransitionGroup(SrmDocument document, return new TransitionGroupDocNode(group, document.Annotations, document.Settings, null, null, moleculeInfo.ExplicitTransitionGroupValues, null, new[] { tran }, false); } - catch (InvalidDataException x) - { - errmsg = x.Message; - } - catch (InvalidOperationException x) // Adduct handling code can throw these + catch (Exception x) when (IsParserException(x)) { errmsg = x.Message; }