|
1 | 1 | export enum ERROR {
|
2 |
| - Default = 'Something wrong', |
3 |
| - ErrInsufficientQuantity = 'orderbook: insufficient quantity to calculate price', |
4 |
| - ErrInvalidConditionalOrder = 'orderbook: Stop-Limit Order (BUY: marketPrice < stopPrice <= price, SELL: marketPrice > stopPrice >= price). Stop-Market Order (BUY: marketPrice < stopPrice, SELL: marketPrice > stopPrice). OCO order (BUY: price < marketPrice < stopPrice, SELL: price > marketPrice > stopPrice)', |
5 |
| - ErrInvalidOrderType = "orderbook: supported order type are 'limit' and 'market'", |
6 |
| - ErrInvalidPrice = 'orderbook: invalid order price', |
7 |
| - ErrInvalidPriceLevel = 'orderbook: invalid order price level', |
8 |
| - ErrInvalidPriceOrQuantity = 'orderbook: invalid order price or quantity', |
9 |
| - ErrInvalidQuantity = 'orderbook: invalid order quantity', |
10 |
| - ErrInvalidSide = "orderbook: given neither 'bid' nor 'ask'", |
11 |
| - ErrInvalidTimeInForce = "orderbook: supported time in force are 'GTC', 'IOC' and 'FOK'", |
12 |
| - ErrLimitFOKNotFillable = 'orderbook: limit FOK order not fillable', |
13 |
| - ErrOrderExists = 'orderbook: order already exists', |
14 |
| - ErrOrderNotFound = 'orderbook: order not found', |
15 |
| - ErrJournalLog = 'journal: invalid journal log format', |
| 2 | + DEFAULT = 'DEFAULT', |
| 3 | + INSUFFICIENT_QUANTITY = 'INSUFFICIENT_QUANTITY', |
| 4 | + INVALID_CONDITIONAL_ORDER = 'INVALID_CONDITIONAL_ORDER', |
| 5 | + INVALID_JOURNAL_LOG = 'INVALID_JOURNAL_LOG', |
| 6 | + INVALID_ORDER_TYPE = 'INVALID_ORDER_TYPE', |
| 7 | + INVALID_PRICE = 'INVALID_PRICE', |
| 8 | + INVALID_PRICE_LEVEL = 'INVALID_PRICE_LEVEL', |
| 9 | + INVALID_PRICE_OR_QUANTITY = 'INVALID_PRICE_OR_QUANTITY', |
| 10 | + INVALID_QUANTITY = 'INVALID_QUANTITY', |
| 11 | + INVALID_SIDE = 'INVALID_SIDE', |
| 12 | + INVALID_TIF = 'INVALID_TIF', |
| 13 | + LIMIT_ORDER_FOK_NOT_FILLABLE = 'LIMIT_ORDER_FOK_NOT_FILLABLE', |
| 14 | + LIMIT_ORDER_POST_ONLY = 'LIMIT_ORDER_POST_ONLY', |
| 15 | + ORDER_ALREDY_EXISTS = 'ORDER_ALREDY_EXISTS', |
| 16 | + ORDER_NOT_FOUND = 'ORDER_NOT_FOUND', |
16 | 17 | }
|
17 | 18 |
|
18 |
| -export const CustomError = (error?: ERROR | string): Error => { |
19 |
| - switch (error) { |
20 |
| - case ERROR.ErrInvalidQuantity: |
21 |
| - return new Error(ERROR.ErrInvalidQuantity) |
22 |
| - case ERROR.ErrInsufficientQuantity: |
23 |
| - return new Error(ERROR.ErrInsufficientQuantity) |
24 |
| - case ERROR.ErrInvalidPrice: |
25 |
| - return new Error(ERROR.ErrInvalidPrice) |
26 |
| - case ERROR.ErrInvalidPriceLevel: |
27 |
| - return new Error(ERROR.ErrInvalidPriceLevel) |
28 |
| - case ERROR.ErrInvalidPriceOrQuantity: |
29 |
| - return new Error(ERROR.ErrInvalidPriceOrQuantity) |
30 |
| - case ERROR.ErrOrderExists: |
31 |
| - return new Error(ERROR.ErrOrderExists) |
32 |
| - case ERROR.ErrOrderNotFound: |
33 |
| - return new Error(ERROR.ErrOrderNotFound) |
34 |
| - case ERROR.ErrInvalidSide: |
35 |
| - return new Error(ERROR.ErrInvalidSide) |
36 |
| - case ERROR.ErrInvalidConditionalOrder: |
37 |
| - return new Error(ERROR.ErrInvalidConditionalOrder) |
38 |
| - case ERROR.ErrInvalidOrderType: |
39 |
| - return new Error(ERROR.ErrInvalidOrderType) |
40 |
| - case ERROR.ErrInvalidTimeInForce: |
41 |
| - return new Error(ERROR.ErrInvalidTimeInForce) |
42 |
| - case ERROR.ErrLimitFOKNotFillable: |
43 |
| - return new Error(ERROR.ErrLimitFOKNotFillable) |
44 |
| - case ERROR.ErrJournalLog: |
45 |
| - return new Error(ERROR.ErrJournalLog) |
46 |
| - default: |
47 |
| - error = error === undefined || error === '' ? '' : `: ${error}` |
48 |
| - return new Error(`${ERROR.Default}${error}`) |
| 19 | +export const ErrorMessages: Record<ERROR, string> = { |
| 20 | + [ERROR.DEFAULT]: 'Something wrong', |
| 21 | + [ERROR.INSUFFICIENT_QUANTITY]: |
| 22 | + 'orderbook: insufficient quantity to calculate price', |
| 23 | + [ERROR.INVALID_CONDITIONAL_ORDER]: |
| 24 | + 'orderbook: Stop-Limit Order (BUY: marketPrice < stopPrice <= price, SELL: marketPrice > stopPrice >= price). Stop-Market Order (BUY: marketPrice < stopPrice, SELL: marketPrice > stopPrice). OCO order (BUY: price < marketPrice < stopPrice, SELL: price > marketPrice > stopPrice)', |
| 25 | + [ERROR.INVALID_ORDER_TYPE]: |
| 26 | + "orderbook: supported order type are 'limit' and 'market'", |
| 27 | + [ERROR.INVALID_PRICE]: 'orderbook: invalid order price', |
| 28 | + [ERROR.INVALID_PRICE_LEVEL]: 'orderbook: invalid order price level', |
| 29 | + [ERROR.INVALID_PRICE_OR_QUANTITY]: |
| 30 | + 'orderbook: invalid order price or quantity', |
| 31 | + [ERROR.INVALID_QUANTITY]: 'orderbook: invalid order quantity', |
| 32 | + [ERROR.INVALID_SIDE]: "orderbook: given neither 'bid' nor 'ask'", |
| 33 | + [ERROR.INVALID_TIF]: |
| 34 | + "orderbook: supported time in force are 'GTC', 'IOC' and 'FOK'", |
| 35 | + [ERROR.LIMIT_ORDER_FOK_NOT_FILLABLE]: |
| 36 | + 'orderbook: limit FOK order not fillable', |
| 37 | + [ERROR.LIMIT_ORDER_POST_ONLY]: |
| 38 | + 'orderbook: Post-only order rejected because would execute immediately', |
| 39 | + [ERROR.ORDER_ALREDY_EXISTS]: 'orderbook: order already exists', |
| 40 | + [ERROR.ORDER_NOT_FOUND]: 'orderbook: order not found', |
| 41 | + [ERROR.INVALID_JOURNAL_LOG]: 'journal: invalid journal log format' |
| 42 | +} |
| 43 | + |
| 44 | +class CustomErrorFactory extends Error { |
| 45 | + constructor (error?: ERROR | string) { |
| 46 | + let errorMessage: string |
| 47 | + if (error != null && ErrorMessages[error as ERROR] != null) { |
| 48 | + errorMessage = ErrorMessages[error as ERROR] |
| 49 | + } else { |
| 50 | + const customMessage = error === undefined || error === '' ? '' : `: ${error}` |
| 51 | + errorMessage = `${ErrorMessages.DEFAULT}${customMessage}` |
| 52 | + } |
| 53 | + super(errorMessage) |
49 | 54 | }
|
50 | 55 | }
|
| 56 | + |
| 57 | +export const CustomError = (error?: ERROR | string): Error => { |
| 58 | + return new CustomErrorFactory(error) |
| 59 | +} |
0 commit comments