ModelBinders should not throw exception, but return error on ModelState

This commit is contained in:
nicolas.dorier
2021-04-26 12:55:03 +09:00
parent dcc4214dcb
commit 8d5c3c5cdd
3 changed files with 18 additions and 2 deletions

View File

@@ -43,11 +43,17 @@ namespace BTCPayServer.ModelBinders
var data = network.NBXplorerNetwork.DerivationStrategyFactory.Parse(key);
if (!bindingContext.ModelType.IsInstanceOfType(data))
{
throw new FormatException("Invalid destination type");
bindingContext.Result = ModelBindingResult.Failed();
bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Invalid derivation scheme");
return Task.CompletedTask;
}
bindingContext.Result = ModelBindingResult.Success(data);
}
catch { throw new FormatException("Invalid derivation scheme"); }
catch
{
bindingContext.Result = ModelBindingResult.Failed();
bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Invalid derivation scheme");
}
return Task.CompletedTask;
}

View File

@@ -31,6 +31,11 @@ namespace BTCPayServer.ModelBinders
{
bindingContext.Result = ModelBindingResult.Success(paymentId);
}
else
{
bindingContext.Result = ModelBindingResult.Failed();
bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Invalid payment id");
}
return Task.CompletedTask;
}
}

View File

@@ -30,6 +30,11 @@ namespace BTCPayServer.ModelBinders
{
bindingContext.Result = ModelBindingResult.Success(walletId);
}
else
{
bindingContext.Result = ModelBindingResult.Failed();
bindingContext.ModelState.AddModelError(bindingContext.ModelName, "Invalid wallet id");
}
return Task.CompletedTask;
}
}