Skip to content

Commit

Permalink
Update status codes for better monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoMogg committed Aug 22, 2024
1 parent dfd69a0 commit 82edaa9
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion apps/web/pages/api/proofs/baseEthHolders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}

// If error is not an instance of Error, return a generic error message
return res.status(409).json({ error: 'An unexpected error occurred' });
return res.status(500).json({ error: 'An unexpected error occurred' });
}
2 changes: 1 addition & 1 deletion apps/web/pages/api/proofs/bns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}

// If error is not an instance of Error, return a generic error message
return res.status(409).json({ error: 'An unexpected error occurred' });
return res.status(500).json({ error: 'An unexpected error occurred' });
}
14 changes: 7 additions & 7 deletions apps/web/pages/api/proofs/cb1/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { trustedSignerPKey } from 'apps/web/src/constants';
import { DiscountType, proofValidation } from 'apps/web/src/utils/proofs';
import { DiscountType, ProofsException, proofValidation } from 'apps/web/src/utils/proofs';
import { sybilResistantUsernameSigning } from 'apps/web/src/utils/proofs/sybil_resistance';
import type { NextApiRequest, NextApiResponse } from 'next';

Expand Down Expand Up @@ -52,12 +52,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
);
return res.status(200).json(result);
} catch (error) {
console.error(error);
if (error instanceof Error) {
return res.status(409).json({ error: error.message });
if (error instanceof ProofsException) {
return res.status(error.statusCode).json({ error: error.message });
}

// If error is not an instance of Error, return a generic error message
return res.status(409).json({ error: 'An unexpected error occurred' });
console.error(error);
}

// If error is not an instance of Error, return a generic error message
return res.status(500).json({ error: 'An unexpected error occurred' });
}
2 changes: 1 addition & 1 deletion apps/web/pages/api/proofs/cbid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
console.error(error);
}
// If error is not an instance of Error, return a generic error message
return res.status(409).json({ error: 'An unexpected error occurred' });
return res.status(500).json({ error: 'An unexpected error occurred' });
}
18 changes: 12 additions & 6 deletions apps/web/pages/api/proofs/coinbase/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { trustedSignerPKey } from 'apps/web/src/constants';
import { DiscountType, proofValidation, VerifiedAccount } from 'apps/web/src/utils/proofs';
import {
DiscountType,
ProofsException,
proofValidation,
VerifiedAccount,
} from 'apps/web/src/utils/proofs';
import { sybilResistantUsernameSigning } from 'apps/web/src/utils/proofs/sybil_resistance';
import type { NextApiRequest, NextApiResponse } from 'next';
import { Address } from 'viem';
Expand Down Expand Up @@ -54,11 +59,12 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return res.status(200).json(result);
} catch (error) {
console.error(error);
if (error instanceof Error) {
return res.status(409).json({ error: error.message });
if (error instanceof ProofsException) {
return res.status(error.statusCode).json({ error: error.message });
}

// If error is not an instance of Error, return a generic error message
return res.status(409).json({ error: 'An unexpected error occurred' });
console.error(error);
}

// If error is not an instance of Error, return a generic error message
return res.status(500).json({ error: 'An unexpected error occurred' });
}
2 changes: 1 addition & 1 deletion apps/web/pages/api/proofs/earlyAccess/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
console.error(error);
}
// If error is not an instance of Error, return a generic error message
return res.status(409).json({ error: 'An unexpected error occurred' });
return res.status(500).json({ error: 'An unexpected error occurred' });
}
2 changes: 1 addition & 1 deletion apps/web/src/utils/proofs/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function getWalletProofs(
const hasPreviouslyRegistered = await hasRegisteredWithDiscount([address], chain);
// if any linked address registered previously return an error
if (hasPreviouslyRegistered) {
throw new ProofsException('This address has already claimed a username.', 400);
throw new ProofsException('This address has already claimed a username.', 409);
}
const [content] = await getProofsByNamespaceAndAddress(address, namespace, caseInsensitive);
const proofs = content?.proofs ? (JSON.parse(content.proofs) as `0x${string}`[]) : [];
Expand Down
11 changes: 8 additions & 3 deletions apps/web/src/utils/proofs/sybil_resistance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
DiscountTypes,
PreviousClaim,
PreviousClaims,
ProofsException,
VerifiedAccount,
} from 'apps/web/src/utils/proofs/types';
import { REGISTER_CONTRACT_ADDRESSES } from 'apps/web/src/utils/usernames';
Expand Down Expand Up @@ -122,7 +123,7 @@ export async function sybilResistantUsernameSigning(
const discountValidatorAddress = discountTypes[chainId][discountType]?.discountValidatorAddress;

if (!discountValidatorAddress || !isAddress(discountValidatorAddress)) {
throw new Error('Must provide a valid discountValidatorAddress');
throw new ProofsException('Must provide a valid discountValidatorAddress', 500);
}

const attestations = await getAttestations(
Expand All @@ -144,7 +145,7 @@ export async function sybilResistantUsernameSigning(
const hasPreviouslyRegistered = await hasRegisteredWithDiscount(linkedAddresses, chainId);
// if any linked address registered previously return an error
if (hasPreviouslyRegistered) {
throw new Error('You have already claimed a discounted basename (onchain).');
throw new ProofsException('You have already claimed a discounted basename (onchain).', 409);
}

const kvKey = `${previousClaimsKVPrefix}${idemKey}`;
Expand All @@ -153,8 +154,9 @@ export async function sybilResistantUsernameSigning(
const previousClaim = previousClaims[discountType];
if (previousClaim) {
if (previousClaim.address != address) {
throw new Error(
throw new ProofsException(
'You tried claiming this with a different address, wait a couple minutes to try again.',
400,
);
}
// return previously signed message
Expand Down Expand Up @@ -185,6 +187,9 @@ export async function sybilResistantUsernameSigning(
};
} catch (error) {
console.error(error);
if (error instanceof Error) {
throw new ProofsException(error.message, 500);
}
throw error;
}
}

0 comments on commit 82edaa9

Please sign in to comment.