Reputation: 4217
I get the DoNotPassLiteralsAsLocalizedParameters FxCop violation for both exception throwing lines in the below method code:
public bool IsPageAccessible(string url, string documentId) {
if (url == null) {
throw new ArgumentNullException("url", @"url must not be null, use string.Empty if you don't care what the url is.");
}
if (documentId == null) {
throw new ArgumentNullException("documentId", "documentId must not be null, use string.Empty if you don't care what the documentId is.");
}
return true;
}
It means:
fxcop Globalization#CA1303 String literals that are embedded in source code are difficult to localize. Avoid passing string literals as arguments in circumstances where a localized string is generally expected. Most localized applications, for example, should localize string arguments that are passed to exception constructors. When creating an Exception instance, therefore, a string argument retrieved from a string table is more appropriate than a string literal.
Reasoning:
I do not wish to localize the exception message. Having only English is fine. Even though we are building an API, English is known by all developers. And the exception message should not be shown to the visitor on a production server anyway.
Questions:
Upvotes: 4
Views: 715
Reputation: 49970
I think your reasoning is good, I hate it when I have localized exception in Visual Studio and can't find help on it because the lingua franca for programming is English.
More generally, you shouldn't try to conform to every fxcop rules, this can quickly be a burden. It is better to concentrate on a subset of rules.
I don't think that you can exclude warning in a particular exception, but you can exclude detection using SuppressMessage
attribute :
[SuppressMessage("Microsoft.Globalization",
"CA1303:DoNotPassLiteralsAsLocalizedParameters",
Justification="Exception are not localized")]
public bool IsPageAccessible(string url, string documentId) {
if (url == null) {
throw new ArgumentNullException("url", @"url must not be null, use string.Empty if you don't care what the url is.");
}
if (documentId == null) {
throw new ArgumentNullException("documentId", "documentId must not be null, use string.Empty if you don't care what the documentId is.");
}
return true;
}
Another way, would be to write a custom fxcop rule to add this behavior.
Upvotes: 4