Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Sentry error logging readability #3677

Merged
merged 11 commits into from
Jul 12, 2023
9 changes: 6 additions & 3 deletions src/composables/swap/useSor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ import { isMainnet } from '../useNetwork';
import { useTokens } from '@/providers/tokens.provider';
import useTransactions, { TransactionAction } from '../useTransactions';
import { SwapQuote } from './types';
import { captureException } from '@sentry/browser';
import { overflowProtected } from '@/components/_global/BalTextInput/helpers';
import { isUserError } from '../useTransactionErrors';
import { captureBalancerException } from '@/lib/utils/errors';

type SorState = {
validationErrors: {
Expand Down Expand Up @@ -805,13 +805,16 @@ export default function useSor({
if (!isUserError(error)) {
console.trace(error);
state.submissionError = t('swapException', ['Balancer']);
captureException(new Error(state.submissionError, { cause: error }), {
level: 'fatal',

captureBalancerException(error, 'swap', state.submissionError, {
extra: {
sender: account.value,
tokenIn,
tokenOut,
},
tags: {
swapType: 'balancer',
},
});
}
swapping.value = false;
Expand Down
80 changes: 80 additions & 0 deletions src/lib/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import debounce from 'lodash/debounce';
import { captureException } from '@sentry/browser';
import { ScopeContext } from '@sentry/types/types/scope';

type BalancerExceptionAction = 'swap' | 'joinPool' | 'exitPool';

export const captureBalancerException = debounce(
_captureBalancerException,
1000
);

function _captureBalancerException(
error: Error,
action: BalancerExceptionAction,
messagePrefix: string,
captureContext?: Partial<ScopeContext>
): void {
const { reason, balError } = getReasonAndBalErrorFromError(error);
const tags: { [key: string]: string } = { ...captureContext?.tags, action };

if (balError) {
tags.balError = balError;
}

captureException(
getErrorForAction(
action,
`${balError ? `BAL#${balError} ` : ''}${messagePrefix}: ${reason}`,
error
),
{
...captureContext,
extra: {
...captureContext?.extra,
reason,
balError,
},
tags,
}
);
}

function getReasonAndBalErrorFromError(error: Error): {
reason: string;
balError: string | null;
} {
const reason: string =
(error as { reason?: string }).reason || 'no reason available';
const balError = reason.match(/BAL#[0-9]{3}/g);

return {
reason: reason,
balError: balError && balError[0] ? balError[0].slice(-3) : null,
};
}

function getErrorForAction(
action: BalancerExceptionAction,
message: string,
originalError: Error
) {
switch (action) {
case 'swap':
return new BatchSwapError(message, { cause: originalError });
case 'joinPool':
return new JoinPoolError(message, { cause: originalError });
case 'exitPool':
return new ExitPoolError(message, { cause: originalError });
}
}

class BatchSwapError extends Error {
name = 'BatchSwapError';
}
class JoinPoolError extends Error {
name = 'JoinPoolError';
}
class ExitPoolError extends Error {
name = 'ExitPoolError';
}
1 change: 1 addition & 0 deletions src/services/contracts/vault.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ describe('vault.service', () => {
expect.any(Number), // expect.any(Number) refers to the deadline from calculateValidTo(transactionDeadline)
],
options: {},
shouldLogFailure: false,
});
});
});
Expand Down
1 change: 1 addition & 0 deletions src/services/contracts/vault.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default class VaultService {
action: 'batchSwap',
params: [swapKind, swaps, tokenAddresses, funds, limits, deadline],
options,
shouldLogFailure: false,
});
}

Expand Down
4 changes: 3 additions & 1 deletion src/services/web3/transactions/concerns/contract.concern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type SendTransactionOpts = {
action: string;
params?: any[];
options?: TransactionRequest;
shouldLogFailure?: boolean;
};

export class ContractConcern extends TransactionConcern {
Expand All @@ -36,6 +37,7 @@ export class ContractConcern extends TransactionConcern {
action,
params = [],
options = {},
shouldLogFailure = true,
}: SendTransactionOpts): Promise<TransactionResponse> {
const EthersContract = getEthersContract();
const contractWithSigner = new EthersContract(
Expand Down Expand Up @@ -69,7 +71,7 @@ export class ContractConcern extends TransactionConcern {
} catch (err) {
const error = err as WalletError;

if (this.shouldLogFailure(error)) {
if (shouldLogFailure && this.shouldLogFailure(error)) {
await this.logFailedTx(
error,
contractWithSigner,
Expand Down