Skip to content

Commit

Permalink
[btce, kraken] knowm#832 Throw specific exceptions on throttled, nonc…
Browse files Browse the repository at this point in the history
…e error or insufficient funds
  • Loading branch information
rafalkrupinski committed Jan 23, 2015
1 parent 8eaa567 commit 3e2408b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.xeiam.xchange.FundsExceededException;
import com.xeiam.xchange.NonceException;

import si.mazi.rescu.ParamsDigest;
import si.mazi.rescu.RestProxyFactory;
Expand All @@ -22,15 +22,13 @@
import com.xeiam.xchange.service.BaseExchangeService;
import com.xeiam.xchange.service.polling.BasePollingService;

/**
* @author Matija Mazi
*/
public class BTCEBasePollingService<T extends BTCE> extends BaseExchangeService implements BasePollingService {

protected static final String PREFIX = "btce";
protected static final String KEY_ORDER_SIZE_SCALE_DEFAULT = PREFIX + SUF_ORDER_SIZE_SCALE_DEFAULT;

private final Logger logger = LoggerFactory.getLogger(BTCEBasePollingService.class);
private static final String ERR_MSG_NONCE = "invalid nonce parameter; on key:";
private static final String ERR_MSG_FUNDS = "It is not enough ";

public final Set<CurrencyPair> currencyPairs = new HashSet<CurrencyPair>();

Expand Down Expand Up @@ -70,16 +68,25 @@ protected SynchronizedValueFactory<Integer> nextNonce() {
return nonceFactory;
}

protected void checkResult(BTCEReturn<?> info) {

if (!info.isSuccess()) {
throw new ExchangeException(info.getError());
protected void checkResult(BTCEReturn<?> result) {
String error = result.getError();

if (!result.isSuccess()) {
if (error != null) {
if (error.startsWith(ERR_MSG_NONCE))
throw new NonceException(error);
else if (error.startsWith(ERR_MSG_FUNDS))
throw new FundsExceededException(error);
}
throw new ExchangeException(error);
}
else if (info.getReturnValue() == null) {
throw new ExchangeException("Didn't receive any return value. Message: " + info.getError());

else if (result.getReturnValue() == null) {
throw new ExchangeException("Didn't receive any return value. Message: " + error);
}
else if (info.getError() != null) {
throw new ExchangeException(info.getError());

else if (error != null) {
throw new ExchangeException(error);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.HashSet;
import java.util.Set;

import com.xeiam.xchange.FrequencyLimitExceededException;
import com.xeiam.xchange.NonceException;
import si.mazi.rescu.ParamsDigest;
import si.mazi.rescu.RestProxyFactory;
import si.mazi.rescu.SynchronizedValueFactory;
Expand Down Expand Up @@ -142,7 +144,16 @@ public KrakenAssetPairs getKrakenAssetPairs(CurrencyPair... currencyPairs) throw
protected <R> R checkResult(KrakenResult<R> krakenResult) {

if (!krakenResult.isSuccess()) {
throw new ExchangeException(Arrays.toString(krakenResult.getError()));
String[] errors = krakenResult.getError();
if (errors.length == 0)
throw new ExchangeException("Missing error message");
String error = errors[0];
if ("EAPI:Invalid nonce".equals(error))
throw new NonceException(error);
else if ("EGeneral:Temporary lockout".equals(error))
throw new FrequencyLimitExceededException(error);

throw new ExchangeException(Arrays.toString(errors));
}

return krakenResult.getResult();
Expand Down

0 comments on commit 3e2408b

Please sign in to comment.