Skip to content
This repository has been archived by the owner on May 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #81 from connext/31-return-404-for-not-found
Browse files Browse the repository at this point in the history
404 error for Hashlock Transfer not found
  • Loading branch information
Rahul Sethuram authored Aug 18, 2020
2 parents 39fc50c + 3d1ebe2 commit f827d58
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 64 deletions.
2 changes: 1 addition & 1 deletion examples/6-hashlock-transfer.http
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Content-Type: application/json

###

# Create status of corresponding hashlock transfer
# Get status of corresponding hashlock transfer
GET http://localhost:5040/hashlock-status/0x7c0023168317e7010af1e8f5d583d2990bd0f7141f2068a9269ca92d9128c95a/0x0000000000000000000000000000000000000000/indra53QJ6exujj3pQQ8BPuTDfQqBXvpyzkz8uS4fwbNMtMZKQZWuUS

###
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function verifyType(value: any, type: string): boolean {
}
}

export async function requireParam(obj: any, param: string, type = "string"): Promise<void> {
export function requireParam(obj: any, param: string, type = "string"): void {
if (typeof obj[param] === "undefined" || !verifyType(obj[param], type)) {
throw new Error(`Invalid or missing ${param}`);
}
Expand Down
128 changes: 66 additions & 62 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ app.after(() => {
Routes.get.fundingBalance.opts,
async (req, res) => {
try {
await requireParam(req.params, "assetId");
requireParam(req.params, "assetId");

res
.status(200)
Expand All @@ -145,9 +145,9 @@ app.after(() => {

app.get<GetBalanceRequest>(Routes.get.balance.url, Routes.get.balance.opts, async (req, res) => {
try {
await requireParam(req.params, "assetId");
requireParam(req.params, "assetId");
if (!config.legacyMode) {
await requireParam(req.params, "publicIdentifier");
requireParam(req.params, "publicIdentifier");
}
let client: Client | undefined;
try {
Expand Down Expand Up @@ -191,7 +191,7 @@ app.after(() => {
app.get<GetConfigRequest>(Routes.get.config.url, Routes.get.config.opts, async (req, res) => {
try {
if (!config.legacyMode) {
await requireParam(req.params, "publicIdentifier");
requireParam(req.params, "publicIdentifier");
}
const client = multiClient.getClient(req.params.publicIdentifier);
res.status(200).send<RouteMethods.GetConfigResponse>(client.getConfig());
Expand All @@ -210,11 +210,11 @@ app.after(() => {
Routes.get.hashLockStatus.opts,
async (req, res) => {
try {
await requireParam(req.params, "lockHash");
await requireParam(req.params, "assetId");
requireParam(req.params, "lockHash");
requireParam(req.params, "assetId");
const { lockHash, assetId } = req.params;
if (!config.legacyMode) {
await requireParam(req.params, "publicIdentifier");
requireParam(req.params, "publicIdentifier");
}
const client = multiClient.getClient(req.params.publicIdentifier);
res
Expand All @@ -224,7 +224,11 @@ app.after(() => {
);
} catch (error) {
app.log.error(error);
res.status(500).send<GenericErrorResponse>({ message: error.message });
let statusCode = 500;
if ((error.message as string).includes(`No HashLock Transfer found for lockHash`)) {
statusCode = 404;
}
res.status(statusCode).send<GenericErrorResponse>({ message: error.message });
}
},
);
Expand All @@ -238,10 +242,10 @@ app.after(() => {
Routes.get.linkedStatus.opts,
async (req, res) => {
try {
await requireParam(req.params, "paymentId");
requireParam(req.params, "paymentId");
const { paymentId } = req.params;
if (!config.legacyMode) {
await requireParam(req.params, "publicIdentifier");
requireParam(req.params, "publicIdentifier");
}
const client = multiClient.getClient(req.params.publicIdentifier);
res
Expand All @@ -263,9 +267,9 @@ app.after(() => {
Routes.get.appinstanceDetails.opts,
async (req, res) => {
try {
await requireParam(req.params, "appIdentityHash");
requireParam(req.params, "appIdentityHash");
if (!config.legacyMode) {
await requireParam(req.params, "publicIdentifier");
requireParam(req.params, "publicIdentifier");
}
const client = multiClient.getClient(req.params.publicIdentifier);
res
Expand All @@ -290,7 +294,7 @@ app.after(() => {
async (req, res) => {
try {
if (!config.legacyMode) {
await requireParam(req.params, "publicIdentifier");
requireParam(req.params, "publicIdentifier");
}
const client = multiClient.getClient(req.params.publicIdentifier);
res
Expand All @@ -311,7 +315,7 @@ app.after(() => {

app.post<PostCreateRequest>(Routes.post.create.url, Routes.post.create.opts, async (req, res) => {
try {
await requireParam(req.body, "index", "number");
requireParam(req.body, "index", "number");
res
.status(200)
.send<RouteMethods.PostCreateResponse>(
Expand All @@ -332,7 +336,7 @@ app.after(() => {
Routes.post.mnemonic.opts,
async (req, res) => {
try {
await requireParam(req.body, "mnemonic");
requireParam(req.body, "mnemonic");
if (multiClient.keyring.mnemonic !== req.body.mnemonic) {
await multiClient.reset();
}
Expand All @@ -354,11 +358,11 @@ app.after(() => {
Routes.post.onchainTransfer.opts,
async (req, res) => {
try {
await requireParam(req.body, "amount");
await requireParam(req.body, "assetId");
await requireParam(req.body, "recipient");
requireParam(req.body, "amount");
requireParam(req.body, "assetId");
requireParam(req.body, "recipient");
if (!config.legacyMode) {
await requireParam(req.body, "publicIdentifier");
requireParam(req.body, "publicIdentifier");
}
res
.status(200)
Expand All @@ -380,7 +384,7 @@ app.after(() => {
async (req, res) => {
try {
if (!config.legacyMode) {
await requireParam(req.body, "publicIdentifier");
requireParam(req.body, "publicIdentifier");
}
const client = await multiClient.connectClient(req.body);
res.status(200).send<RouteMethods.GetConfigResponse>(await client.getConfig());
Expand All @@ -401,7 +405,7 @@ app.after(() => {
async (req, res) => {
try {
if (!config.legacyMode) {
await requireParam(req.body, "publicIdentifier");
requireParam(req.body, "publicIdentifier");
}
await multiClient.disconnectClient(req.body?.publicIdentifier);
res.status(200).send<GenericSuccessResponse>({ success: true });
Expand All @@ -421,13 +425,13 @@ app.after(() => {
Routes.post.hashLockTransfer.opts,
async (req, res) => {
try {
await requireParam(req.body, "amount");
await requireParam(req.body, "assetId");
await requireParam(req.body, "lockHash");
await requireParam(req.body, "timelock");
await requireParam(req.body, "recipient");
requireParam(req.body, "amount");
requireParam(req.body, "assetId");
requireParam(req.body, "lockHash");
requireParam(req.body, "timelock");
requireParam(req.body, "recipient");
if (!config.legacyMode) {
await requireParam(req.body, "publicIdentifier");
requireParam(req.body, "publicIdentifier");
}
const client = multiClient.getClient(req.body.publicIdentifier);
res
Expand All @@ -449,10 +453,10 @@ app.after(() => {
Routes.post.hashLockResolve.opts,
async (req, res) => {
try {
await requireParam(req.body, "preImage");
await requireParam(req.body, "assetId");
requireParam(req.body, "preImage");
requireParam(req.body, "assetId");
if (!config.legacyMode) {
await requireParam(req.body, "publicIdentifier");
requireParam(req.body, "publicIdentifier");
}
const client = multiClient.getClient(req.body.publicIdentifier);
res
Expand All @@ -474,10 +478,10 @@ app.after(() => {
Routes.post.linkedTransfer.opts,
async (req, res) => {
try {
await requireParam(req.body, "assetId");
await requireParam(req.body, "amount");
requireParam(req.body, "assetId");
requireParam(req.body, "amount");
if (!config.legacyMode) {
await requireParam(req.body, "publicIdentifier");
requireParam(req.body, "publicIdentifier");
}
const client = multiClient.getClient(req.body.publicIdentifier);
res
Expand All @@ -499,10 +503,10 @@ app.after(() => {
Routes.post.linkedResolve.opts,
async (req, res) => {
try {
await requireParam(req.body, "preImage");
await requireParam(req.body, "paymentId");
requireParam(req.body, "preImage");
requireParam(req.body, "paymentId");
if (!config.legacyMode) {
await requireParam(req.body, "publicIdentifier");
requireParam(req.body, "publicIdentifier");
}
const client = multiClient.getClient(req.body.publicIdentifier);
res
Expand All @@ -520,10 +524,10 @@ app.after(() => {

app.post<PostFundRequest>(Routes.post.fund.url, Routes.post.fund.opts, async (req, res) => {
try {
await requireParam(req.body, "amount");
await requireParam(req.body, "assetId");
requireParam(req.body, "amount");
requireParam(req.body, "assetId");
if (!config.legacyMode) {
await requireParam(req.body, "publicIdentifier");
requireParam(req.body, "publicIdentifier");
}
const client = multiClient.getClient(req.body.publicIdentifier);
res
Expand All @@ -546,10 +550,10 @@ app.after(() => {
Routes.post.deposit.opts,
async (req, res) => {
try {
await requireParam(req.body, "amount");
await requireParam(req.body, "assetId");
requireParam(req.body, "amount");
requireParam(req.body, "assetId");
if (!config.legacyMode) {
await requireParam(req.body, "publicIdentifier");
requireParam(req.body, "publicIdentifier");
}
const client = multiClient.getClient(req.body.publicIdentifier);
res.status(200).send<RouteMethods.PostDepositResponse>(await client.deposit(req.body));
Expand All @@ -569,9 +573,9 @@ app.after(() => {
{ ...Routes.post.requestCollateral.opts, preHandler: app.auth([app.verifyApiKey]) },
async (req, res) => {
try {
await requireParam(req.body, "assetId");
requireParam(req.body, "assetId");
if (!config.legacyMode) {
await requireParam(req.body, "publicIdentifier");
requireParam(req.body, "publicIdentifier");
}
const client = multiClient.getClient(req.body.publicIdentifier);
await client.requestCollateral(req.body);
Expand All @@ -589,12 +593,12 @@ app.after(() => {

app.post<PostSwapRequest>(Routes.post.swap.url, Routes.post.swap.opts, async (req, res) => {
try {
await requireParam(req.body, "amount");
await requireParam(req.body, "fromAssetId");
await requireParam(req.body, "swapRate");
await requireParam(req.body, "toAssetId");
requireParam(req.body, "amount");
requireParam(req.body, "fromAssetId");
requireParam(req.body, "swapRate");
requireParam(req.body, "toAssetId");
if (!config.legacyMode) {
await requireParam(req.body, "publicIdentifier");
requireParam(req.body, "publicIdentifier");
}
const client = multiClient.getClient(req.body.publicIdentifier);
res.status(200).send<RouteMethods.PostSwapResponse>(await client.swap(req.body));
Expand All @@ -613,10 +617,10 @@ app.after(() => {
Routes.post.withdraw.opts,
async (req, res) => {
try {
await requireParam(req.body, "amount");
await requireParam(req.body, "assetId");
requireParam(req.body, "amount");
requireParam(req.body, "assetId");
if (!config.legacyMode) {
await requireParam(req.body, "publicIdentifier");
requireParam(req.body, "publicIdentifier");
}
const client = multiClient.getClient(req.body.publicIdentifier);
res.status(200).send<RouteMethods.PostWithdrawResponse>(await client.withdraw(req.body));
Expand Down Expand Up @@ -662,10 +666,10 @@ app.after(() => {
Routes.post.subscribe.opts,
async (req, res) => {
try {
await requireParam(req.body, "event");
await requireParam(req.body, "webhook");
requireParam(req.body, "event");
requireParam(req.body, "webhook");
if (!config.legacyMode) {
await requireParam(req.body, "publicIdentifier");
requireParam(req.body, "publicIdentifier");
}
const client = multiClient.getClient(req.body.publicIdentifier);
res.status(200).send<SubscriptionResponse>(await client.subscribe(req.body));
Expand All @@ -688,9 +692,9 @@ app.after(() => {
Routes.post.batchSubscribe.opts,
async (req, res) => {
try {
await requireParam(req.body, "params", "array");
requireParam(req.body, "params", "array");
if (!config.legacyMode) {
await requireParam(req.body, "publicIdentifier");
requireParam(req.body, "publicIdentifier");
}
const client = multiClient.getClient(req.body.publicIdentifier);
res
Expand All @@ -717,9 +721,9 @@ app.after(() => {
Routes.delete.subscribe.opts,
async (req, res) => {
try {
await requireParam(req.body, "id");
requireParam(req.body, "id");
if (!config.legacyMode) {
await requireParam(req.body, "publicIdentifier");
requireParam(req.body, "publicIdentifier");
}
const client = multiClient.getClient(req.body.publicIdentifier);
res.status(200).send<GenericSuccessResponse>(await client.unsubscribe(req.body.id));
Expand All @@ -742,9 +746,9 @@ app.after(() => {
Routes.delete.batchSubscribe.opts,
async (req, res) => {
try {
await requireParam(req.body, "ids", "array");
requireParam(req.body, "ids", "array");
if (!config.legacyMode) {
await requireParam(req.body, "publicIdentifier");
requireParam(req.body, "publicIdentifier");
}
const client = multiClient.getClient(req.body.publicIdentifier);
res.status(200).send<GenericSuccessResponse>(await client.unsubscribeBatch(req.body.ids));
Expand All @@ -767,7 +771,7 @@ app.after(() => {
async (req, res) => {
try {
if (!config.legacyMode) {
await requireParam(req.body, "publicIdentifier");
requireParam(req.body, "publicIdentifier");
}
const client = multiClient.getClient(req.body.publicIdentifier);
res.status(200).send<GenericSuccessResponse>(await client.unsubscribeAll());
Expand Down

0 comments on commit f827d58

Please sign in to comment.