Skip to content

Commit

Permalink
[Relay] Update error message of EVM
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Sep 15, 2023
1 parent 5d87c56 commit b36749c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 41 deletions.
50 changes: 25 additions & 25 deletions packages/relay/src/routers/DefaultRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export class DefaultRouter {
}

private async getHealthStatus(req: express.Request, res: express.Response) {
return res.json("OK");
return res.status(200).json("OK");
}

/**
Expand All @@ -211,10 +211,9 @@ export class DefaultRouter {
private async payMileage(req: express.Request, res: express.Response) {
logger.http(`POST /payMileage`);

// TODO 필요시 access secret 검사
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.json(
return res.status(200).json(
this.makeResponseData(501, undefined, {
message: "Failed to check the validity of parameters.",
validation: errors.array(),
Expand All @@ -241,7 +240,7 @@ export class DefaultRouter {
})
);

// 이메일 EmailLinkerContract에 이메일 등록여부 체크 및 구매자 주소와 동일여부
// 컨트랙트에서 이메일 등록여부 체크 및 구매자 주소와 동일여부
const emailToAddress: string = await (await this.getEmailLinkerContract()).toAddress(email);
if (emailToAddress !== signer) {
return res.status(200).json(
Expand All @@ -255,11 +254,12 @@ export class DefaultRouter {
.payMileage(purchaseId, amount, email, franchiseeId, signer, signature);

logger.http(`TxHash(payMileage): `, tx.hash);
return res.json(this.makeResponseData(200, { txHash: tx.hash }));
return res.status(200).json(this.makeResponseData(200, { txHash: tx.hash }));
} catch (error: any) {
const message = error.message !== undefined ? error.message : "Failed pay mileage";
let message = ContractUtils.cacheEVMError(error as any);
if (message === "") message = "Failed pay mileage";
logger.error(`POST /payMileage :`, message);
return res.json(
return res.status(200).json(
this.makeResponseData(500, undefined, {
message,
})
Expand All @@ -275,10 +275,9 @@ export class DefaultRouter {
private async payToken(req: express.Request, res: express.Response) {
logger.http(`POST /payToken`);

// TODO 필요시 access secret 검사
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.json(
return res.status(200).json(
this.makeResponseData(501, undefined, {
message: "Failed to check the validity of parameters.",
validation: errors.array(),
Expand All @@ -304,7 +303,7 @@ export class DefaultRouter {
})
);

// 이메일 EmailLinkerContract에 이메일 등록여부 체크 및 구매자 주소와 동일여부
// 컨트랙트에서 이메일 등록여부 체크 및 구매자 주소와 동일여부
const emailToAddress: string = await (await this.getEmailLinkerContract()).toAddress(email);
if (emailToAddress !== signer) {
return res.status(200).json(
Expand All @@ -318,11 +317,12 @@ export class DefaultRouter {
.payToken(purchaseId, amount, email, franchiseeId, signer, signature);

logger.http(`TxHash(payToken): `, tx.hash);
return res.json(this.makeResponseData(200, { txHash: tx.hash }));
return res.status(200).json(this.makeResponseData(200, { txHash: tx.hash }));
} catch (error: any) {
const message = error.message !== undefined ? error.message : "Failed pay token";
let message = ContractUtils.cacheEVMError(error as any);
if (message === "") message = "Failed pay token";
logger.error(`POST /payToken :`, message);
return res.json(
return res.status(200).json(
this.makeResponseData(500, undefined, {
message,
})
Expand All @@ -338,10 +338,9 @@ export class DefaultRouter {
private async exchangeTokenToMileage(req: express.Request, res: express.Response) {
logger.http(`POST /exchangeTokenToMileage`);

// TODO 필요시 access secret 검사
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.json(
return res.status(200).json(
this.makeResponseData(501, undefined, {
message: "Failed to check the validity of parameters.",
validation: errors.array(),
Expand All @@ -365,7 +364,7 @@ export class DefaultRouter {
})
);

// 이메일 EmailLinkerContract에 이메일 등록여부 체크 및 구매자 주소와 동일여부
// 컨트랙트에서 이메일 등록여부 체크 및 구매자 주소와 동일여부
const emailToAddress: string = await (await this.getEmailLinkerContract()).toAddress(email);
if (emailToAddress !== signer) {
return res.status(200).json(
Expand All @@ -379,11 +378,12 @@ export class DefaultRouter {
.exchangeTokenToMileage(email, amountToken, signer, signature);

logger.http(`TxHash(exchangeTokenToMileage): `, tx.hash);
return res.json(this.makeResponseData(200, { txHash: tx.hash }));
return res.status(200).json(this.makeResponseData(200, { txHash: tx.hash }));
} catch (error: any) {
const message = error.message !== undefined ? error.message : "Failed exchange Token To Mileage";
let message = ContractUtils.cacheEVMError(error as any);
if (message === "") message = "Failed exchange Token To Mileage";
logger.error(`POST /exchangeTokenToMileage :`, message);
return res.json(
return res.status(200).json(
this.makeResponseData(500, undefined, {
message,
})
Expand All @@ -399,10 +399,9 @@ export class DefaultRouter {
private async exchangeMileageToToken(req: express.Request, res: express.Response) {
logger.http(`POST /exchangeMileageToToken`);

// TODO 필요시 access secret 검사
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.json(
return res.status(200).json(
this.makeResponseData(501, undefined, {
message: "Failed to check the validity of parameters.",
validation: errors.array(),
Expand All @@ -426,7 +425,7 @@ export class DefaultRouter {
})
);

// 이메일 EmailLinkerContract에 이메일 등록여부 체크 및 구매자 주소와 동일여부
// 컨트랙트에서 이메일 등록여부 체크 및 구매자 주소와 동일여부
const emailToAddress: string = await (await this.getEmailLinkerContract()).toAddress(email);
if (emailToAddress !== signer) {
return res.status(200).json(
Expand All @@ -440,11 +439,12 @@ export class DefaultRouter {
.exchangeMileageToToken(email, amountMileage, signer, signature);

logger.http(`TxHash(exchangeMileageToToken): `, tx.hash);
return res.json(this.makeResponseData(200, { txHash: tx.hash }));
return res.status(200).json(this.makeResponseData(200, { txHash: tx.hash }));
} catch (error: any) {
const message = error.message !== undefined ? error.message : "Failed exchange Mileage To Token";
let message = ContractUtils.cacheEVMError(error as any);
if (message === "") message = "Failed exchange Mileage To Token";
logger.error(`POST /exchangeMileageToToken :`, message);
return res.json(
return res.status(200).json(
this.makeResponseData(500, undefined, {
message,
})
Expand Down
20 changes: 20 additions & 0 deletions packages/relay/src/utils/ContractUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ export class ContractUtils {
return "0x" + data.toString("hex");
}

private static find_message = "reverted with reason string";
private static find_length = ContractUtils.find_message.length;
public static cacheEVMError(error: any): string {
if (error instanceof Error) {
const idx = error.message.indexOf(ContractUtils.find_message);
const message =
idx >= 0
? error.message
.substring(idx + ContractUtils.find_length)
.replace(/['|"]/gi, "")
.trim()
: error.message;
return message;
} else if (error.message) {
return error.message;
} else {
return error.toString();
}
}

public static getRequestId(emailHash: string, address: string, nonce: BigNumberish): string {
const encodedResult = hre.ethers.utils.defaultAbiCoder.encode(
["bytes32", "address", "uint256", "bytes32"],
Expand Down
20 changes: 4 additions & 16 deletions packages/relay/test/Endpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,7 @@ describe("Test of Server", function () {
signature,
});
assert.deepStrictEqual(response.data.code, 500);
assert.ok(
response.data.error.message ===
"VM Exception while processing transaction: reverted with reason string 'Insufficient balance'"
);
assert.ok(response.data.error.message === "Insufficient balance");
});

it("Failure Exchange token to mileage", async () => {
Expand Down Expand Up @@ -341,10 +338,7 @@ describe("Test of Server", function () {
signature,
});
assert.deepStrictEqual(response.data.code, 500);
assert.ok(
response.data.error.message ===
"VM Exception while processing transaction: reverted with reason string 'Insufficient balance'"
);
assert.ok(response.data.error.message === "Insufficient balance");
});

it("Failure Exchange mileage to token", async () => {
Expand Down Expand Up @@ -415,10 +409,7 @@ describe("Test of Server", function () {
});

assert.deepStrictEqual(response.data.code, 500);
assert.ok(
response.data.error.message ===
"VM Exception while processing transaction: reverted with reason string 'Insufficient balance'"
);
assert.ok(response.data.error.message === "Insufficient balance");
});

it("Failure test of the path /payMileage 'Email is not valid.'", async () => {
Expand Down Expand Up @@ -549,10 +540,7 @@ describe("Test of Server", function () {
});

assert.deepStrictEqual(response.data.code, 500);
assert.ok(
response.data.error.message ===
"VM Exception while processing transaction: reverted with reason string 'Insufficient balance'"
);
assert.ok(response.data.error.message === "Insufficient balance");
});

it("Failure test of the path /payToken 'Email is not valid.'", async () => {
Expand Down

0 comments on commit b36749c

Please sign in to comment.