Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

Commit

Permalink
Error handling + action fix
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-cortes committed Jul 8, 2021
1 parent 6dbe445 commit c501659
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 26 deletions.
30 changes: 30 additions & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,36 @@ export const SwapGenericAPIError = createCustomErrorClass(
"SwapGenericAPIError"
);

export const JSONRPCResponseError = createCustomErrorClass(
"JSONRPCResponseError"
);
export const JSONDecodeError = createCustomErrorClass("JSONDecodeError");
export const NoIPHeaderError = createCustomErrorClass("NoIPHeaderError");
export const CurrencyNotSupportedError = createCustomErrorClass(
"CurrencyNotSupportedError"
);
export const CurrencyDisabledError = createCustomErrorClass(
"CurrencyDisabledError"
);
export const CurrencyDisabledAsInputError = createCustomErrorClass(
"CurrencyDisabledAsInputError"
);
export const CurrencyDisabledAsOutputError = createCustomErrorClass(
"CurrencyDisabledAsOutputError"
);
export const CurrencyNotSupportedByProviderError = createCustomErrorClass(
"CurrencyNotSupportedByProviderError"
);
export const TradeMethodNotSupportedError = createCustomErrorClass(
"TradeMethodNotSupportedError"
);
export const UnexpectedError = createCustomErrorClass("UnexpectedError");
export const NotImplementedError = createCustomErrorClass(
"NotImplementedError"
);
export const ValidationError = createCustomErrorClass("ValidationError");
export const AccessDeniedError = createCustomErrorClass("AccessDeniedError");

export const AlgorandASANotOptInInRecipient = createCustomErrorClass(
"AlgorandASANotOptInInRecipient"
);
Expand Down
15 changes: 11 additions & 4 deletions src/exchange/swap/getExchangeRates.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Unit } from "../../types";
import { formatCurrencyUnit } from "../../currencies";
import { mockGetExchangeRates } from "./mock";
import network from "../../network";
import { getSwapAPIBaseURL } from "./";
import { getSwapAPIError, getSwapAPIBaseURL } from "./";
import { getEnv } from "../../env";
import { BigNumber } from "bignumber.js";
import {
Expand Down Expand Up @@ -109,17 +109,24 @@ const inferError = (
minAmountFrom: string,
maxAmountFrom: string,
errorCode?: number,
errorMessage?: string,
}
): ?Error => {
const tenPowMagnitude = BigNumber(10).pow(unitFrom.magnitude);
const { amountTo, minAmountFrom, maxAmountFrom, errorCode } = responseData;
const {
amountTo,
minAmountFrom,
maxAmountFrom,
errorCode,
errorMessage,
} = responseData;

if (!amountTo) {
// We are in an error case regardless of api version.
if (errorCode) {
// TODO Do we have consistent errorCodes for providers?
return new Error(`Generic Swap API error with code [${errorCode}]`);
return getSwapAPIError(errorCode, errorMessage);
}

// For out of range errors we will have a min/max pairing
if (minAmountFrom) {
const isTooSmall = BigNumber(apiAmount).lte(minAmountFrom);
Expand Down
38 changes: 37 additions & 1 deletion src/exchange/swap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ import getKYCStatus from "./getKYCStatus";
import submitKYC from "./submitKYC";
import initSwap from "./initSwap";
import { getEnv } from "../../env";
import {
JSONRPCResponseError,
JSONDecodeError,
NoIPHeaderError,
CurrencyNotSupportedError,
CurrencyDisabledError,
CurrencyDisabledAsInputError,
CurrencyDisabledAsOutputError,
CurrencyNotSupportedByProviderError,
TradeMethodNotSupportedError,
UnexpectedError,
NotImplementedError,
ValidationError,
AccessDeniedError,
} from "../../errors";

export const operationStatusList = {
finishedOK: ["finished"],
Expand Down Expand Up @@ -117,7 +132,28 @@ const USStates = {

const countries = {
US: "United States",
ES: "Spain", // FIXME remove before merging
};

const swapBackendErrorCodes = {
"100": JSONRPCResponseError,
"101": JSONDecodeError,
"200": NoIPHeaderError,
"300": CurrencyNotSupportedError,
"301": CurrencyDisabledError,
"302": CurrencyDisabledAsInputError,
"303": CurrencyDisabledAsOutputError,
"304": CurrencyNotSupportedByProviderError,
"400": TradeMethodNotSupportedError,
"500": UnexpectedError,
"600": NotImplementedError,
"700": ValidationError,
"701": AccessDeniedError,
};

export const getSwapAPIError = (errorCode: number, errorMessage?: string) => {
if (errorCode in swapBackendErrorCodes)
return new swapBackendErrorCodes[errorCode](errorMessage);
return new Error(errorMessage);
};

export {
Expand Down
39 changes: 20 additions & 19 deletions src/exchange/swap/submitKYC.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,31 @@
import network from "../../network";
import { getSwapAPIBaseURL } from "./";
import type { KYCData, SubmitKYC } from "./types";
import { SwapCheckKYCStatusFailed, SwapSubmitKYCFailed } from "../../errors";
import { ValidationError } from "../../errors";

export const submitKYC: SubmitKYC = async (provider: string, data: KYCData) => {
const res = await network({
method: "POST",
url: `${getSwapAPIBaseURL()}/provider/${provider}/user`,
data,
});
try {
const res = await network({
method: "POST",
url: `${getSwapAPIBaseURL()}/provider/${provider}/user`,
data,
});

if (res.data?.code) {
// Wyre KYC errors get mapped to {code, message}
return new SwapSubmitKYCFailed(res.data.message);
}

if (!res.data?.status) {
return new SwapCheckKYCStatusFailed();
}
const { id, status } = res.data;

const { id, status } = res.data;
return {
id,
status,
};
} catch (error) {
// Nb this is the best we have, no _per field_ validation but rather
// error handling at the top level.

return {
id,
status,
};
// TODO detect KYC specs changed to throw specific error
return {
error: new ValidationError(error.message),
};
}
};

export default submitKYC;
5 changes: 4 additions & 1 deletion src/exchange/swap/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import type {
export type ValidKYCStatus = "open" | "pending" | "approved" | "closed";
export type KYCStatus = { id: string, status: ValidKYCStatus };
export type GetKYCStatus = (string, string) => Promise<KYCStatus>;
export type SubmitKYC = (string, KYCData) => Promise<KYCStatus>;
export type SubmitKYC = (
string,
KYCData
) => Promise<KYCStatus | { error: Error }>;

export type KYCData = {
firstName: string,
Expand Down
11 changes: 10 additions & 1 deletion src/hw/actions/initSwap.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type InitSwapRequest = {
exchangeRate: ExchangeRate,
transaction: Transaction,
userId?: string,
requireLatestFirmware?: boolean,
};

type Result =
Expand Down Expand Up @@ -117,7 +118,14 @@ export const createAction = (
state.freezeReduxDevice
);

const { exchange, exchangeRate, transaction, userId } = initSwapRequest;
const {
exchange,
exchangeRate,
transaction,
userId,
requireLatestFirmware,
} = initSwapRequest;

const {
fromAccount,
fromParentAccount,
Expand All @@ -134,6 +142,7 @@ export const createAction = (
{ account: mainFromAccount },
{ account: maintoAccount },
],
requireLatestFirmware,
}
);

Expand Down

0 comments on commit c501659

Please sign in to comment.