Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 270be00

Browse files
MarcoRossignolikrwq
authored andcommitted
"Don't directly throw Exception" System.Private.Xml (#25975)
* Don't directly throw Exception * Don't directly throw Exception * Don't directly throw Exception * Don't directly throw Exception * Fix test
1 parent a011ef1 commit 270be00

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImplAsync.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3593,7 +3593,7 @@ private async Task<ValueTuple<int, int, int, bool>> ParseTextAsync_Surrogate(int
35933593
ThrowInvalidChar(_ps.chars, _ps.charsUsed, _ps.charPos + offset);
35943594
}
35953595
//should never hit here
3596-
throw new Exception();
3596+
throw new XmlException(SR.Xml_InternalError);
35973597
}
35983598

35993599
private async Task<ValueTuple<int, int, int, bool>> ParseTextAsync_ReadData(int outOrChars, char[] chars, int pos, int rcount, int rpos, int orChars, char c)

src/System.Private.Xml/src/System/Xml/Serialization/Xmlcustomformatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ internal static string FromDefaultValue(object value, string formatter)
7777
return FromXmlNmTokens((string)value);
7878
}
7979
}
80-
throw new Exception(SR.Format(SR.XmlUnsupportedDefaultType, type.FullName));
80+
throw new XmlException(SR.Format(SR.XmlUnsupportedDefaultType, type.FullName));
8181
}
8282

8383
internal static string FromDate(DateTime value)
@@ -243,7 +243,7 @@ internal static object ToDefaultValue(string value, string formatter)
243243
{
244244
return ToXmlNmTokens(value);
245245
}
246-
throw new Exception(SR.Format(SR.XmlUnsupportedDefaultValue, formatter));
246+
throw new XmlException(SR.Format(SR.XmlUnsupportedDefaultValue, formatter));
247247
// Debug.WriteLineIf(CompModSwitches.XmlSerialization.TraceVerbose, "XmlSerialization::Unhandled default value " + value + " formatter " + formatter);
248248
// return DBNull.Value;
249249
}

src/System.Private.Xml/src/System/Xml/Xsl/Xslt/XPathPatternBuilder.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,15 @@ public QilNode Function(string prefix, string name, IList<QilNode> args)
321321
}
322322

323323
public QilNode String(string value) { return _f.String(value); } // As argument of id() or key() function
324-
public QilNode Number(double value) { return UnexpectedToken("Literal number"); }
325-
public QilNode Variable(string prefix, string name) { return UnexpectedToken("Variable"); }
326-
327-
private QilNode UnexpectedToken(string tokenName)
324+
public QilNode Number(double value)
325+
{
326+
//Internal Error: Literal number is not allowed in XSLT pattern outside of predicate.
327+
throw new XmlException(SR.Xml_InternalError);
328+
}
329+
public QilNode Variable(string prefix, string name)
328330
{
329-
string prompt = string.Format(CultureInfo.InvariantCulture, "Internal Error: {0} is not allowed in XSLT pattern outside of predicate.", tokenName);
330-
Debug.Assert(false, prompt);
331-
throw new Exception(prompt);
331+
//Internal Error: Variable is not allowed in XSLT pattern outside of predicate.
332+
throw new XmlException(SR.Xml_InternalError);
332333
}
333334

334335
// -------------------------------------- Priority / Parent ---------------------------------------

src/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.RuntimeOnly.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,20 @@ public static void XmlReflectionImporterTest()
19361936
Assert.False(xmp.CheckSpecified);
19371937
}
19381938

1939+
[Fact]
1940+
public static void XmlSchemaExporter_ExportMembersMapping_NotSupportedDefaultValue()
1941+
{
1942+
XmlReflectionImporter importer = new XmlReflectionImporter("http://www.contoso.com/");
1943+
XmlReflectionMember[] members = new XmlReflectionMember[1];
1944+
XmlReflectionMember member = members[0] = new XmlReflectionMember();
1945+
member.MemberType = typeof(TypeWithQNameArrayAsXmlAttributeInvalidDefaultValue);
1946+
XmlMembersMapping mappings = importer.ImportMembersMapping("root", "", members, true);
1947+
XmlMemberMapping xmp = mappings[0];
1948+
XmlSchemas schema = new XmlSchemas();
1949+
XmlSchemaExporter exporter = new XmlSchemaExporter(schema);
1950+
AssertExtensions.Throws<XmlException,Exception>(() => exporter.ExportMembersMapping(mappings));
1951+
}
1952+
19391953
[Fact]
19401954
public static void XmlSerializerVersionAttributeTest()
19411955
{

src/System.Runtime.Serialization.Xml/tests/SerializationTypes.RuntimeOnly.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,13 @@ public class TypeWithArrayLikeXmlAttributeWithFields
19791979
public int IntField;
19801980
}
19811981

1982+
[XmlType(TypeName = "MyXmlType")]
1983+
public class TypeWithQNameArrayAsXmlAttributeInvalidDefaultValue
1984+
{
1985+
[DefaultValue("DefaultValue")]
1986+
public XmlQualifiedName XmlAttributeForms;
1987+
}
1988+
19821989
[XmlType(TypeName = "MyXmlType")]
19831990
public class TypeWithQNameArrayAsXmlAttribute
19841991
{

0 commit comments

Comments
 (0)