Fix createpaymentacct validation problems

- Add missing boilerplate error handling to GrpcPaymentAccountsService.

- Edit some PaymentAccountForm & PaymentAccountTypeAdapter exception
  messages that would be passed to CLI;  they should be in the same
  style as existing CLI err msgs.
This commit is contained in:
ghubstan 2020-11-19 13:38:34 -03:00
parent cb9a68bae1
commit ff887eb339
No known key found for this signature in database
GPG key ID: E35592D6800A861E
3 changed files with 105 additions and 57 deletions

View file

@ -147,7 +147,9 @@ public class PaymentAccountForm {
String json = gson.toJson(paymentAccount); // serializes target to json
outputStreamWriter.write(json);
} catch (Exception ex) {
log.error(format("Could not export json file for a %s account.", paymentMethod.getShortName()), ex);
log.error(format("Could not write json file for a %s account.", paymentMethodId), ex);
throw new IllegalStateException(
format("cannot create a payment account form for a %s payment method", paymentMethodId));
}
return file;
}
@ -158,6 +160,7 @@ public class PaymentAccountForm {
* @param jsonForm The file representing a new payment account form.
* @return A populated PaymentAccount subclass instance.
*/
@SuppressWarnings("unused")
@VisibleForTesting
public PaymentAccount toPaymentAccount(File jsonForm) {
String jsonString = toJsonString(jsonForm);
@ -181,7 +184,7 @@ public class PaymentAccountForm {
checkNotNull(jsonFile, "json file cannot be null");
return new String(Files.readAllBytes(Paths.get(jsonFile.getAbsolutePath())));
} catch (IOException ex) {
throw new IllegalStateException(format("Could not read content from file '%s'",
throw new IllegalStateException(format("cannot read content of file '%s'",
jsonFile.getAbsolutePath()), ex);
}
}
@ -218,7 +221,7 @@ public class PaymentAccountForm {
private Class<? extends PaymentAccount> getPaymentAccountClassFromJson(String json) {
Map<String, Object> jsonMap = gsonBuilder.create().fromJson(json, (Type) Object.class);
String paymentMethodId = checkNotNull((String) jsonMap.get("paymentMethodId"),
format("Could not find a paymentMethodId in the json string: %s", json));
format("cannot not find a paymentMethodId in json string: %s", json));
return getPaymentAccountClass(paymentMethodId);
}

View file

@ -116,7 +116,7 @@ class PaymentAccountTypeAdapter extends TypeAdapter<PaymentAccount> {
}
} catch (Exception ex) {
throw new IllegalStateException(
format("Could not serialize a %s to json", account.getClass().getSimpleName()), ex);
format("cannot not serialize a %s", account.getClass().getSimpleName()), ex);
}
});
out.endObject();
@ -144,21 +144,16 @@ class PaymentAccountTypeAdapter extends TypeAdapter<PaymentAccount> {
if (didReadCountryField(in, account, currentFieldName))
continue;
try {
Optional<Field> field = fieldSettersMap.keySet().stream()
.filter(k -> k.getName().equals(currentFieldName)).findFirst();
Optional<Field> field = fieldSettersMap.keySet().stream()
.filter(k -> k.getName().equals(currentFieldName)).findFirst();
field.ifPresentOrElse((f) -> invokeSetterMethod(account, f, in), () -> {
throw new IllegalStateException(
format("Could not de-serialize json to a '%s' because there is no %s field.",
account.getClass().getSimpleName(),
currentFieldName));
});
} catch (Exception ex) {
field.ifPresentOrElse((f) -> invokeSetterMethod(account, f, in), () -> {
throw new IllegalStateException(
format("Could not de-serialize json to a '%s'.",
account.getClass().getSimpleName()), ex);
}
format("programmer error: cannot de-serialize json to a '%s' "
+ " because there is no %s field.",
account.getClass().getSimpleName(),
currentFieldName));
});
}
in.endObject();
if (log.isDebugEnabled())
@ -178,19 +173,20 @@ class PaymentAccountTypeAdapter extends TypeAdapter<PaymentAccount> {
} else if (isSetterOnPaymentAccountPayloadClass(setter.get(), account)) {
setter.get().invoke(account.getPaymentAccountPayload(), nextStringOrNull(jsonReader));
} else {
String exMsg = format("Could not de-serialize json to a '%s' using reflection"
+ " because the setter's declaring class was not found.",
String exMsg = format("programmer error: cannot de-serialize json to a '%s' using reflection"
+ " because the setter method's declaring class was not found.",
account.getClass().getSimpleName());
throw new IllegalStateException(exMsg);
}
} catch (IllegalAccessException | InvocationTargetException ex) {
throw new IllegalStateException(
format("Could not de-serialize json to a '%s' due to reflection error.",
format("programmer error: cannot de-serialize json to a '%s' due to reflection error.",
account.getClass().getSimpleName()), ex);
}
} else {
throw new IllegalStateException(
format("Could not de-serialize json to a '%s' because there is no setter for field %s.",
format("programmer error: cannot de-serialize json to a '%s' "
+ " because there is no setter method for field %s.",
account.getClass().getSimpleName(),
field.getName()));
}
@ -233,7 +229,9 @@ class PaymentAccountTypeAdapter extends TypeAdapter<PaymentAccount> {
return in.nextString();
}
} catch (IOException ex) {
throw new IllegalStateException("Could not peek at next String value in JsonReader.", ex);
throw new IllegalStateException(
"programmer error: cannot not peek at next string value in json reader.",
ex);
}
}
@ -247,7 +245,9 @@ class PaymentAccountTypeAdapter extends TypeAdapter<PaymentAccount> {
return in.nextLong();
}
} catch (IOException ex) {
throw new IllegalStateException("Could not peek at next Long value in JsonReader.", ex);
throw new IllegalStateException(
"programmer error: cannot not peek at next long value in json reader.",
ex);
}
}
@ -290,9 +290,8 @@ class PaymentAccountTypeAdapter extends TypeAdapter<PaymentAccount> {
account.setSingleTradeCurrency(fiatCurrency);
return true;
} else {
throw new IllegalStateException(
format("Could not de-serialize json to a '%s' because %s is an invalid country code.",
account.getClass().getSimpleName(), countryCode));
throw new IllegalArgumentException(
format("'%s' is an invalid country code.", countryCode));
}
} else {
return false;
@ -307,7 +306,7 @@ class PaymentAccountTypeAdapter extends TypeAdapter<PaymentAccount> {
+ "." + paymentAccountType.getSimpleName() + "Payload");
} catch (Exception ex) {
throw new IllegalStateException(
format("Could not get payload class for %s",
format("programmer error: cannot get payload class for %s",
paymentAccountType.getSimpleName()), ex);
}
}
@ -319,11 +318,15 @@ class PaymentAccountTypeAdapter extends TypeAdapter<PaymentAccount> {
paymentAccount.init();
return paymentAccount;
} catch (NoSuchMethodException ex) {
throw new IllegalStateException(format("No default declared constructor found for class %s",
paymentAccountType.getSimpleName()), ex);
throw new IllegalStateException(
format("programmer error: no default declared constructor found for class %s",
paymentAccountType.getSimpleName()),
ex);
} catch (IllegalAccessException | InstantiationException | InvocationTargetException ex) {
throw new IllegalStateException(format("Could not instantiate class %s",
paymentAccountType.getSimpleName()), ex);
throw new IllegalStateException(
format("programmer error: cannot instantiate class %s",
paymentAccountType.getSimpleName()),
ex);
}
}
}

View file

@ -31,6 +31,8 @@ import bisq.proto.grpc.GetPaymentMethodsReply;
import bisq.proto.grpc.GetPaymentMethodsRequest;
import bisq.proto.grpc.PaymentAccountsGrpc;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import io.grpc.stub.StreamObserver;
import javax.inject.Inject;
@ -50,46 +52,86 @@ class GrpcPaymentAccountsService extends PaymentAccountsGrpc.PaymentAccountsImpl
@Override
public void createPaymentAccount(CreatePaymentAccountRequest req,
StreamObserver<CreatePaymentAccountReply> responseObserver) {
PaymentAccount paymentAccount = coreApi.createPaymentAccount(req.getPaymentAccountForm());
var reply = CreatePaymentAccountReply.newBuilder()
.setPaymentAccount(paymentAccount.toProtoMessage())
.build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
try {
PaymentAccount paymentAccount = coreApi.createPaymentAccount(req.getPaymentAccountForm());
var reply = CreatePaymentAccountReply.newBuilder()
.setPaymentAccount(paymentAccount.toProtoMessage())
.build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
} catch (IllegalArgumentException cause) {
var ex = new StatusRuntimeException(Status.INVALID_ARGUMENT.withDescription(cause.getMessage()));
responseObserver.onError(ex);
throw ex;
} catch (IllegalStateException cause) {
var ex = new StatusRuntimeException(Status.UNKNOWN.withDescription(cause.getMessage()));
responseObserver.onError(ex);
throw ex;
}
}
@Override
public void getPaymentAccounts(GetPaymentAccountsRequest req,
StreamObserver<GetPaymentAccountsReply> responseObserver) {
var paymentAccounts = coreApi.getPaymentAccounts().stream()
.map(PaymentAccount::toProtoMessage)
.collect(Collectors.toList());
var reply = GetPaymentAccountsReply.newBuilder()
.addAllPaymentAccounts(paymentAccounts).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
try {
var paymentAccounts = coreApi.getPaymentAccounts().stream()
.map(PaymentAccount::toProtoMessage)
.collect(Collectors.toList());
var reply = GetPaymentAccountsReply.newBuilder()
.addAllPaymentAccounts(paymentAccounts).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
} catch (IllegalArgumentException cause) {
var ex = new StatusRuntimeException(Status.INVALID_ARGUMENT.withDescription(cause.getMessage()));
responseObserver.onError(ex);
throw ex;
} catch (IllegalStateException cause) {
var ex = new StatusRuntimeException(Status.UNKNOWN.withDescription(cause.getMessage()));
responseObserver.onError(ex);
throw ex;
}
}
@Override
public void getPaymentMethods(GetPaymentMethodsRequest req,
StreamObserver<GetPaymentMethodsReply> responseObserver) {
var paymentMethods = coreApi.getFiatPaymentMethods().stream()
.map(PaymentMethod::toProtoMessage)
.collect(Collectors.toList());
var reply = GetPaymentMethodsReply.newBuilder()
.addAllPaymentMethods(paymentMethods).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
try {
var paymentMethods = coreApi.getFiatPaymentMethods().stream()
.map(PaymentMethod::toProtoMessage)
.collect(Collectors.toList());
var reply = GetPaymentMethodsReply.newBuilder()
.addAllPaymentMethods(paymentMethods).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
} catch (IllegalArgumentException cause) {
var ex = new StatusRuntimeException(Status.INVALID_ARGUMENT.withDescription(cause.getMessage()));
responseObserver.onError(ex);
throw ex;
} catch (IllegalStateException cause) {
var ex = new StatusRuntimeException(Status.UNKNOWN.withDescription(cause.getMessage()));
responseObserver.onError(ex);
throw ex;
}
}
@Override
public void getPaymentAccountForm(GetPaymentAccountFormRequest req,
StreamObserver<GetPaymentAccountFormReply> responseObserver) {
var paymentAccountFormJson = coreApi.getPaymentAccountForm(req.getPaymentMethodId());
var reply = GetPaymentAccountFormReply.newBuilder()
.setPaymentAccountFormJson(paymentAccountFormJson)
.build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
try {
var paymentAccountFormJson = coreApi.getPaymentAccountForm(req.getPaymentMethodId());
var reply = GetPaymentAccountFormReply.newBuilder()
.setPaymentAccountFormJson(paymentAccountFormJson)
.build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
} catch (IllegalArgumentException cause) {
var ex = new StatusRuntimeException(Status.INVALID_ARGUMENT.withDescription(cause.getMessage()));
responseObserver.onError(ex);
throw ex;
} catch (IllegalStateException cause) {
var ex = new StatusRuntimeException(Status.UNKNOWN.withDescription(cause.getMessage()));
responseObserver.onError(ex);
throw ex;
}
}
}