Skip to content

Commit e975de7

Browse files
committed
fix: dedupe flipPosition trace
1 parent fb7f852 commit e975de7

2 files changed

Lines changed: 8 additions & 67 deletions

File tree

app/components/UI/Perps/hooks/usePerpsFlipPosition.test.ts

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ describe('usePerpsFlipPosition', () => {
199199
expect(mockOnError).toHaveBeenCalledWith('perps.errors.unknown');
200200
});
201201

202-
it('handles exceptions and logs via Logger.error', async () => {
202+
it('surfaces exception via toast and onError without double-reporting to Sentry', async () => {
203203
const testError = new Error('Network error');
204204
mockFlipPosition.mockRejectedValue(testError);
205205
const mockOnError = jest.fn();
@@ -212,31 +212,13 @@ describe('usePerpsFlipPosition', () => {
212212
await result.current.handleFlipPosition(mockLongPosition);
213213
});
214214

215-
expect(Logger.error).toHaveBeenCalledWith(
216-
testError,
217-
expect.objectContaining({
218-
tags: expect.objectContaining({
219-
feature: 'perps',
220-
component: 'usePerpsFlipPosition',
221-
action: 'flip_position',
222-
operation: 'position_management',
223-
}),
224-
context: expect.objectContaining({
225-
name: 'usePerpsFlipPosition',
226-
data: expect.objectContaining({
227-
symbol: 'ETH',
228-
size: '2.5',
229-
currentDirection: 'long',
230-
targetDirection: 'short',
231-
positionSize: 2.5,
232-
}),
233-
}),
234-
}),
235-
);
215+
// Sentry reporting is handled at the controller layer; the UI hook must not duplicate it
216+
expect(Logger.error).not.toHaveBeenCalled();
217+
expect(mockShowToast).toHaveBeenCalled();
236218
expect(mockOnError).toHaveBeenCalledWith('Network error');
237219
});
238220

239-
it('handles non-Error exceptions', async () => {
221+
it('handles non-Error exceptions with fallback message without double-reporting to Sentry', async () => {
240222
mockFlipPosition.mockRejectedValue('String error');
241223
const mockOnError = jest.fn();
242224

@@ -248,19 +230,9 @@ describe('usePerpsFlipPosition', () => {
248230
await result.current.handleFlipPosition(mockLongPosition);
249231
});
250232

251-
const [loggedError, loggerContext] = (Logger.error as jest.Mock).mock
252-
.calls[0];
253-
expect(loggedError).toBeInstanceOf(Error);
254-
expect((loggedError as Error).message).toBe('String error');
255-
expect(loggerContext).toEqual(
256-
expect.objectContaining({
257-
context: expect.objectContaining({
258-
data: expect.objectContaining({
259-
rawError: 'String error',
260-
}),
261-
}),
262-
}),
263-
);
233+
// Sentry reporting is handled at the controller layer; the UI hook must not duplicate it
234+
expect(Logger.error).not.toHaveBeenCalled();
235+
expect(mockShowToast).toHaveBeenCalled();
264236
expect(mockOnError).toHaveBeenCalledWith('perps.errors.unknown');
265237
});
266238

app/components/UI/Perps/hooks/usePerpsFlipPosition.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { useCallback, useState } from 'react';
22
import { strings } from '../../../../../locales/i18n';
33
import { DevLogger } from '../../../../core/SDKConnect/utils/DevLogger';
4-
import Logger from '../../../../util/Logger';
5-
import { ensureError } from '../../../../util/errorUtils';
64
import { usePerpsTrading } from './usePerpsTrading';
75
import {
86
getPerpsDisplaySymbol,
9-
PERPS_CONSTANTS,
107
type Position,
118
type OrderDirection,
129
} from '@metamask/perps-controller';
@@ -81,34 +78,6 @@ export function usePerpsFlipPosition(options?: UsePerpsFlipPositionOptions) {
8178
} catch (error) {
8279
DevLogger.log('Error flipping position:', error);
8380

84-
Logger.error(ensureError(error, 'usePerpsFlipPosition.handle'), {
85-
tags: {
86-
feature: PERPS_CONSTANTS.FeatureName,
87-
component: 'usePerpsFlipPosition',
88-
action: 'flip_position',
89-
operation: 'position_management',
90-
},
91-
context: {
92-
name: 'usePerpsFlipPosition',
93-
data: {
94-
symbol: position.symbol,
95-
size: position.size,
96-
currentDirection,
97-
targetDirection: oppositeDirection,
98-
positionSize,
99-
entryPrice: position.entryPrice,
100-
unrealizedPnl: position.unrealizedPnl,
101-
leverage: position.leverage,
102-
rawError:
103-
error instanceof Error
104-
? undefined
105-
: error === undefined
106-
? 'undefined'
107-
: String(error),
108-
},
109-
},
110-
});
111-
11281
const errorMessage =
11382
error instanceof Error
11483
? error.message

0 commit comments

Comments
 (0)