Skip to content

Commit 010dc51

Browse files
authored
Use exception convenience methods (#5982)
1 parent 5230336 commit 010dc51

28 files changed

+96
-200
lines changed

src/Npgsql/Internal/NpgsqlDatabaseInfo.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,7 @@ protected static Version ParseServerVersion(string value)
313313
/// </summary>
314314
public static void RegisterFactory(INpgsqlDatabaseInfoFactory factory)
315315
{
316-
if (factory == null)
317-
throw new ArgumentNullException(nameof(factory));
316+
ArgumentNullException.ThrowIfNull(factory);
318317

319318
var factories = new INpgsqlDatabaseInfoFactory[Factories.Length + 1];
320319
factories[0] = factory;

src/Npgsql/Internal/NpgsqlReadBuffer.Stream.cs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ public override long Position
7373
}
7474
set
7575
{
76-
if (value < 0)
77-
throw new ArgumentOutOfRangeException(nameof(value), "Non - negative number required.");
76+
ArgumentOutOfRangeException.ThrowIfNegative(value);
7877
Seek(value, SeekOrigin.Begin);
7978
}
8079
}
@@ -85,8 +84,7 @@ public override long Seek(long offset, SeekOrigin origin)
8584

8685
if (!_canSeek)
8786
throw new NotSupportedException();
88-
if (offset > int.MaxValue)
89-
throw new ArgumentOutOfRangeException(nameof(offset), "Stream length must be non-negative and less than 2^31 - 1 - origin.");
87+
ArgumentOutOfRangeException.ThrowIfGreaterThan(offset, int.MaxValue);
9088

9189
const string seekBeforeBegin = "An attempt was made to move the position before the beginning of the stream.";
9290

@@ -191,10 +189,7 @@ public override void Write(byte[] buffer, int offset, int count)
191189
=> throw new NotSupportedException();
192190

193191
void CheckDisposed()
194-
{
195-
if (IsDisposed)
196-
ThrowHelper.ThrowObjectDisposedException(nameof(ColumnStream));
197-
}
192+
=> ObjectDisposedException.ThrowIf(IsDisposed, this);
198193

199194
protected override void Dispose(bool disposing)
200195
{
@@ -224,13 +219,10 @@ async ValueTask DisposeCore(bool async)
224219

225220
static void ValidateArguments(byte[] buffer, int offset, int count)
226221
{
227-
if (buffer == null)
228-
throw new ArgumentNullException(nameof(buffer));
229-
if (offset < 0)
230-
throw new ArgumentOutOfRangeException(nameof(offset));
231-
if (count < 0)
232-
throw new ArgumentOutOfRangeException(nameof(count));
222+
ArgumentNullException.ThrowIfNull(buffer);
223+
ArgumentOutOfRangeException.ThrowIfNegative(offset);
224+
ArgumentOutOfRangeException.ThrowIfNegative(count);
233225
if (buffer.Length - offset < count)
234-
throw new ArgumentException("Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.");
226+
ThrowHelper.ThrowArgumentException("Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.");
235227
}
236228
}

src/Npgsql/Internal/NpgsqlReadBuffer.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,7 @@ internal NpgsqlReadBuffer(
113113
Encoding relaxedTextEncoding,
114114
bool usePool = false)
115115
{
116-
if (size < MinimumSize)
117-
{
118-
throw new ArgumentOutOfRangeException(nameof(size), size, "Buffer size must be at least " + MinimumSize);
119-
}
116+
ArgumentOutOfRangeException.ThrowIfLessThan(size, MinimumSize);
120117

121118
Connector = connector!; // TODO: Clean this up
122119
Underlying = stream;

src/Npgsql/Internal/NpgsqlWriteBuffer.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ internal NpgsqlWriteBuffer(
101101
int size,
102102
Encoding textEncoding)
103103
{
104-
if (size < MinimumSize)
105-
throw new ArgumentOutOfRangeException(nameof(size), size, "Buffer size must be at least " + MinimumSize);
104+
ArgumentOutOfRangeException.ThrowIfLessThan(size, MinimumSize);
106105

107106
Connector = connector!; // TODO: Clean this up; only null when used from PregeneratedMessages, where we don't care.
108107
Underlying = stream;
@@ -579,8 +578,7 @@ void AdvanceMessageBytesFlushed(int count)
579578

580579
void Throw()
581580
{
582-
if (count < 0)
583-
throw new ArgumentOutOfRangeException(nameof(count), "Can't advance by a negative count");
581+
ArgumentOutOfRangeException.ThrowIfNegative(count);
584582

585583
if (_messageLength is null)
586584
throw Connector.Break(new InvalidOperationException("No message was started"));

src/Npgsql/Internal/PgWriter.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -495,14 +495,11 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
495495

496496
Task Write(bool async, byte[] buffer, int offset, int count, CancellationToken cancellationToken)
497497
{
498-
if (buffer is null)
499-
throw new ArgumentNullException(nameof(buffer));
500-
if (offset < 0)
501-
throw new ArgumentNullException(nameof(offset));
502-
if (count < 0)
503-
throw new ArgumentNullException(nameof(count));
498+
ArgumentNullException.ThrowIfNull(buffer);
499+
ArgumentOutOfRangeException.ThrowIfNegative(offset);
500+
ArgumentOutOfRangeException.ThrowIfNegative(count);
504501
if (buffer.Length - offset < count)
505-
throw new ArgumentException("Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.");
502+
ThrowHelper.ThrowArgumentException("Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.");
506503

507504
if (async)
508505
{

src/Npgsql/NameTranslation/NpgsqlSnakeCaseNameTranslator.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ public NpgsqlSnakeCaseNameTranslator(bool legacyMode, CultureInfo? culture = nul
5555
/// </summary>
5656
public string TranslateMemberName(string clrName)
5757
{
58-
if (clrName == null)
59-
throw new ArgumentNullException(nameof(clrName));
58+
ArgumentNullException.ThrowIfNull(clrName);
6059

6160
return LegacyMode
6261
? string.Concat(LegacyModeMap(clrName)).ToLower(_culture)

src/Npgsql/NpgsqlCommand.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,7 @@ public override int CommandTimeout
223223
get => _timeout ?? (InternalConnection?.CommandTimeout ?? DefaultTimeout);
224224
set
225225
{
226-
if (value < 0) {
227-
throw new ArgumentOutOfRangeException(nameof(value), value, "CommandTimeout can't be less than zero.");
228-
}
226+
ArgumentOutOfRangeException.ThrowIfNegative(value);
229227

230228
_timeout = value;
231229
}
@@ -1955,8 +1953,7 @@ public virtual NpgsqlCommand Clone()
19551953

19561954
NpgsqlConnection? CheckAndGetConnection()
19571955
{
1958-
if (State is CommandState.Disposed)
1959-
ThrowHelper.ThrowObjectDisposedException(GetType().FullName);
1956+
ObjectDisposedException.ThrowIf(State is CommandState.Disposed, this);
19601957

19611958
var conn = InternalConnection;
19621959
if (conn is null)

src/Npgsql/NpgsqlConnection.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,8 +1145,7 @@ public Task<NpgsqlBinaryImporter> BeginBinaryImportAsync(string copyFromCommand,
11451145

11461146
async Task<NpgsqlBinaryImporter> BeginBinaryImport(bool async, string copyFromCommand, CancellationToken cancellationToken = default)
11471147
{
1148-
if (copyFromCommand == null)
1149-
throw new ArgumentNullException(nameof(copyFromCommand));
1148+
ArgumentNullException.ThrowIfNull(copyFromCommand);
11501149
if (!IsValidCopyCommand(copyFromCommand))
11511150
throw new ArgumentException("Must contain a COPY FROM STDIN command!", nameof(copyFromCommand));
11521151

@@ -1196,8 +1195,7 @@ public Task<NpgsqlBinaryExporter> BeginBinaryExportAsync(string copyToCommand, C
11961195

11971196
async Task<NpgsqlBinaryExporter> BeginBinaryExport(bool async, string copyToCommand, CancellationToken cancellationToken = default)
11981197
{
1199-
if (copyToCommand == null)
1200-
throw new ArgumentNullException(nameof(copyToCommand));
1198+
ArgumentNullException.ThrowIfNull(copyToCommand);
12011199
if (!IsValidCopyCommand(copyToCommand))
12021200
throw new ArgumentException("Must contain a COPY TO STDOUT command!", nameof(copyToCommand));
12031201

@@ -1253,8 +1251,7 @@ public Task<TextWriter> BeginTextImportAsync(string copyFromCommand, Cancellatio
12531251

12541252
async Task<TextWriter> BeginTextImport(bool async, string copyFromCommand, CancellationToken cancellationToken = default)
12551253
{
1256-
if (copyFromCommand == null)
1257-
throw new ArgumentNullException(nameof(copyFromCommand));
1254+
ArgumentNullException.ThrowIfNull(copyFromCommand);
12581255
if (!IsValidCopyCommand(copyFromCommand))
12591256
throw new ArgumentException("Must contain a COPY FROM STDIN command!", nameof(copyFromCommand));
12601257

@@ -1311,8 +1308,7 @@ public Task<TextReader> BeginTextExportAsync(string copyToCommand, CancellationT
13111308

13121309
async Task<TextReader> BeginTextExport(bool async, string copyToCommand, CancellationToken cancellationToken = default)
13131310
{
1314-
if (copyToCommand == null)
1315-
throw new ArgumentNullException(nameof(copyToCommand));
1311+
ArgumentNullException.ThrowIfNull(copyToCommand);
13161312
if (!IsValidCopyCommand(copyToCommand))
13171313
throw new ArgumentException("Must contain a COPY TO STDOUT command!", nameof(copyToCommand));
13181314

@@ -1369,8 +1365,7 @@ public Task<NpgsqlRawCopyStream> BeginRawBinaryCopyAsync(string copyCommand, Can
13691365

13701366
async Task<NpgsqlRawCopyStream> BeginRawBinaryCopy(bool async, string copyCommand, CancellationToken cancellationToken = default)
13711367
{
1372-
if (copyCommand == null)
1373-
throw new ArgumentNullException(nameof(copyCommand));
1368+
ArgumentNullException.ThrowIfNull(copyCommand);
13741369
if (!IsValidCopyCommand(copyCommand))
13751370
throw new ArgumentException("Must contain a COPY TO STDOUT OR COPY FROM STDIN command!", nameof(copyCommand));
13761371

@@ -1534,10 +1529,7 @@ void CheckClosed()
15341529
}
15351530

15361531
void CheckDisposed()
1537-
{
1538-
if (_disposed)
1539-
ThrowHelper.ThrowObjectDisposedException(nameof(NpgsqlConnection));
1540-
}
1532+
=> ObjectDisposedException.ThrowIf(_disposed, this);
15411533

15421534
internal void CheckReady()
15431535
{
@@ -1825,8 +1817,7 @@ public async ValueTask<NpgsqlConnection> CloneWithAsync(string connectionString,
18251817
/// <param name="dbName">The name of the database to use in place of the current database.</param>
18261818
public override void ChangeDatabase(string dbName)
18271819
{
1828-
if (dbName == null)
1829-
throw new ArgumentNullException(nameof(dbName));
1820+
ArgumentNullException.ThrowIfNull(dbName);
18301821
if (string.IsNullOrEmpty(dbName))
18311822
throw new ArgumentOutOfRangeException(nameof(dbName), dbName, $"Invalid database name: {dbName}");
18321823

src/Npgsql/NpgsqlConnectionStringBuilder.cs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,7 @@ public int Port
244244
get => _port;
245245
set
246246
{
247-
if (value <= 0)
248-
throw new ArgumentOutOfRangeException(nameof(value), value, "Invalid port: " + value);
247+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value);
249248

250249
_port = value;
251250
SetValue(nameof(Port), value);
@@ -720,8 +719,7 @@ public int MinPoolSize
720719
get => _minPoolSize;
721720
set
722721
{
723-
if (value < 0)
724-
throw new ArgumentOutOfRangeException(nameof(value), value, "MinPoolSize can't be negative");
722+
ArgumentOutOfRangeException.ThrowIfNegative(value);
725723

726724
_minPoolSize = value;
727725
SetValue(nameof(MinPoolSize), value);
@@ -742,8 +740,7 @@ public int MaxPoolSize
742740
get => _maxPoolSize;
743741
set
744742
{
745-
if (value < 0)
746-
throw new ArgumentOutOfRangeException(nameof(value), value, "MaxPoolSize can't be negative");
743+
ArgumentOutOfRangeException.ThrowIfNegative(value);
747744

748745
_maxPoolSize = value;
749746
SetValue(nameof(MaxPoolSize), value);
@@ -836,8 +833,8 @@ public int Timeout
836833
get => _timeout;
837834
set
838835
{
839-
if (value < 0 || value > NpgsqlConnection.TimeoutLimit)
840-
throw new ArgumentOutOfRangeException(nameof(value), value, "Timeout must be between 0 and " + NpgsqlConnection.TimeoutLimit);
836+
ArgumentOutOfRangeException.ThrowIfNegative(value);
837+
ArgumentOutOfRangeException.ThrowIfGreaterThan(value, NpgsqlConnection.TimeoutLimit);
841838

842839
_timeout = value;
843840
SetValue(nameof(Timeout), value);
@@ -861,8 +858,7 @@ public int CommandTimeout
861858
get => _commandTimeout;
862859
set
863860
{
864-
if (value < 0)
865-
throw new ArgumentOutOfRangeException(nameof(value), value, "CommandTimeout can't be negative");
861+
ArgumentOutOfRangeException.ThrowIfNegative(value);
866862

867863
_commandTimeout = value;
868864
SetValue(nameof(CommandTimeout), value);
@@ -885,8 +881,7 @@ public int CancellationTimeout
885881
get => _cancellationTimeout;
886882
set
887883
{
888-
if (value < -1)
889-
throw new ArgumentOutOfRangeException(nameof(value), value, $"{nameof(CancellationTimeout)} can't less than -1");
884+
ArgumentOutOfRangeException.ThrowIfLessThan(value, -1);
890885

891886
_cancellationTimeout = value;
892887
SetValue(nameof(CancellationTimeout), value);
@@ -975,8 +970,7 @@ public int HostRecheckSeconds
975970
get => _hostRecheckSeconds;
976971
set
977972
{
978-
if (value < 0)
979-
throw new ArgumentException($"{HostRecheckSeconds} cannot be negative", nameof(HostRecheckSeconds));
973+
ArgumentOutOfRangeException.ThrowIfNegative(value);
980974
_hostRecheckSeconds = value;
981975
SetValue(nameof(HostRecheckSeconds), value);
982976
}
@@ -1000,8 +994,7 @@ public int KeepAlive
1000994
get => _keepAlive;
1001995
set
1002996
{
1003-
if (value < 0)
1004-
throw new ArgumentOutOfRangeException(nameof(value), value, "KeepAlive can't be negative");
997+
ArgumentOutOfRangeException.ThrowIfNegative(value);
1005998

1006999
_keepAlive = value;
10071000
SetValue(nameof(KeepAlive), value);
@@ -1041,8 +1034,7 @@ public int TcpKeepAliveTime
10411034
get => _tcpKeepAliveTime;
10421035
set
10431036
{
1044-
if (value < 0)
1045-
throw new ArgumentOutOfRangeException(nameof(value), value, "TcpKeepAliveTime can't be negative");
1037+
ArgumentOutOfRangeException.ThrowIfNegative(value);
10461038

10471039
_tcpKeepAliveTime = value;
10481040
SetValue(nameof(TcpKeepAliveTime), value);
@@ -1063,8 +1055,7 @@ public int TcpKeepAliveInterval
10631055
get => _tcpKeepAliveInterval;
10641056
set
10651057
{
1066-
if (value < 0)
1067-
throw new ArgumentOutOfRangeException(nameof(value), value, "TcpKeepAliveInterval can't be negative");
1058+
ArgumentOutOfRangeException.ThrowIfNegative(value);
10681059

10691060
_tcpKeepAliveInterval = value;
10701061
SetValue(nameof(TcpKeepAliveInterval), value);
@@ -1160,8 +1151,7 @@ public int MaxAutoPrepare
11601151
get => _maxAutoPrepare;
11611152
set
11621153
{
1163-
if (value < 0)
1164-
throw new ArgumentOutOfRangeException(nameof(value), value, $"{nameof(MaxAutoPrepare)} cannot be negative");
1154+
ArgumentOutOfRangeException.ThrowIfNegative(value);
11651155

11661156
_maxAutoPrepare = value;
11671157
SetValue(nameof(MaxAutoPrepare), value);
@@ -1183,8 +1173,7 @@ public int AutoPrepareMinUsages
11831173
get => _autoPrepareMinUsages;
11841174
set
11851175
{
1186-
if (value < 1)
1187-
throw new ArgumentOutOfRangeException(nameof(value), value, $"{nameof(AutoPrepareMinUsages)} must be 1 or greater");
1176+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value);
11881177

11891178
_autoPrepareMinUsages = value;
11901179
SetValue(nameof(AutoPrepareMinUsages), value);
@@ -1408,8 +1397,7 @@ public int InternalCommandTimeout
14081397

14091398
internal void PostProcessAndValidate()
14101399
{
1411-
if (string.IsNullOrWhiteSpace(Host))
1412-
throw new ArgumentException("Host can't be null");
1400+
ArgumentException.ThrowIfNullOrWhiteSpace(Host);
14131401
if (Multiplexing && !Pooling)
14141402
throw new ArgumentException("Pooling must be on to use multiplexing");
14151403
if (SslNegotiation == SslNegotiation.Direct && SslMode is not SslMode.Require and not SslMode.VerifyCA and not SslMode.VerifyFull)

src/Npgsql/NpgsqlDataSource.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,7 @@ protected virtual async ValueTask DisposeAsyncBase()
533533
}
534534

535535
private protected void CheckDisposed()
536-
{
537-
if (_isDisposed == 1)
538-
ThrowHelper.ThrowObjectDisposedException(GetType().FullName);
539-
}
536+
=> ObjectDisposedException.ThrowIf(_isDisposed == 1, this);
540537

541538
#endregion
542539

0 commit comments

Comments
 (0)