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

View File

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

View File

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