Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Strings resx file for the exception messages within System.Text.Encodings.Web #30510

Closed
ahsonkhan opened this issue Aug 6, 2019 · 7 comments · Fixed by dotnet/corefx#40239
Labels
area-System.Text.Encodings.Web enhancement Product code improvement that does NOT require public API changes/additions good first issue Issue should be easy to implement, good for first-time contributors help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@ahsonkhan
Copy link
Contributor

See dotnet/corefx#39900 (comment)

We currently inline the exception message strings when throwing in System.Text.Encodings.Web.
We should be adding these to a Strings.resx file and use SR.X to access them.

Even though we're not currently localizing exception strings on .NET Core, being in the Strings.resx file does mean that a) there aren't two different copies that get out of sync if the message gets tweaked and b) we could localize if desired.

Look at other projects for examples. E.g:
https://github.com/dotnet/corefx/blob/c6f5ceedea28edf01ae7d3e13c02935669dad434/src/System.Text.Json/src/Resources/Strings.resx#L138-L140
https://github.com/dotnet/corefx/blob/47c35fe385c18d5f0a4ceacea381f790db472ba2/src/System.Text.Json/src/System/Text/Json/ThrowHelper.cs#L443-L446
https://github.com/dotnet/corefx/blob/82408cd90f4d4573d502e8df2ca437b35e6a37f7/src/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.Unescaping.cs#L209

The use of the "ThrowHelper" pattern isn't necessary here (applied on case-by-case basis where performance concerns were higher than code readability).

Another example:
https://github.com/dotnet/corefx/blob/82408cd90f4d4573d502e8df2ca437b35e6a37f7/src/System.Private.Xml/src/Resources/Strings.resx#L134-L136
https://github.com/dotnet/corefx/blob/e5d38a54cf21f6427ae4fe99c3841c8067139e75/src/System.Private.Xml/src/System/Xml/Xslt/XslCompiledTransform.cs#L475-L485

cc @GrabYourPitchforks

@stephentoub
Copy link
Member

a) there aren't two different copies that get out of sync if the message gets tweaked and b) we could localize if desired.

And maybe most importantly, (c) for size we could choose to have the linker (e.g. when linking a whole app) strip out all of the resx contents; that's harder to do when the string is directly embedded in the code because it's harder to determine with 100% fidelity that the string isn't being used for something functional.

@danmoseley
Copy link
Member

Anyone notice any more resx debt? My assumption had been that there isn't any.

@ahsonkhan
Copy link
Contributor Author

ahsonkhan commented Aug 7, 2019

A naiive regex search for Exception\(".+?"\) within cs files shows quite a few results but most are false positives.
1144 results in 472 files (almost all are in tests, or under #if DEBUG, or single word arg names, and can be ignored).

Some are in Common:
https://github.com/dotnet/corefx/blob/cc5653fcceefce5eac38dc7847b12b371027f78f/src/Common/src/System/IO/RowConfigReader.cs#L53
https://github.com/dotnet/corefx/blob/cc5653fcceefce5eac38dc7847b12b371027f78f/src/Common/src/CoreLib/System/PasteArguments.Windows.cs#L38
https://github.com/dotnet/corefx/blob/cc5653fcceefce5eac38dc7847b12b371027f78f/src/Common/src/CoreLib/System/Diagnostics/Tracing/DiagnosticCounter.cs#L86

https://github.com/dotnet/corefx/blob/cc5653fcceefce5eac38dc7847b12b371027f78f/src/System.Composition.Hosting/src/System/Composition/Hosting/Core/ExportDescriptorRegistryUpdate.cs#L35
https://github.com/dotnet/corefx/blob/cc5653fcceefce5eac38dc7847b12b371027f78f/src/System.Data.SqlClient/src/Interop/SNINativeMethodWrapper.Unix.cs#L13

System.Drawing.Common has some.
https://github.com/dotnet/corefx/blob/cc5653fcceefce5eac38dc7847b12b371027f78f/src/System.Drawing.Common/src/System/Drawing/Font.Unix.cs#L218
https://github.com/dotnet/corefx/blob/cc5653fcceefce5eac38dc7847b12b371027f78f/src/System.Drawing.Common/src/System/Drawing/Printing/PrinterSettings.Unix.cs#L105

And then a few sprinkled in various assemblies. For example:
https://github.com/dotnet/corefx/blob/cc5653fcceefce5eac38dc7847b12b371027f78f/src/System.IO.Packaging/src/System/IO/Packaging/ZipPackage.cs#L356
https://github.com/dotnet/corefx/blob/cc5653fcceefce5eac38dc7847b12b371027f78f/src/System.Private.DataContractSerialization/src/System/Runtime/Serialization/DataContractSerializer.cs#L46
https://github.com/dotnet/corefx/blob/cc5653fcceefce5eac38dc7847b12b371027f78f/src/System.Private.Xml/src/System/Xml/Xsl/Xslt/XsltLoader.cs#L1318

Note: This is not an exhaustive list but it looks like ~25 or so real instances.

Question: Should single word messages like Argument...Exception("foo") be changed too?

@danmoseley
Copy link
Member

danmoseley commented Aug 7, 2019

Ok, so a little debt for if we decide to support localization (let's not discuss that in this issue as it's already discussed elsewhere and there is no plan at this point..)

@danmoseley
Copy link
Member

Question: Should single word messages like Argument...Exception("foo") be changed too

In this case the exception provides the message. Ideally would use nameof though.

@Marusyk
Copy link
Member

Marusyk commented Aug 12, 2019

Can I go ahead with other projects, or it should be separate issue?

@ahsonkhan
Copy link
Contributor Author

Can I go ahead with other projects, or it should be separate issue?

Feel free to fix other instances (you don't have to wait to file a separate issue). However, I would fix that in a separate PR.

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 5.0 milestone Feb 1, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Encodings.Web enhancement Product code improvement that does NOT require public API changes/additions good first issue Issue should be easy to implement, good for first-time contributors help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants