Skip to content

Commit

Permalink
refactor!: Use IllegalStateException instead of `IllegalArgumentExc…
Browse files Browse the repository at this point in the history
…eption` in some scenarios.
  • Loading branch information
overcat committed Nov 14, 2024
1 parent a3f2172 commit a56b374
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 25 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/stellar/sdk/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public abstract class Asset implements Comparable<Asset> {
*
* @param canonicalForm Canonical string representation of an Alpha4 or Alpha12 asset
* @return Asset or throws IllegalArgumentException if not Alpha4 or Alpha12 asset code
* @throws IllegalArgumentException if the asset string is invalid
*/
public static Asset create(String canonicalForm) {
if (canonicalForm.equals("native")) {
Expand Down Expand Up @@ -114,6 +115,7 @@ public static Asset fromXdr(org.stellar.sdk.xdr.Asset xdr) {
* @param code The asset code.
* @param issuer The issuer account ID.
* @return Asset (alphanum4 or alphanum12)
* @throws AssetCodeLengthInvalidException if the asset code is invalid
*/
public static Asset createNonNativeAsset(@NonNull String code, @NonNull String issuer) {
if (!code.isEmpty() && code.length() <= 4) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public final class AssetTypeCreditAlphaNum12 extends AssetTypeCreditAlphaNum {
*
* @param code Asset code
* @param issuer Asset issuer
* @throws AssetCodeLengthInvalidException when code is invalid
*/
public AssetTypeCreditAlphaNum12(String code, String issuer) {
super(code, issuer);
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/org/stellar/sdk/KeyPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,19 +189,21 @@ public byte[] getPublicKey() {

/** Returns the signature hint for this keypair. */
public SignatureHint getSignatureHint() {
ByteArrayOutputStream publicKeyBytesStream = new ByteArrayOutputStream();
XdrDataOutputStream xdrOutputStream = new XdrDataOutputStream(publicKeyBytesStream);

try {
ByteArrayOutputStream publicKeyBytesStream = new ByteArrayOutputStream();
XdrDataOutputStream xdrOutputStream = new XdrDataOutputStream(publicKeyBytesStream);
this.getXdrPublicKey().encode(xdrOutputStream);
byte[] publicKeyBytes = publicKeyBytesStream.toByteArray();
byte[] signatureHintBytes =
Arrays.copyOfRange(publicKeyBytes, publicKeyBytes.length - 4, publicKeyBytes.length);
SignatureHint signatureHint = new SignatureHint();
signatureHint.setSignatureHint(signatureHintBytes);
return signatureHint;
} catch (IOException e) {
throw new UnexpectedException(e);
}

byte[] publicKeyBytes = publicKeyBytesStream.toByteArray();
byte[] signatureHintBytes =
Arrays.copyOfRange(publicKeyBytes, publicKeyBytes.length - 4, publicKeyBytes.length);
SignatureHint signatureHint = new SignatureHint();
signatureHint.setSignatureHint(signatureHintBytes);
return signatureHint;
}

/** Returns the XDR {@link org.stellar.sdk.xdr.PublicKey} for this keypair. */
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/stellar/sdk/SorobanDataBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.NonNull;
import lombok.Value;
import org.jetbrains.annotations.Nullable;
import org.stellar.sdk.exception.UnexpectedException;
import org.stellar.sdk.xdr.ExtensionPoint;
import org.stellar.sdk.xdr.Int64;
import org.stellar.sdk.xdr.LedgerFootprint;
Expand Down Expand Up @@ -143,7 +144,7 @@ public SorobanTransactionData build() {
try {
return SorobanTransactionData.fromXdrByteArray(data.toXdrByteArray());
} catch (IOException e) {
throw new IllegalArgumentException("Copy SorobanData failed, please report this bug.", e);
throw new UnexpectedException("Copy SorobanData failed, please report this bug.", e);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/stellar/sdk/SorobanServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public GetTransactionsResponse getTransactions(GetTransactionsRequest getTransac
* paginate as long as the pages fall within the history retention of their corresponding RPC
* provider.
*
* @param getLedgersRequest The {@link GetEventsRequest} to use for the request.
* @param getLedgersRequest The {@link GetLedgersRequest} to use for the request.
* @return A {@link GetLedgersResponse} object containing the ledgers that match the request.
* @throws org.stellar.sdk.exception.NetworkException All the exceptions below are subclasses of
* NetworkError
Expand Down Expand Up @@ -582,6 +582,7 @@ private Transaction assembleTransaction(
// existing entries are empty and the simulation result contains auth entries.
if (simulateTransactionResponse.getResults() == null
|| simulateTransactionResponse.getResults().size() != 1) {

throw new IllegalArgumentException(
"invalid simulateTransactionResponse: results must contain exactly one element if the operation is an InvokeHostFunctionOperation");
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/stellar/sdk/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public TransactionEnvelope toEnvelopeXdr() {
v0Envelope.setSignatures(signatures);
xdr.setV0(v0Envelope);
} else {
throw new IllegalArgumentException("invalid envelope type: " + this.envelopeType);
throw new IllegalStateException("invalid envelope type: " + this.envelopeType);
}

return xdr;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/stellar/sdk/TransactionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public TransactionBuilder addPreconditions(@NonNull TransactionPreconditions pre
*/
public TransactionBuilder addMemo(@NonNull Memo memo) {
if (this.memo != null) {
throw new IllegalArgumentException("Memo has been already added.");
throw new IllegalStateException("Memo has been already added.");
}
this.memo = memo;
return this;
Expand Down Expand Up @@ -154,7 +154,7 @@ public TransactionBuilder setBaseFee(long baseFee) {
public Transaction build() {
// ensure that the preconditions are valid
if (preconditions.getTimeBounds() != null && txTimeout != null) {
throw new IllegalArgumentException(
throw new IllegalStateException(
"Can not set both TransactionPreconditions.timeBounds and timeout.");
}

Expand All @@ -170,11 +170,11 @@ public Transaction build() {
preconditions.isValid();

if (baseFee == null) {
throw new IllegalArgumentException("baseFee has to be set. you must call setBaseFee().");
throw new IllegalStateException("baseFee has to be set. you must call setBaseFee().");
}

if (network == null) {
throw new IllegalArgumentException("network has to be set. you must call setNetwork().");
throw new IllegalStateException("network has to be set. you must call setNetwork().");
}

long sequenceNumber = sourceAccount.getIncrementedSequenceNumber();
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/stellar/sdk/TransactionPreconditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,16 @@ public class TransactionPreconditions {
/**
* Validates the preconditions.
*
* @throws IllegalArgumentException if the preconditions are invalid
* @throws IllegalStateException if the preconditions are invalid
*/
// TODO: check valid
public void isValid() {
if (timeBounds == null) {
throw new IllegalArgumentException("Invalid preconditions, must define timebounds");
throw new IllegalStateException("Invalid preconditions, must define timebounds");
}

if (extraSigners.size() > MAX_EXTRA_SIGNERS_COUNT) {
throw new IllegalArgumentException(
throw new IllegalStateException(
"Invalid preconditions, too many extra signers, can only have up to "
+ MAX_EXTRA_SIGNERS_COUNT);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/stellar/sdk/StrKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public void testEncodeToXDRMuxedAccountInvalidAddress() {
StrKey.encodeToXDRMuxedAccount("MBU2RRGLXH3E5CQHTD3ODLDF2BWDCYUSSBLLZ5GNW7JXHDIYKXZWGTOG");
fail();
} catch (IllegalArgumentException e) {
assertEquals("Checksum invalid", e.getMessage());
assertEquals("Invalid data length, expected 40 bytes, got 32", e.getMessage());
}

try {
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/stellar/sdk/TransactionBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public void testBuilderRequiresTimeoutOrTimeBounds() {
.setBaseFee(Transaction.MIN_BASE_FEE)
.build();
fail();
} catch (IllegalArgumentException ignored) {
} catch (IllegalStateException ignored) {
}
}

Expand Down Expand Up @@ -561,7 +561,7 @@ public void testBuilderFailsWhenTooManyExtraSigners() {
.setBaseFee(Transaction.MIN_BASE_FEE)
.build();
fail();
} catch (IllegalArgumentException ignored) {
} catch (IllegalStateException ignored) {
}
}

Expand Down Expand Up @@ -627,7 +627,7 @@ public void testBuilderFailsWhenTimeBoundsAndTimeoutBothSet() {
.setTimeout(30)
.build();
fail();
} catch (IllegalArgumentException exception) {
} catch (IllegalStateException exception) {
assertTrue(
exception
.getMessage()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public void itChecksNonValidityOfTimeBounds() {
try {
preconditions.isValid();
fail();
} catch (IllegalArgumentException ignored) {
} catch (IllegalStateException ignored) {
}
}

Expand All @@ -225,7 +225,7 @@ public void itChecksNonValidityOfExtraSignersSize() {
try {
preconditions.isValid();
fail();
} catch (IllegalArgumentException ignored) {
} catch (IllegalStateException ignored) {
}
}

Expand All @@ -235,7 +235,7 @@ public void itChecksValidityWhenNoTimeboundsSet() {
try {
preconditions.isValid();
fail();
} catch (IllegalArgumentException exception) {
} catch (IllegalStateException exception) {
assertTrue(exception.getMessage().contains("Invalid preconditions, must define timebounds"));
}
}
Expand Down

0 comments on commit a56b374

Please sign in to comment.