Handle error to send to sentry.

This commit is contained in:
2025-04-26 22:40:22 +07:00
parent 084977c717
commit 3c044a1e29
4 changed files with 54 additions and 121 deletions

View File

@@ -21,7 +21,7 @@ public class PositionTests : BaseTests
"test",
MoneyManagement,
TradeDirection.Long,
Ticker.BTC,
Ticker.ETH,
PositionInitiator.User,
DateTime.UtcNow,
_account.User,

View File

@@ -26,6 +26,7 @@ import {DecreasePositionAmounts} from '../../generated/gmxsdk/types/trade.js';
import {encodeReferralCode} from '../../generated/gmxsdk/utils/referrals.js';
import {formatUsd} from '../../generated/gmxsdk/utils/numbers/formatting.js';
import {calculateDisplayDecimals} from '../../generated/gmxsdk/utils/numbers/index.js';
import {handleError} from '../../utils/errorHandler.js';
/**
* GMX Plugin
@@ -162,7 +163,6 @@ export const openGmxPositionImpl = async (
allowedSlippageBps: 100, // 1% slippage
leverage: leverageBps,
skipSimulation: true,
limitPrice: limitPrice,
referralCodeForTxn: encodeReferralCode("kaigen_ai"),
stopLossPrice: stopLossPrice ? numberToBigint(stopLossPrice, 30) : undefined,
takeProfitPrice: takeProfitPrice ? numberToBigint(takeProfitPrice, 30) : undefined
@@ -247,14 +247,7 @@ export async function openGmxPosition(
hash
};
} catch (error) {
this.log.error(error);
// Return appropriate error response
reply.status(error instanceof z.ZodError ? 400 : 500);
return {
success: false,
error: error instanceof Error ? error.message : 'An unknown error occurred'
};
return handleError(this, reply, error, 'gmx/open-position');
}
}
@@ -345,14 +338,7 @@ export async function cancelGmxOrders(
success
};
} catch (error) {
this.log.error(error);
// Return appropriate error response
reply.status(error instanceof z.ZodError ? 400 : 500);
return {
success: false,
error: error instanceof Error ? error.message : 'An unknown error occurred'
};
return handleError(this, reply, error, 'gmx/cancel-orders');
}
}
@@ -524,14 +510,7 @@ export async function closeGmxPosition(
hash
};
} catch (error) {
this.log.error(error);
// Return appropriate error response
reply.status(500);
return {
success: false,
error: error instanceof Error ? error.message : 'An unknown error occurred'
};
return handleError(this, reply, error, 'gmx/close-position');
}
}
@@ -650,14 +629,7 @@ export async function getGmxTrade(
trades
};
} catch (error) {
this.log.error(error);
// Return appropriate error response
reply.status(500);
return {
success: false,
error: error instanceof Error ? error.message : 'An unknown error occurred'
};
return handleError(this, reply, error, 'gmx/trades');
}
}
@@ -807,12 +779,7 @@ export async function getGmxPositions(
positions,
};
} catch (error) {
this.log.error(error);
reply.status(500);
return {
success: false,
error: error instanceof Error ? error.message : 'An unknown error occurred',
};
return handleError(this, reply, error, 'gmx/positions');
}
}

View File

@@ -1,7 +1,6 @@
import {FastifyPluginAsyncTypebox} from '@fastify/type-provider-typebox'
import {Type} from '@sinclair/typebox'
import { TradeDirection } from '../../../generated/ManagingApiTypes'
import { handleError } from '../../../utils/errorHandler.js'
import {TradeDirection} from '../../../generated/ManagingApiTypes'
const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
// Define route to open a position
@@ -29,25 +28,19 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
}, async (request, reply) => {
const { account, tradeType, ticker, direction, price, quantity, leverage, stopLossPrice, takeProfitPrice } = request.body
try {
// Call the openPosition method from the GMX plugin
const result = await request.openGmxPosition(
reply,
account,
tradeType,
ticker,
direction as TradeDirection,
price,
quantity,
leverage,
stopLossPrice,
takeProfitPrice
)
return result
} catch (error) {
return handleError(request, reply, error, 'gmx/open-position');
}
// Call the openPosition method from the GMX plugin
return request.openGmxPosition(
reply,
account,
tradeType,
ticker,
direction as TradeDirection,
price,
quantity,
leverage,
stopLossPrice,
takeProfitPrice
)
})
// Define route to cancel orders
@@ -67,18 +60,12 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
}, async (request, reply) => {
const { account, ticker } = request.body
try {
// Call the cancelGmxOrders method from the GMX plugin
const result = await request.cancelGmxOrders(
reply,
account,
ticker,
)
return result
} catch (error) {
return handleError(request, reply, error, 'gmx/cancel-orders');
}
// Call the cancelGmxOrders method from the GMX plugin
return request.cancelGmxOrders(
reply,
account,
ticker,
)
})
// Define route to close a position
@@ -100,19 +87,13 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
}, async (request, reply) => {
const { account, ticker, direction } = request.body
try {
// Call the closePosition method from the GMX plugin
const result = await request.closeGmxPosition(
reply,
account,
ticker,
direction as TradeDirection
);
return result;
} catch (error) {
return handleError(request, reply, error, 'gmx/close-position');
}
// Call the closePosition method from the GMX plugin
return request.closeGmxPosition(
reply,
account,
ticker,
direction as TradeDirection
)
})
// Define route to get a trade
@@ -133,20 +114,16 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
}, async (request, reply) => {
const { account, ticker } = request.query
try {
// Call the getGmxTrade method from the GMX plugin
const result = await request.getGmxTrade(
reply,
account,
ticker
)
// Call the getGmxTrade method from the GMX plugin
const result = await request.getGmxTrade(
reply,
account,
ticker
)
console.log('result', result)
return result
} catch (error) {
return handleError(request, reply, error, 'gmx/trades');
}
console.log('result', result)
return result
})
// Define route to get positions
@@ -166,17 +143,11 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
}, async (request, reply) => {
const { account } = request.query
try {
// Call the getGmxPositions method from the GMX plugin
const result = await request.getGmxPositions(
reply,
account
)
return result
} catch (error) {
return handleError(request, reply, error, 'gmx/positions');
}
// Call the getGmxPositions method from the GMX plugin
return request.getGmxPositions(
reply,
account
)
})
}

View File

@@ -9,7 +9,7 @@ import type {
Time,
UTCTimestamp,
} from 'lightweight-charts'
import {LineStyle, createChart, CrosshairMode} from 'lightweight-charts'
import {createChart, CrosshairMode, LineStyle} from 'lightweight-charts'
import moment from 'moment'
import * as React from 'react'
import {useEffect, useRef, useState} from 'react'
@@ -22,10 +22,7 @@ import type {
StrategiesResultBase,
StrategyType,
} from '../../../../generated/ManagingApi'
import {
PositionStatus,
TradeDirection,
} from '../../../../generated/ManagingApi'
import {PositionStatus, TradeDirection,} from '../../../../generated/ManagingApi'
import useTheme from '../../../../hooks/useTheme'
// var customTheme = {
@@ -143,8 +140,8 @@ const TradeChart = ({
let color = 'mintcream'
if (position == undefined) return color
const negativeColor = 'palevioletred'
const positiveColor = 'lightgreen'
const negativeColor = theme.error
const positiveColor = theme.success
const status = position.status
const realized = position.profitAndLoss?.realized ?? 0
@@ -158,13 +155,11 @@ const TradeChart = ({
} else {
color = negativeColor
}
}else if (status == PositionStatus.Filled) {
color = theme.warning
}
}
if (position.profitAndLoss?.realized == null) {
color = 'yellow'
}
return color
}