Skip to content

Commit

Permalink
Use ArgumentNullException.ThrowIfNull everywhere (btcpayserver#3239)
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier authored Dec 28, 2021
1 parent 9b7ca76 commit ed5b159
Show file tree
Hide file tree
Showing 50 changed files with 122 additions and 244 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ public void AllowUnsafeHashes(string script = null)
/// <param name="script"></param>
public void AllowInline(string script)
{
if (script is null)
throw new ArgumentNullException(nameof(script));
ArgumentNullException.ThrowIfNull(script);
var sha = GetSha256(script);
Add("script-src", $"'sha256-{sha}'");
}
Expand Down
3 changes: 1 addition & 2 deletions BTCPayServer.Common/BTCPayNetworkProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ public BTCPayNetworkBase GetNetwork(string cryptoCode)
}
public T GetNetwork<T>(string cryptoCode) where T : BTCPayNetworkBase
{
if (cryptoCode == null)
throw new ArgumentNullException(nameof(cryptoCode));
ArgumentNullException.ThrowIfNull(cryptoCode);
if (!_Networks.TryGetValue(cryptoCode.ToUpperInvariant(), out BTCPayNetworkBase network))
{
if (cryptoCode == "XBT")
Expand Down
3 changes: 1 addition & 2 deletions BTCPayServer.Rating/CurrencyNameTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ static CurrencyData[] LoadCurrency()

public CurrencyData GetCurrencyData(string currency, bool useFallback)
{
if (currency == null)
throw new ArgumentNullException(nameof(currency));
ArgumentNullException.ThrowIfNull(currency);
CurrencyData result;
if (!_Currencies.TryGetValue(currency.ToUpperInvariant(), out result))
{
Expand Down
9 changes: 3 additions & 6 deletions BTCPayServer.Rating/CurrencyPair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ public class CurrencyPair
{
public CurrencyPair(string left, string right)
{
if (right == null)
throw new ArgumentNullException(nameof(right));
if (left == null)
throw new ArgumentNullException(nameof(left));
ArgumentNullException.ThrowIfNull(right);
ArgumentNullException.ThrowIfNull(left);
Right = right.ToUpperInvariant();
Left = left.ToUpperInvariant();
}
Expand All @@ -25,8 +23,7 @@ public static CurrencyPair Parse(string str)
}
public static bool TryParse(string str, out CurrencyPair value)
{
if (str == null)
throw new ArgumentNullException(nameof(str));
ArgumentNullException.ThrowIfNull(str);
value = null;
str = str.Trim();
if (str.Length > 12)
Expand Down
6 changes: 2 additions & 4 deletions BTCPayServer.Rating/ExchangeRates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,8 @@ public class PairRate
{
public PairRate(CurrencyPair currencyPair, BidAsk bidAsk)
{
if (currencyPair == null)
throw new ArgumentNullException(nameof(currencyPair));
if (bidAsk == null)
throw new ArgumentNullException(nameof(bidAsk));
ArgumentNullException.ThrowIfNull(currencyPair);
ArgumentNullException.ThrowIfNull(bidAsk);
this.CurrencyPair = currencyPair;
this.BidAsk = bidAsk;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ internal PairRate[] GetResult()

public BackgroundFetcherRateProvider(IRateProvider inner)
{
if (inner == null)
throw new ArgumentNullException(nameof(inner));
ArgumentNullException.ThrowIfNull(inner);
_Inner = inner;
}

Expand Down
3 changes: 1 addition & 2 deletions BTCPayServer.Rating/Providers/ExchangeSharpRateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ namespace BTCPayServer.Services.Rates
readonly HttpClient _httpClient;
public ExchangeSharpRateProvider(HttpClient httpClient, bool reverseCurrencyPair = false)
{
if (httpClient == null)
throw new ArgumentNullException(nameof(httpClient));
ArgumentNullException.ThrowIfNull(httpClient);
ReverseCurrencyPair = reverseCurrencyPair;
_httpClient = httpClient;
}
Expand Down
3 changes: 1 addition & 2 deletions BTCPayServer.Rating/Providers/FallbackRateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public class FallbackRateProvider : IRateProvider
readonly IRateProvider[] _Providers;
public FallbackRateProvider(IRateProvider[] providers)
{
if (providers == null)
throw new ArgumentNullException(nameof(providers));
ArgumentNullException.ThrowIfNull(providers);
_Providers = providers;
}

Expand Down
6 changes: 2 additions & 4 deletions BTCPayServer.Rating/Providers/HttpClientRequestMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,8 @@ public IReadOnlyList<string> GetHeader(string name)

public HttpClientRequestMaker(IAPIRequestHandler api, HttpClient httpClient, CancellationToken cancellationToken)
{
if (api == null)
throw new ArgumentNullException(nameof(api));
if (httpClient == null)
throw new ArgumentNullException(nameof(httpClient));
ArgumentNullException.ThrowIfNull(api);
ArgumentNullException.ThrowIfNull(httpClient);
this.api = api;
_httpClient = httpClient;
_cancellationToken = cancellationToken;
Expand Down
6 changes: 2 additions & 4 deletions BTCPayServer.Rating/Services/RateFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ public async Task<RateResult> FetchRate(CurrencyPair pair, RateRules rules, Canc

public Dictionary<CurrencyPair, Task<RateResult>> FetchRates(HashSet<CurrencyPair> pairs, RateRules rules, CancellationToken cancellationToken)
{
if (rules == null)
throw new ArgumentNullException(nameof(rules));
ArgumentNullException.ThrowIfNull(rules);

var fetchingRates = new Dictionary<CurrencyPair, Task<RateResult>>();
var fetchingExchanges = new Dictionary<string, Task<QueryRateResult>>();
Expand All @@ -67,8 +66,7 @@ public Dictionary<CurrencyPair, Task<RateResult>> FetchRates(HashSet<CurrencyPai

public Task<RateResult> FetchRate(RateRule rateRule, CancellationToken cancellationToken)
{
if (rateRule == null)
throw new ArgumentNullException(nameof(rateRule));
ArgumentNullException.ThrowIfNull(rateRule);
var fetchingExchanges = new Dictionary<string, Task<QueryRateResult>>();
var dependentQueries = new List<Task<QueryRateResult>>();
foreach (var requiredExchange in rateRule.ExchangeRates)
Expand Down
3 changes: 1 addition & 2 deletions BTCPayServer/Configuration/ExternalConnectionString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ public ExternalConnectionString Clone()
}
public static bool TryParse(string str, out ExternalConnectionString result, out string error)
{
if (str == null)
throw new ArgumentNullException(nameof(str));
ArgumentNullException.ThrowIfNull(str);
error = null;
result = null;
var resultTemp = new ExternalConnectionString();
Expand Down
3 changes: 1 addition & 2 deletions BTCPayServer/Controllers/Macaroons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public Macaroon(byte[] bytes)
}
public static async Task<Macaroons> GetFromDirectoryAsync(string directoryPath)
{
if (directoryPath == null)
throw new ArgumentNullException(nameof(directoryPath));
ArgumentNullException.ThrowIfNull(directoryPath);
Macaroons macaroons = new Macaroons();
if (!Directory.Exists(directoryPath))
throw new DirectoryNotFoundException("Macaroons directory not found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ public class AddressClaimDestination : IBitcoinLikeClaimDestination

public AddressClaimDestination(BitcoinAddress bitcoinAddress)
{
if (bitcoinAddress == null)
throw new ArgumentNullException(nameof(bitcoinAddress));
ArgumentNullException.ThrowIfNull(bitcoinAddress);
_bitcoinAddress = bitcoinAddress;
}
public BitcoinAddress Address => _bitcoinAddress;
Expand Down
3 changes: 1 addition & 2 deletions BTCPayServer/Data/Payouts/BitcoinLike/UriClaimDestination.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public class UriClaimDestination : IBitcoinLikeClaimDestination

public UriClaimDestination(BitcoinUrlBuilder bitcoinUrl)
{
if (bitcoinUrl == null)
throw new ArgumentNullException(nameof(bitcoinUrl));
ArgumentNullException.ThrowIfNull(bitcoinUrl);
if (bitcoinUrl.Address is null)
throw new ArgumentException(nameof(bitcoinUrl));
_bitcoinUrl = bitcoinUrl;
Expand Down
3 changes: 1 addition & 2 deletions BTCPayServer/Data/StoreDataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public static bool SetStoreBlob(this StoreData storeData, StoreBlob storeBlob)

public static IEnumerable<ISupportedPaymentMethod> GetSupportedPaymentMethods(this StoreData storeData, BTCPayNetworkProvider networks)
{
if (storeData == null)
throw new ArgumentNullException(nameof(storeData));
ArgumentNullException.ThrowIfNull(storeData);
#pragma warning disable CS0618
bool btcReturned = false;

Expand Down
12 changes: 4 additions & 8 deletions BTCPayServer/DerivationSchemeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public class DerivationSchemeParser

public DerivationSchemeParser(BTCPayNetwork expectedNetwork)
{
if (expectedNetwork == null)
throw new ArgumentNullException(nameof(expectedNetwork));
ArgumentNullException.ThrowIfNull(expectedNetwork);
BtcPayNetwork = expectedNetwork;
}

Expand Down Expand Up @@ -44,8 +43,7 @@ public DerivationSchemeParser(BTCPayNetwork expectedNetwork)
}
}

if (str == null)
throw new ArgumentNullException(nameof(str));
ArgumentNullException.ThrowIfNull(str);
str = str.Trim();
var outputDescriptor = OutputDescriptor.Parse(str, Network);
switch(outputDescriptor)
Expand Down Expand Up @@ -95,8 +93,7 @@ public DerivationSchemeParser(BTCPayNetwork expectedNetwork)
public DerivationStrategyBase ParseElectrum(string str)
{

if (str == null)
throw new ArgumentNullException(nameof(str));
ArgumentNullException.ThrowIfNull(str);
str = str.Trim();
var data = Network.GetBase58CheckEncoder().DecodeData(str);
if (data.Length < 4)
Expand All @@ -123,8 +120,7 @@ public DerivationStrategyBase ParseElectrum(string str)

public DerivationStrategyBase Parse(string str)
{
if (str == null)
throw new ArgumentNullException(nameof(str));
ArgumentNullException.ThrowIfNull(str);
str = str.Trim();

HashSet<string> hintedLabels = new HashSet<string>();
Expand Down
24 changes: 8 additions & 16 deletions BTCPayServer/DerivationSchemeSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ public class DerivationSchemeSettings : ISupportedPaymentMethod
{
public static DerivationSchemeSettings Parse(string derivationStrategy, BTCPayNetwork network)
{
if (network == null)
throw new ArgumentNullException(nameof(network));
if (derivationStrategy == null)
throw new ArgumentNullException(nameof(derivationStrategy));
ArgumentNullException.ThrowIfNull(network);
ArgumentNullException.ThrowIfNull(derivationStrategy);
var result = new DerivationSchemeSettings();
result.Network = network;
var parser = new DerivationSchemeParser(network);
Expand All @@ -32,10 +30,8 @@ public static DerivationSchemeSettings Parse(string derivationStrategy, BTCPayNe

public static bool TryParseFromJson(string config, BTCPayNetwork network, out DerivationSchemeSettings strategy)
{
if (network == null)
throw new ArgumentNullException(nameof(network));
if (config == null)
throw new ArgumentNullException(nameof(config));
ArgumentNullException.ThrowIfNull(network);
ArgumentNullException.ThrowIfNull(config);
strategy = null;
try
{
Expand Down Expand Up @@ -90,10 +86,8 @@ private static bool TryParseXpub(string xpub, DerivationSchemeParser derivationS
public static bool TryParseFromWalletFile(string fileContents, BTCPayNetwork network, out DerivationSchemeSettings settings)
{
settings = null;
if (fileContents == null)
throw new ArgumentNullException(nameof(fileContents));
if (network == null)
throw new ArgumentNullException(nameof(network));
ArgumentNullException.ThrowIfNull(fileContents);
ArgumentNullException.ThrowIfNull(network);
var result = new DerivationSchemeSettings();
var derivationSchemeParser = new DerivationSchemeParser(network);
JObject jobj = null;
Expand Down Expand Up @@ -247,10 +241,8 @@ public DerivationSchemeSettings()

public DerivationSchemeSettings(DerivationStrategyBase derivationStrategy, BTCPayNetwork network)
{
if (network == null)
throw new ArgumentNullException(nameof(network));
if (derivationStrategy == null)
throw new ArgumentNullException(nameof(derivationStrategy));
ArgumentNullException.ThrowIfNull(network);
ArgumentNullException.ThrowIfNull(derivationStrategy);
AccountDerivation = derivationStrategy;
Network = network;
AccountKeySettings = derivationStrategy.GetExtPubKeys().Select(c => new AccountKeySettings()
Expand Down
3 changes: 1 addition & 2 deletions BTCPayServer/EventAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ public void Publish<T>(T evt) where T : class

public void Publish(object evt, Type evtType)
{
if (evt == null)
throw new ArgumentNullException(nameof(evt));
ArgumentNullException.ThrowIfNull(evt);
List<Action<object>> actionList = new List<Action<object>>();
lock (_Subscriptions)
{
Expand Down
3 changes: 1 addition & 2 deletions BTCPayServer/Events/InvoiceNeedUpdateEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ public class InvoiceNeedUpdateEvent
{
public InvoiceNeedUpdateEvent(string invoiceId)
{
if (invoiceId == null)
throw new ArgumentNullException(nameof(invoiceId));
ArgumentNullException.ThrowIfNull(invoiceId);
InvoiceId = invoiceId;
}

Expand Down
3 changes: 1 addition & 2 deletions BTCPayServer/ExplorerClientProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public ExplorerClient GetExplorerClient(string cryptoCode)

public ExplorerClient GetExplorerClient(BTCPayNetworkBase network)
{
if (network == null)
throw new ArgumentNullException(nameof(network));
ArgumentNullException.ThrowIfNull(network);
return GetExplorerClient(network.CryptoCode);
}

Expand Down
3 changes: 1 addition & 2 deletions BTCPayServer/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ public static void SetHeader(this HttpResponse resp, string name, string value)

public static bool IsLocalNetwork(string server)
{
if (server == null)
throw new ArgumentNullException(nameof(server));
ArgumentNullException.ThrowIfNull(server);
if (Uri.CheckHostName(server) == UriHostNameType.Dns)
{
return server.EndsWith(".internal", StringComparison.OrdinalIgnoreCase) ||
Expand Down
12 changes: 4 additions & 8 deletions BTCPayServer/Extensions/SSHClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public static class SSHClientExtensions
{
public static async Task<SshClient> ConnectAsync(this SSHSettings sshSettings, CancellationToken cancellationToken = default)
{
if (sshSettings == null)
throw new ArgumentNullException(nameof(sshSettings));
ArgumentNullException.ThrowIfNull(sshSettings);
TaskCompletionSource<SshClient> tcs = new TaskCompletionSource<SshClient>(TaskCreationOptions.RunContinuationsAsynchronously);
new Thread(() =>
{
Expand Down Expand Up @@ -58,10 +57,8 @@ public static string EscapeSingleQuotes(this string command)

public static Task<SSHCommandResult> RunBash(this SshClient sshClient, string command, TimeSpan? timeout = null)
{
if (sshClient == null)
throw new ArgumentNullException(nameof(sshClient));
if (command == null)
throw new ArgumentNullException(nameof(command));
ArgumentNullException.ThrowIfNull(sshClient);
ArgumentNullException.ThrowIfNull(command);
command = $"bash -c '{command.EscapeSingleQuotes()}'";
var sshCommand = sshClient.CreateCommand(command);
if (timeout is TimeSpan v)
Expand Down Expand Up @@ -106,8 +103,7 @@ private static SSHCommandResult CreateSSHCommandResult(SshCommand sshCommand)

public static async Task DisconnectAsync(this SshClient sshClient, CancellationToken cancellationToken = default)
{
if (sshClient == null)
throw new ArgumentNullException(nameof(sshClient));
ArgumentNullException.ThrowIfNull(sshClient);

TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
new Thread(() =>
Expand Down
3 changes: 1 addition & 2 deletions BTCPayServer/HostedServices/InvoiceWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@ public static PaymentMethod GetNearestClearedPayment(PaymentMethodDictionary all

private void Watch(string invoiceId)
{
if (invoiceId == null)
throw new ArgumentNullException(nameof(invoiceId));
ArgumentNullException.ThrowIfNull(invoiceId);

if (!_WatchRequests.Writer.TryWrite(invoiceId))
{
Expand Down
Loading

0 comments on commit ed5b159

Please sign in to comment.