Skip to content

Commit 03bc24c

Browse files
committed
refactor: enable useUnknownInCatchVariables
1 parent f7f3dc2 commit 03bc24c

File tree

9 files changed

+24
-15
lines changed

9 files changed

+24
-15
lines changed

src/AeSdkWallet.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
} from './aepp-wallet-communication/rpc/types';
2121
import { Encoded } from './utils/encoder';
2222
import jsonBig from './utils/json-big';
23+
import { ensureError } from './utils/other';
2324

2425
type RpcClientWallet = RpcClient<AeppApi, WalletApi>;
2526

@@ -259,6 +260,7 @@ export default class AeSdkWallet extends AeSdk {
259260
} catch (error) {
260261
const validation = await verifyTransaction(tx, this.api);
261262
if (validation.length > 0) throw new RpcInvalidTransactionError(validation);
263+
ensureError(error);
262264
throw new RpcBroadcastError(error.message);
263265
}
264266
},

src/chain.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AE_AMOUNT_FORMATS, formatAmount } from './utils/amount-formatter';
22
import verifyTransaction, { ValidatorResult } from './tx/validator';
3-
import { isAccountNotFoundError, pause } from './utils/other';
3+
import { ensureError, isAccountNotFoundError, pause } from './utils/other';
44
import { isNameValid, produceNameId } from './tx/builder/helpers';
55
import { DRY_RUN_ACCOUNT } from './tx/builder/schema';
66
import { AensName } from './tx/builder/constants';
@@ -208,6 +208,7 @@ export async function sendTransaction(
208208
}
209209
return { hash: txHash, rawTx: tx };
210210
} catch (error) {
211+
ensureError(error);
211212
throw Object.assign(error, {
212213
rawTx: tx,
213214
verifyTx: async () => verifyTransaction(tx, onNode),

src/channel/internal.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from '../utils/errors';
1616
import { encodeContractAddress } from '../utils/crypto';
1717
import { buildTx } from '../tx/builder';
18+
import { ensureError } from '../utils/other';
1819

1920
export interface ChannelEvents {
2021
statusChanged: (status: ChannelStatus) => void;
@@ -240,6 +241,7 @@ async function dequeueMessage(channel: Channel): Promise<void> {
240241
try {
241242
await handleMessage(channel, message);
242243
} catch (error) {
244+
ensureError(error);
243245
emit(channel, 'error', new ChannelIncomingMessageError(error, message));
244246
}
245247
}

src/contract/compiler/Cli.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import CompilerBase, { Aci } from './Base';
77
import { Encoded } from '../../utils/encoder';
88
import { CompilerError, InternalError, UnsupportedVersionError } from '../../utils/errors';
99
import semverSatisfies from '../../utils/semver-satisfies';
10+
import { ensureError } from '../../utils/other';
1011

1112
const getPackagePath = (): string => {
1213
const path = dirname(fileURLToPath(import.meta.url));
@@ -81,6 +82,7 @@ export default class CompilerCli extends CompilerBase {
8182
aci: aci as Aci,
8283
};
8384
} catch (error) {
85+
ensureError(error);
8486
throw new CompilerError(error.message);
8587
}
8688
}

src/tx/validator.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { RestError } from '@azure/core-rest-pipeline';
12
import { hash, verify } from '../utils/crypto';
23
import { TxUnpacked } from './builder/schema.generated';
34
import { CtVersion, ProtocolToVmAbi } from './builder/field-types/ct-version';
@@ -183,9 +184,9 @@ validators.push(
183184
checkedKeys: ['contractId'],
184185
}];
185186
} catch (error) {
186-
if (error.response?.parsedBody?.reason == null) throw error;
187+
if (!(error instanceof RestError) || error.response?.bodyAsText == null) throw error;
187188
return [{
188-
message: error.response.parsedBody.reason,
189+
message: JSON.parse(error.response.bodyAsText).reason, // TODO: use parsedBody instead
189190
key: 'ContractNotFound',
190191
checkedKeys: ['contractId'],
191192
}];

src/utils/autorest.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { RestError, PipelineResponse, PipelinePolicy } from '@azure/core-rest-pi
22
import { AdditionalPolicyConfig } from '@azure/core-client';
33
import { pause } from './other';
44
import semverSatisfies from './semver-satisfies';
5-
import { UnexpectedTsError, UnsupportedVersionError } from './errors';
5+
import { UnsupportedVersionError } from './errors';
66

77
export const genRequestQueuesPolicy = (): AdditionalPolicyConfig => {
88
const requestQueues = new Map<string, Promise<unknown>>();
@@ -126,22 +126,17 @@ export const genRetryOnFailurePolicy = (
126126
const intervalSum = intervals.reduce((a, b) => a + b);
127127
const intervalsInMs = intervals.map((el) => (el / intervalSum) * retryOverallDelay);
128128

129-
let error: Error | undefined;
129+
let error = new RestError('Not expected to be thrown');
130130
for (let attempt = 0; attempt <= retryCount; attempt += 1) {
131-
if (error != null) {
132-
if (
133-
!(error instanceof RestError)
134-
|| statusesToNotRetry.includes(error.response?.status ?? 0)
135-
) throw error;
136-
await pause(intervalsInMs[attempt - 1]);
137-
}
131+
if (attempt !== 0) await pause(intervalsInMs[attempt - 1]);
138132
try {
139133
return await next(request);
140134
} catch (e) {
135+
if (!(e instanceof RestError)) throw e;
136+
if (statusesToNotRetry.includes(e.response?.status ?? 0)) throw e;
141137
error = e;
142138
}
143139
}
144-
if (error == null) throw new UnexpectedTsError();
145140
throw error;
146141
},
147142
},

src/utils/errors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,11 @@ export class UnexpectedChannelMessageError extends ChannelError {
401401
* @category exception
402402
*/
403403
export class ChannelIncomingMessageError extends ChannelError {
404-
handlerError: BaseError;
404+
handlerError: Error;
405405

406406
incomingMessage: { [key: string]: any };
407407

408-
constructor(handlerError: BaseError, incomingMessage: { [key: string]: any }) {
408+
constructor(handlerError: Error, incomingMessage: { [key: string]: any }) {
409409
super(handlerError.message);
410410
this.handlerError = handlerError;
411411
this.incomingMessage = incomingMessage;

src/utils/other.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,8 @@ export function isAccountNotFoundError(error: Error): boolean {
5757
export type UnionToIntersection<Union> =
5858
(Union extends any ? (k: Union) => void : never) extends ((k: infer Intersection) => void)
5959
? Intersection : never;
60+
61+
export function ensureError(error: unknown): asserts error is Error {
62+
if (error instanceof Error) return;
63+
throw error;
64+
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"downlevelIteration": true,
2121
"strictPropertyInitialization": true,
2222
"strictNullChecks": true,
23+
"useUnknownInCatchVariables": true,
2324
"allowSyntheticDefaultImports": true,
2425
"typeRoots": [
2526
"./node_modules/@types",

0 commit comments

Comments
 (0)