-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Live 14437 sell fund improve exchange sdk code error (#94)
* refactor: error handling * refactor: rename errors * test: fix swap tests * refactor: remove comment
- Loading branch information
1 parent
1b23f91
commit 9c25bf9
Showing
11 changed files
with
243 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
export class ExchangeBaseError extends Error { | ||
cause: { | ||
exchangeErrorCode: string; | ||
[key: string]: string | Error | unknown | undefined; | ||
}; | ||
message: string; | ||
constructor(exchangeErrorCode = "exchange000", nestedError?: Error) { | ||
super(); | ||
this.name = "ExchangeError"; | ||
// Support log for different error types. | ||
this.cause = { | ||
exchangeErrorCode, | ||
...(nestedError?.constructor !== Object || | ||
nestedError?.constructor !== Array | ||
? { message: `${nestedError}` } | ||
: {}), | ||
...nestedError, | ||
}; | ||
this.message = nestedError?.message | ||
? nestedError.message | ||
: `${nestedError}`; | ||
} | ||
} | ||
|
||
export class NonceStepError extends ExchangeBaseError { | ||
constructor(nestedError?: Error) { | ||
super("exchange001", nestedError); | ||
this.name = "NonceStepError"; | ||
} | ||
} | ||
|
||
export class PayloadStepError extends ExchangeBaseError { | ||
constructor(nestedError?: Error) { | ||
super("exchange002", nestedError); | ||
this.name = "PayloadStepError"; | ||
} | ||
} | ||
|
||
export class SignatureStepError extends ExchangeBaseError { | ||
constructor(nestedError?: Error) { | ||
super("exchange003", nestedError); | ||
this.name = "SignatureStepError"; | ||
} | ||
} | ||
export class NotEnoughFunds extends ExchangeBaseError { | ||
constructor() { | ||
super("exchange004"); | ||
this.name = "NotEnoughFunds"; | ||
} | ||
} | ||
|
||
export class ListAccountError extends ExchangeBaseError { | ||
constructor(nestedError?: Error) { | ||
super("exchange005", nestedError); | ||
this.name = "ListAccountError"; | ||
} | ||
} | ||
|
||
export class ListCurrencyError extends ExchangeBaseError { | ||
constructor(nestedError?: Error) { | ||
super("exchange006", nestedError); | ||
this.name = "ListCurrencyError"; | ||
} | ||
} | ||
|
||
export class UnknownAccountError extends ExchangeBaseError { | ||
constructor(nestedError?: Error) { | ||
super("exchange007", nestedError); | ||
this.name = "UnknownAccountError"; | ||
} | ||
} | ||
|
||
export class PayinExtraIdError extends ExchangeBaseError { | ||
constructor(nestedError?: Error) { | ||
super("exchange010", nestedError); | ||
this.name = "PayinExtraIdError"; | ||
} | ||
} | ||
export type ExchangeSdkErrorType = ExchangeBaseError | NonceStepError | PayloadStepError | SignatureStepError | NotEnoughFunds | ListAccountError | ListCurrencyError | UnknownAccountError | PayinExtraIdError | ||
|
||
export default { | ||
ExchangeBaseError, | ||
NonceStepError, | ||
PayloadStepError, | ||
SignatureStepError, | ||
NotEnoughFunds, | ||
ListAccountError, | ||
ListCurrencyError, | ||
UnknownAccountError, | ||
PayinExtraIdError | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import ExchangeSdkError from "./ExchangeSdkError" | ||
|
||
export class DrawerClosedError extends ExchangeSdkError.ExchangeBaseError { | ||
handled: boolean; | ||
constructor(nestedError?: Error) { | ||
super("ll001", nestedError); | ||
this.name = "DrawerClosedError"; | ||
this.handled = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { IgnoredSignatureStepError, ListAccountError, ListCurrencyError, NonceStepError, NotEnoughFunds, PayinExtraIdError, PayloadStepError, SignatureStepError, UnknownAccountError } from './SwapError'; | ||
import ExchangeSdkError, { ExchangeSdkErrorType } from "./ExchangeSdkError" | ||
import { DrawerClosedError } from "./LedgerLiveError" | ||
import { FlowType } from '../sdk'; | ||
|
||
export enum StepError { | ||
NONCE = 'NonceStepError', | ||
PAYLOAD = 'PayloadStepError', | ||
SIGNATURE = 'SignatureStepError', | ||
IGNORED_SIGNATURE = 'IgnoredSignatureStepError', | ||
CHECK_FUNDS = 'CheckFundsStepError', | ||
LIST_ACCOUNT = 'ListAccountStepError', | ||
LIST_CURRENCY = 'ListCurrencyStepError', | ||
UNKNOWN_ACCOUNT = 'UnknownAccountStepError', | ||
PAYIN_EXTRA_ID = 'PayinExtraIdStepError', | ||
} | ||
|
||
export const parseError = (flowType: FlowType, err: Error, step?: StepError) => { | ||
if (err instanceof Error && err.name === "DrawerClosedError") { | ||
return new DrawerClosedError(err) | ||
} | ||
|
||
switch (flowType) { | ||
case 'generic': | ||
case 'sell': | ||
case 'card': | ||
const genericError = step && GenericErrors[step] | ||
return genericError ? new genericError(err) : err | ||
case 'swap': | ||
return step ? new SwapErrors[step](err) : err; | ||
} | ||
} | ||
type ErrorConstructor = (new (err?: Error) => ExchangeSdkErrorType) | undefined; | ||
|
||
const GenericErrors: Record<StepError, ErrorConstructor> = { | ||
[StepError.NONCE]: ExchangeSdkError.NonceStepError, | ||
[StepError.PAYLOAD]: ExchangeSdkError.PayloadStepError, | ||
[StepError.SIGNATURE]: ExchangeSdkError.SignatureStepError, | ||
[StepError.CHECK_FUNDS]: ExchangeSdkError.NotEnoughFunds, | ||
[StepError.IGNORED_SIGNATURE]: undefined, | ||
[StepError.LIST_ACCOUNT]: ExchangeSdkError.ListAccountError, | ||
[StepError.LIST_CURRENCY]: ExchangeSdkError.ListCurrencyError, | ||
[StepError.UNKNOWN_ACCOUNT]: ExchangeSdkError.UnknownAccountError, | ||
[StepError.PAYIN_EXTRA_ID]: ExchangeSdkError.PayinExtraIdError | ||
} | ||
|
||
const SwapErrors: Record<StepError, new (err?: Error) => Error | undefined> = { | ||
[StepError.NONCE]: NonceStepError, | ||
[StepError.PAYLOAD]: PayloadStepError, | ||
[StepError.SIGNATURE]: SignatureStepError, | ||
[StepError.CHECK_FUNDS]: NotEnoughFunds, | ||
[StepError.IGNORED_SIGNATURE]: IgnoredSignatureStepError, | ||
[StepError.LIST_ACCOUNT]: ListAccountError, | ||
[StepError.LIST_CURRENCY]: ListCurrencyError, | ||
[StepError.UNKNOWN_ACCOUNT]: UnknownAccountError, | ||
[StepError.PAYIN_EXTRA_ID]: PayinExtraIdError | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
export { | ||
ExchangeError, | ||
ExchangeBaseError, | ||
ListAccountError, | ||
ListCurrencyError, | ||
NonceStepError, | ||
DrawerClosedError, | ||
NotEnoughFunds, | ||
PayloadStepError, | ||
SignatureStepError, | ||
UnknownAccountError, | ||
} from "./error"; | ||
} from "./error/ExchangeSdkError"; | ||
export { DrawerClosedError } from './error/LedgerLiveError'; | ||
export { QueryParams } from "./liveapp"; | ||
export { ExchangeSDK } from "./sdk"; | ||
export type { FeeStrategy } from "./sdk"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.