Skip to content

Commit

Permalink
Use corresponding exception message, if null passed (#90505)
Browse files Browse the repository at this point in the history
* use corresponding exception message if null passed

* fix

* review fixes

* fix tests

* do not use default message if null passed for SystemException, ApplicationException, IOException

* change COMException default message

---------

Co-authored-by: David Cantú <dacantu@microsoft.com>
  • Loading branch information
OwnageIsMagic and jozkee authored Jan 18, 2024
1 parent 08cff56 commit 56769fa
Show file tree
Hide file tree
Showing 99 changed files with 281 additions and 286 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public HostAbortedException() : base(SR.HostAbortedExceptionMessage) { }
/// The caller of this constructor is required to ensure that this string has been localized for the
/// current system culture.
/// </remarks>
public HostAbortedException(string? message) : base(message) { }
public HostAbortedException(string? message) : base(message ?? SR.HostAbortedExceptionMessage) { }

/// <summary>
/// Initializes a new instance of the <see cref="HostAbortedException"/> class
Expand All @@ -52,6 +52,6 @@ public HostAbortedException(string? message) : base(message) { }
/// The caller of this constructor is required to ensure that this string has been localized for the
/// current system culture.
/// </remarks>
public HostAbortedException(string? message, Exception? innerException) : base(message, innerException) { }
public HostAbortedException(string? message, Exception? innerException) : base(message ?? SR.HostAbortedExceptionMessage, innerException) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public CompositionFailedException()
/// </summary>
/// <param name="message">The exception message.</param>
public CompositionFailedException(string message)
: base(message)
: base(message ?? SR.CompositionFailedDefaultExceptionMessage)
{ }

/// <summary>
Expand All @@ -31,7 +31,7 @@ public CompositionFailedException(string message)
/// <param name="message">The exception message.</param>
/// <param name="innerException">The inner exception.</param>
public CompositionFailedException(string message, Exception innerException)
: base(message, innerException)
: base(message ?? SR.CompositionFailedDefaultExceptionMessage, innerException)
{ }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ public void Constructor1()
ConfigurationErrorsException cee = new ConfigurationErrorsException();
Assert.NotNull(cee.BareMessage);

// \p{Pi} any kind of opening quote https://www.compart.com/en/unicode/category/Pi
// \p{Pf} any kind of closing quote https://www.compart.com/en/unicode/category/Pf
// \p{Po} any kind of punctuation character that is not a dash, bracket, quote or connector https://www.compart.com/en/unicode/category/Po
Assert.Matches(@"[\p{Pi}\p{Po}]" + Regex.Escape(typeof(ConfigurationErrorsException).FullName) + @"[\p{Pf}\p{Po}]", cee.BareMessage);

Assert.NotNull(cee.Data);
Assert.Equal(0, cee.Data.Count);
Assert.Null(cee.Filename);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public DBConcurrencyException(string? message) : this(message, null)
{
}

public DBConcurrencyException(string? message, Exception? inner) : base(message, inner)
public DBConcurrencyException(string? message, Exception? inner) : base(message ?? SR.ADP_DBConcurrencyExceptionMessage, inner)
{
HResult = HResults.DBConcurrency;
}

public DBConcurrencyException(string? message, Exception? inner, DataRow[]? dataRows) : base(message, inner)
public DBConcurrencyException(string? message, Exception? inner, DataRow[]? dataRows) : base(message ?? SR.ADP_DBConcurrencyExceptionMessage, inner)
{
HResult = HResults.DBConcurrency;
_dataRows = dataRows;
Expand Down
44 changes: 22 additions & 22 deletions src/libraries/System.Data.Common/src/System/Data/DataException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public DataException() : base(SR.DataSet_DefaultDataException)
HResult = HResults.Data;
}

public DataException(string? s) : base(s)
public DataException(string? s) : base(s ?? SR.DataSet_DefaultDataException)
{
HResult = HResults.Data;
}

public DataException(string? s, Exception? innerException) : base(s, innerException) { }
public DataException(string? s, Exception? innerException) : base(s ?? SR.DataSet_DefaultDataException, innerException) { }
};

[Serializable]
Expand All @@ -46,12 +46,12 @@ public ConstraintException() : base(SR.DataSet_DefaultConstraintException)
HResult = HResults.DataConstraint;
}

public ConstraintException(string? s) : base(s)
public ConstraintException(string? s) : base(s ?? SR.DataSet_DefaultConstraintException)
{
HResult = HResults.DataConstraint;
}

public ConstraintException(string? message, Exception? innerException) : base(message, innerException)
public ConstraintException(string? message, Exception? innerException) : base(message ?? SR.DataSet_DefaultConstraintException, innerException)
{
HResult = HResults.DataConstraint;
}
Expand All @@ -78,12 +78,12 @@ public DeletedRowInaccessibleException() : base(SR.DataSet_DefaultDeletedRowInac
/// <summary>
/// Initializes a new instance of the <see cref='System.Data.DeletedRowInaccessibleException'/> class with the specified string.
/// </summary>
public DeletedRowInaccessibleException(string? s) : base(s)
public DeletedRowInaccessibleException(string? s) : base(s ?? SR.DataSet_DefaultDeletedRowInaccessibleException)
{
HResult = HResults.DataDeletedRowInaccessible;
}

public DeletedRowInaccessibleException(string? message, Exception? innerException) : base(message, innerException)
public DeletedRowInaccessibleException(string? message, Exception? innerException) : base(message ?? SR.DataSet_DefaultDeletedRowInaccessibleException, innerException)
{
HResult = HResults.DataDeletedRowInaccessible;
}
Expand All @@ -104,12 +104,12 @@ public DuplicateNameException() : base(SR.DataSet_DefaultDuplicateNameException)
HResult = HResults.DataDuplicateName;
}

public DuplicateNameException(string? s) : base(s)
public DuplicateNameException(string? s) : base(s ?? SR.DataSet_DefaultDuplicateNameException)
{
HResult = HResults.DataDuplicateName;
}

public DuplicateNameException(string? message, Exception? innerException) : base(message, innerException)
public DuplicateNameException(string? message, Exception? innerException) : base(message ?? SR.DataSet_DefaultDuplicateNameException, innerException)
{
HResult = HResults.DataDuplicateName;
}
Expand All @@ -130,12 +130,12 @@ public InRowChangingEventException() : base(SR.DataSet_DefaultInRowChangingEvent
HResult = HResults.DataInRowChangingEvent;
}

public InRowChangingEventException(string? s) : base(s)
public InRowChangingEventException(string? s) : base(s ?? SR.DataSet_DefaultInRowChangingEventException)
{
HResult = HResults.DataInRowChangingEvent;
}

public InRowChangingEventException(string? message, Exception? innerException) : base(message, innerException)
public InRowChangingEventException(string? message, Exception? innerException) : base(message ?? SR.DataSet_DefaultInRowChangingEventException, innerException)
{
HResult = HResults.DataInRowChangingEvent;
}
Expand All @@ -156,12 +156,12 @@ public InvalidConstraintException() : base(SR.DataSet_DefaultInvalidConstraintEx
HResult = HResults.DataInvalidConstraint;
}

public InvalidConstraintException(string? s) : base(s)
public InvalidConstraintException(string? s) : base(s ?? SR.DataSet_DefaultInvalidConstraintException)
{
HResult = HResults.DataInvalidConstraint;
}

public InvalidConstraintException(string? message, Exception? innerException) : base(message, innerException)
public InvalidConstraintException(string? message, Exception? innerException) : base(message ?? SR.DataSet_DefaultInvalidConstraintException, innerException)
{
HResult = HResults.DataInvalidConstraint;
}
Expand All @@ -182,12 +182,12 @@ public MissingPrimaryKeyException() : base(SR.DataSet_DefaultMissingPrimaryKeyEx
HResult = HResults.DataMissingPrimaryKey;
}

public MissingPrimaryKeyException(string? s) : base(s)
public MissingPrimaryKeyException(string? s) : base(s ?? SR.DataSet_DefaultMissingPrimaryKeyException)
{
HResult = HResults.DataMissingPrimaryKey;
}

public MissingPrimaryKeyException(string? message, Exception? innerException) : base(message, innerException)
public MissingPrimaryKeyException(string? message, Exception? innerException) : base(message ?? SR.DataSet_DefaultMissingPrimaryKeyException, innerException)
{
HResult = HResults.DataMissingPrimaryKey;
}
Expand All @@ -208,12 +208,12 @@ public NoNullAllowedException() : base(SR.DataSet_DefaultNoNullAllowedException)
HResult = HResults.DataNoNullAllowed;
}

public NoNullAllowedException(string? s) : base(s)
public NoNullAllowedException(string? s) : base(s ?? SR.DataSet_DefaultNoNullAllowedException)
{
HResult = HResults.DataNoNullAllowed;
}

public NoNullAllowedException(string? message, Exception? innerException) : base(message, innerException)
public NoNullAllowedException(string? message, Exception? innerException) : base(message ?? SR.DataSet_DefaultNoNullAllowedException, innerException)
{
HResult = HResults.DataNoNullAllowed;
}
Expand All @@ -234,12 +234,12 @@ public ReadOnlyException() : base(SR.DataSet_DefaultReadOnlyException)
HResult = HResults.DataReadOnly;
}

public ReadOnlyException(string? s) : base(s)
public ReadOnlyException(string? s) : base(s ?? SR.DataSet_DefaultReadOnlyException)
{
HResult = HResults.DataReadOnly;
}

public ReadOnlyException(string? message, Exception? innerException) : base(message, innerException)
public ReadOnlyException(string? message, Exception? innerException) : base(message ?? SR.DataSet_DefaultReadOnlyException, innerException)
{
HResult = HResults.DataReadOnly;
}
Expand All @@ -260,12 +260,12 @@ public RowNotInTableException() : base(SR.DataSet_DefaultRowNotInTableException)
HResult = HResults.DataRowNotInTable;
}

public RowNotInTableException(string? s) : base(s)
public RowNotInTableException(string? s) : base(s ?? SR.DataSet_DefaultRowNotInTableException)
{
HResult = HResults.DataRowNotInTable;
}

public RowNotInTableException(string? message, Exception? innerException) : base(message, innerException)
public RowNotInTableException(string? message, Exception? innerException) : base(message ?? SR.DataSet_DefaultRowNotInTableException, innerException)
{
HResult = HResults.DataRowNotInTable;
}
Expand All @@ -286,12 +286,12 @@ public VersionNotFoundException() : base(SR.DataSet_DefaultVersionNotFoundExcept
HResult = HResults.DataVersionNotFound;
}

public VersionNotFoundException(string? s) : base(s)
public VersionNotFoundException(string? s) : base(s ?? (SR.DataSet_DefaultVersionNotFoundException))
{
HResult = HResults.DataVersionNotFound;
}

public VersionNotFoundException(string? message, Exception? innerException) : base(message, innerException)
public VersionNotFoundException(string? message, Exception? innerException) : base(message ?? (SR.DataSet_DefaultVersionNotFoundException), innerException)
{
HResult = HResults.DataVersionNotFound;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public void Constructor1()
DBConcurrencyException dbce = new DBConcurrencyException();
Assert.Null(dbce.InnerException);
Assert.NotNull(dbce.Message);
Assert.NotNull(dbce.Message);
Assert.Null(dbce.Row);
Assert.Equal(0, dbce.RowCount);
}
Expand All @@ -55,9 +54,7 @@ public void Constructor2()
dbce = new DBConcurrencyException(null);
Assert.Null(dbce.InnerException);
Assert.NotNull(dbce.Message);
Assert.Contains(typeof(DBConcurrencyException).FullName, dbce.Message);
Assert.Null(dbce.Row);

Assert.Equal(0, dbce.RowCount);

dbce = new DBConcurrencyException(string.Empty);
Expand All @@ -82,7 +79,7 @@ public void Constructor3()

dbce = new DBConcurrencyException(null, inner);
Assert.Same(inner, dbce.InnerException);
Assert.Contains(typeof(DBConcurrencyException).FullName, dbce.Message);
Assert.NotNull(dbce.Message);
Assert.Null(dbce.Row);
Assert.Equal(0, dbce.RowCount);

Expand All @@ -100,7 +97,7 @@ public void Constructor3()

dbce = new DBConcurrencyException(null, null);
Assert.Null(dbce.InnerException);
Assert.Contains(typeof(DBConcurrencyException).FullName, dbce.Message);
Assert.NotNull(dbce.Message);
Assert.Null(dbce.Row);
Assert.Equal(0, dbce.RowCount);
}
Expand All @@ -126,7 +123,7 @@ public void Constructor4()
rows = new DataRow[] { rowB, rowA, null };
dbce = new DBConcurrencyException(null, inner, rows);
Assert.Same(inner, dbce.InnerException);
Assert.Contains(typeof(DBConcurrencyException).FullName, dbce.Message);
Assert.NotNull(dbce.Message);
Assert.Same(rowB, dbce.Row);
Assert.Equal(3, dbce.RowCount);

Expand Down Expand Up @@ -154,7 +151,7 @@ public void Constructor4()
rows = null;
dbce = new DBConcurrencyException(null, null, rows);
Assert.Null(dbce.InnerException);
Assert.Contains(typeof(DBConcurrencyException).FullName, dbce.Message);
Assert.NotNull(dbce.Message);
Assert.Null(dbce.Row);
Assert.Equal(0, dbce.RowCount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public class DirectoryOperationException : DirectoryException, ISerializable
#endif
protected DirectoryOperationException(SerializationInfo info, StreamingContext context) : base(info, context) { }

public DirectoryOperationException() : base() { }
public DirectoryOperationException() : base(SR.DefaultOperationsError) { }

public DirectoryOperationException(string message) : base(message) { }
public DirectoryOperationException(string message) : base(message ?? SR.DefaultOperationsError) { }

public DirectoryOperationException(string message, Exception inner) : base(message, inner) { }
public DirectoryOperationException(string message, Exception inner) : base(message ?? SR.DefaultOperationsError, inner) { }

public DirectoryOperationException(DirectoryResponse response) :
base(CreateMessage(response, message: null))
Expand All @@ -68,7 +68,7 @@ public DirectoryOperationException(DirectoryResponse response, string message, E

public DirectoryResponse Response { get; internal set; }

private static string CreateMessage(DirectoryResponse response, string message)
private static string CreateMessage(DirectoryResponse response, string? message)
{
string result = message ?? SR.DefaultOperationsError;
if (!string.IsNullOrEmpty(response?.ErrorMessage))
Expand All @@ -93,11 +93,11 @@ public BerConversionException() : base(SR.BerConversionError)
{
}

public BerConversionException(string message) : base(message)
public BerConversionException(string message) : base(message ?? SR.BerConversionError)
{
}

public BerConversionException(string message, Exception inner) : base(message, inner)
public BerConversionException(string message, Exception inner) : base(message ?? SR.BerConversionError, inner)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,19 @@ public override string Message
[System.Runtime.CompilerServices.TypeForwardedFrom("System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public class ActiveDirectoryOperationException : Exception, ISerializable
{
public ActiveDirectoryOperationException(string? message, Exception? inner, int errorCode) : base(message, inner)
public ActiveDirectoryOperationException(string? message, Exception? inner, int errorCode) : base(message ?? SR.DSUnknownFailure, inner)
{
ErrorCode = errorCode;
}

public ActiveDirectoryOperationException(string? message, int errorCode) : base(message)
public ActiveDirectoryOperationException(string? message, int errorCode) : base(message ?? SR.DSUnknownFailure)
{
ErrorCode = errorCode;
}

public ActiveDirectoryOperationException(string? message, Exception? inner) : base(message, inner) { }
public ActiveDirectoryOperationException(string? message, Exception? inner) : base(message ?? SR.DSUnknownFailure, inner) { }

public ActiveDirectoryOperationException(string? message) : base(message) { }
public ActiveDirectoryOperationException(string? message) : base(message ?? SR.DSUnknownFailure) { }

public ActiveDirectoryOperationException() : base(SR.DSUnknownFailure) { }

Expand Down Expand Up @@ -205,14 +205,14 @@ public class SyncFromAllServersOperationException : ActiveDirectoryOperationExce
{
private readonly SyncFromAllServersErrorInformation[]? _errors;

public SyncFromAllServersOperationException(string? message, Exception? inner, SyncFromAllServersErrorInformation[]? errors) : base(message, inner)
public SyncFromAllServersOperationException(string? message, Exception? inner, SyncFromAllServersErrorInformation[]? errors) : base(message ?? SR.DSSyncAllFailure, inner)
{
_errors = errors;
}

public SyncFromAllServersOperationException(string? message, Exception? inner) : base(message, inner) { }
public SyncFromAllServersOperationException(string? message, Exception? inner) : base(message ?? SR.DSSyncAllFailure, inner) { }

public SyncFromAllServersOperationException(string? message) : base(message) { }
public SyncFromAllServersOperationException(string? message) : base(message ?? SR.DSSyncAllFailure) { }

public SyncFromAllServersOperationException() : base(SR.DSSyncAllFailure) { }

Expand Down Expand Up @@ -253,14 +253,14 @@ public override void GetObjectData(SerializationInfo serializationInfo, Streamin
[System.Runtime.CompilerServices.TypeForwardedFrom("System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public class ForestTrustCollisionException : ActiveDirectoryOperationException, ISerializable
{
public ForestTrustCollisionException(string? message, Exception? inner, ForestTrustRelationshipCollisionCollection? collisions) : base(message, inner)
public ForestTrustCollisionException(string? message, Exception? inner, ForestTrustRelationshipCollisionCollection? collisions) : base(message ?? SR.ForestTrustCollision, inner)
{
Collisions = collisions;
}

public ForestTrustCollisionException(string? message, Exception? inner) : base(message, inner) { }
public ForestTrustCollisionException(string? message, Exception? inner) : base(message ?? SR.ForestTrustCollision, inner) { }

public ForestTrustCollisionException(string? message) : base(message) { }
public ForestTrustCollisionException(string? message) : base(message ?? SR.ForestTrustCollision) { }

public ForestTrustCollisionException() : base(SR.ForestTrustCollision) { }

Expand Down
Loading

0 comments on commit 56769fa

Please sign in to comment.