Skip to content

Commit

Permalink
fix(Error): return errorCode in sdkError case
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKozAllB committed Jul 22, 2024
1 parent 66b7dc8 commit 3595b2b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 31 deletions.
2 changes: 1 addition & 1 deletion rest-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"scripts": {
"build": "tsoa spec && nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"format": "prettier --write \"src/**/*.ts\"",
"start": "nest start",
"start:dev": "tsoa spec && nest start --watch",
"start:debug": "nest start --debug --watch",
Expand Down
61 changes: 31 additions & 30 deletions rest-api/src/controller/rest.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
SolanaTxFeeParamsMethod,
SwapCalcInfo,
} from '../service/sdk.service';
import { httpException } from '../error/errors';

type RawTransaction =
| VersionedTransaction
Expand All @@ -54,7 +55,7 @@ export class RestController {
try {
return this.sdkService.chainDetailsMap();
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand All @@ -68,7 +69,7 @@ export class RestController {
try {
return this.sdkService.getTokens();
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand Down Expand Up @@ -103,7 +104,7 @@ export class RestController {
amount: amount,
});
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand Down Expand Up @@ -212,7 +213,7 @@ export class RestController {
try {
return await this.sdkService.send(params);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand Down Expand Up @@ -395,7 +396,7 @@ export class RestController {
try {
return await this.sdkService.send(sendParams);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand All @@ -415,7 +416,7 @@ export class RestController {
sender,
);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand Down Expand Up @@ -459,7 +460,7 @@ export class RestController {
messengerEnum,
);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand All @@ -480,7 +481,7 @@ export class RestController {
try {
return await this.sdkService.getTransferStatus(chainEnum, txId);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand All @@ -506,7 +507,7 @@ export class RestController {
}),
};
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand All @@ -530,7 +531,7 @@ export class RestController {
chainSymbol: chainSymbol,
});
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand All @@ -551,7 +552,7 @@ export class RestController {
address,
);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand Down Expand Up @@ -595,7 +596,7 @@ export class RestController {
messengerEnum,
);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand All @@ -616,7 +617,7 @@ export class RestController {
try {
return await this.sdkService.getGasBalance(chainSymbol, address);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand Down Expand Up @@ -648,7 +649,7 @@ export class RestController {
destinationTokenObj,
);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand Down Expand Up @@ -686,7 +687,7 @@ export class RestController {
destinationTokenObj,
);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand Down Expand Up @@ -730,7 +731,7 @@ export class RestController {
destinationTokenObj,
);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand Down Expand Up @@ -774,7 +775,7 @@ export class RestController {
destinationTokenObj,
);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand Down Expand Up @@ -837,7 +838,7 @@ export class RestController {
feeFloat,
);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand Down Expand Up @@ -900,7 +901,7 @@ export class RestController {
feeFloat,
);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand Down Expand Up @@ -975,7 +976,7 @@ export class RestController {
try {
return await this.sdkService.deposit(params);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand Down Expand Up @@ -1050,7 +1051,7 @@ export class RestController {
try {
return await this.sdkService.withdraw(params);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand Down Expand Up @@ -1120,7 +1121,7 @@ export class RestController {
try {
return await this.sdkService.claimRewards(params);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand All @@ -1137,7 +1138,7 @@ export class RestController {
try {
return await this.sdkService.checkBalanceLine(address, token);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand Down Expand Up @@ -1184,7 +1185,7 @@ export class RestController {
gasFeePaymentMethod: feePaymentMethodEnum,
});
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand All @@ -1207,7 +1208,7 @@ export class RestController {
try {
return await this.sdkService.getPoolInfoFromServer(poolAddressObj);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand All @@ -1230,7 +1231,7 @@ export class RestController {
try {
return await this.sdkService.getPoolInfoFromBlockchain(poolAddressObj);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand Down Expand Up @@ -1267,7 +1268,7 @@ export class RestController {
gasFeePaymentMethod: feePaymentMethodEnum,
});
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand All @@ -1294,7 +1295,7 @@ export class RestController {
poolAddressObj,
);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand Down Expand Up @@ -1325,7 +1326,7 @@ export class RestController {
tokenAddressObj,
);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}

Expand Down Expand Up @@ -1362,7 +1363,7 @@ export class RestController {
tokenAddressObj,
);
} catch (e) {
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
httpException(e);
}
}
}
17 changes: 17 additions & 0 deletions rest-api/src/error/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/*eslint @typescript-eslint/no-useless-constructor: "off"*/

import { HttpException, HttpStatus } from '@nestjs/common';
import { SdkRootError } from '@allbridge/bridge-core-sdk';

export abstract class RestSDKError extends Error {
protected constructor(message?: string) {
super(message);
Expand All @@ -11,3 +14,17 @@ export class ConfigError extends RestSDKError {
super(message);
}
}

export const httpException = (e) => {
if (e instanceof SdkRootError) {
throw new HttpException(
{
statusCode: HttpStatus.BAD_REQUEST,
errorCode: e.errorCode,
message: e.message,
},
HttpStatus.BAD_REQUEST,
);
}
throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
};

0 comments on commit 3595b2b

Please sign in to comment.