Search in sources :

Example 1 with ExchangeSecurityException

use of org.knowm.xchange.exceptions.ExchangeSecurityException in project XChange by knowm.

the class CmcErrorAdapter method adapt.

/**
 * Parse errors from HTTP exceptions
 */
public static void adapt(HttpStatusIOException httpStatusException) {
    String msg = "HTTP Status: " + httpStatusException.getHttpStatusCode();
    // if we have a HTTP body try to parse more error details from body
    if (isNotEmpty(httpStatusException.getHttpBody())) {
        ObjectMapper mapper = new ObjectMapper();
        CmcResult result;
        try {
            result = mapper.readValue(httpStatusException.getHttpBody(), CmcResult.class);
        } catch (Exception e) {
            // but ignore errors on parsing and throw generic ExchangeException instead
            throw new ExchangeException(msg, httpStatusException);
        }
        // but if it contains a parsable result, then try to parse errors from result:
        if (result.getStatus() != null && isNotEmpty(result.getStatus().getErrorMessage()) && !result.isSuccess()) {
            String error = result.getStatus().getErrorMessage();
            if (result.getStatus().getErrorCode() == 401) {
                throw new ExchangeSecurityException(error);
            }
            if (result.getStatus().getErrorCode() == 402) {
                throw new FundsExceededException(error);
            }
            if (result.getStatus().getErrorCode() == 429) {
                throw new FrequencyLimitExceededException(error);
            }
            msg = error + " - ErrorCode: " + result.getStatus().getErrorCode();
            throw new ExchangeException(msg);
        }
    }
    // else: just throw ExchangeException with causing Exception
    throw new ExchangeException(msg, httpStatusException);
}
Also used : CmcResult(org.knowm.xchange.coinmarketcap.pro.v1.dto.CmcResult) ExchangeException(org.knowm.xchange.exceptions.ExchangeException) FrequencyLimitExceededException(org.knowm.xchange.exceptions.FrequencyLimitExceededException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FundsExceededException(org.knowm.xchange.exceptions.FundsExceededException) FrequencyLimitExceededException(org.knowm.xchange.exceptions.FrequencyLimitExceededException) ExchangeSecurityException(org.knowm.xchange.exceptions.ExchangeSecurityException) ExchangeException(org.knowm.xchange.exceptions.ExchangeException) HttpStatusIOException(si.mazi.rescu.HttpStatusIOException) ExchangeSecurityException(org.knowm.xchange.exceptions.ExchangeSecurityException) FundsExceededException(org.knowm.xchange.exceptions.FundsExceededException)

Example 2 with ExchangeSecurityException

use of org.knowm.xchange.exceptions.ExchangeSecurityException in project XChange by knowm.

the class DeribitBaseService method authOverClientSignature.

private DeribitAuthentication authOverClientSignature(String clientId, String clientSecret) throws DeribitException, IOException {
    if (clientId == null || clientId.isEmpty()) {
        throw new ExchangeSecurityException("API key must not be empty.");
    }
    if (clientSecret == null || clientSecret.isEmpty()) {
        throw new ExchangeException("API secret must not be empty.");
    }
    Mac mac;
    try {
        mac = Mac.getInstance(BaseParamsDigest.HMAC_SHA_256);
        final SecretKey secretKey = new SecretKeySpec(clientSecret.getBytes("UTF-8"), BaseParamsDigest.HMAC_SHA_256);
        mac.init(secretKey);
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
        throw new ExchangeException("Invalid API secret", e);
    }
    String timestamp = "" + System.currentTimeMillis();
    String nonce = timestamp;
    String data = "";
    String toSign = timestamp + "\n" + nonce + "\n" + data;
    String signature = DigestUtils.bytesToHex(mac.doFinal(toSign.getBytes("UTF-8"))).toLowerCase();
    return deribit.auth(GrantType.client_signature, null, null, clientId, null, null, timestamp, signature, nonce, null, null).getResult();
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) ExchangeException(org.knowm.xchange.exceptions.ExchangeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) ExchangeSecurityException(org.knowm.xchange.exceptions.ExchangeSecurityException) Mac(javax.crypto.Mac)

Aggregations

ExchangeException (org.knowm.xchange.exceptions.ExchangeException)2 ExchangeSecurityException (org.knowm.xchange.exceptions.ExchangeSecurityException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Mac (javax.crypto.Mac)1 SecretKey (javax.crypto.SecretKey)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1 CmcResult (org.knowm.xchange.coinmarketcap.pro.v1.dto.CmcResult)1 FrequencyLimitExceededException (org.knowm.xchange.exceptions.FrequencyLimitExceededException)1 FundsExceededException (org.knowm.xchange.exceptions.FundsExceededException)1 HttpStatusIOException (si.mazi.rescu.HttpStatusIOException)1