Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new error handling #110

Merged
merged 20 commits into from
Aug 31, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: cleanup and refactor
  • Loading branch information
gjj committed Aug 19, 2020
commit ce8da421f00214129732ba456a343eaacada3ae3
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ const contractNotFound = (address: Hash): Reason => {
message: `Contract ${address} was not found`,
};
};

const contractAddressInvalid = (address: Hash): Reason => {
return {
code: OpenAttestationEthereumDocumentStoreStatusCode.CONTRACT_ADDRESS_INVALID,
@@ -19,6 +20,7 @@ const contractAddressInvalid = (address: Hash): Reason => {
message: `Contract address ${address} is invalid`,
};
};

export const contractNotIssued = (merkleRoot: Hash, address: string): Reason => {
return {
code: OpenAttestationEthereumDocumentStoreStatusCode.DOCUMENT_NOT_ISSUED,
@@ -40,16 +42,15 @@ export const contractRevoked = (merkleRoot: string, address: string): Reason =>
};

/**
* This function handles all non-200 HTTP response codes (e.g. Infura/Cloudflare rate limits, Cloudflare's random 502)
* This function handles all non-200 HTTP response codes (e.g. Infura/Cloudflare 429 rate limits, Cloudflare's random 502)
* @param address the document store address
* TODO: Add the same for tokenStore
*/
export const badResponse = (): Reason => {
return {
code: OpenAttestationEthereumDocumentStoreStatusCode.BAD_RESPONSE,
codeString:
OpenAttestationEthereumDocumentStoreStatusCode[OpenAttestationEthereumDocumentStoreStatusCode.BAD_RESPONSE],
message: `Unable to connect to Ethereum, please try again later`,
message: `Unable to connect to the Ethereum network, please try again later`,
};
};

Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import { DocumentStoreFactory } from "@govtechsg/document-store";
import { DocumentStore } from "@govtechsg/document-store/src/contracts/DocumentStore";
import { Hash, VerificationFragmentType, VerificationFragment, Verifier } from "../../types/core";
import { OpenAttestationEthereumDocumentStoreStatusCode } from "../../types/error";
import { contractNotIssued, getErrorReason, contractRevoked } from "../../common/smartContract/documentStoreErrors";
import { contractNotIssued, getErrorReason, contractRevoked } from "./errors";
import { getIssuersDocumentStore, getProvider } from "../../common/utils";

interface IssuanceStatus {
18 changes: 18 additions & 0 deletions src/verifiers/tokenRegistryStatus/errors.ts
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ const contractNotFound = (address: Hash): Reason => {
message: `Contract ${address} was not found`,
};
};

const contractAddressInvalid = (address: Hash): Reason => {
return {
code: OpenAttestationEthereumTokenRegistryStatusCode.CONTRACT_ADDRESS_INVALID,
@@ -20,6 +21,7 @@ const contractAddressInvalid = (address: Hash): Reason => {
message: `Contract address ${address} is invalid`,
};
};

export const contractNotMinted = (merkleRoot: Hash, address: string): Reason => {
return {
code: OpenAttestationEthereumTokenRegistryStatusCode.DOCUMENT_NOT_MINTED,
@@ -31,6 +33,19 @@ export const contractNotMinted = (merkleRoot: Hash, address: string): Reason =>
};
};

/**
* This function handles all non-200 HTTP response codes (e.g. Infura/Cloudflare 429 rate limits, Cloudflare's random 502)
* @param address the token store address
*/
export const badResponse = (): Reason => {
return {
code: OpenAttestationEthereumTokenRegistryStatusCode.BAD_RESPONSE,
codeString:
OpenAttestationEthereumTokenRegistryStatusCode[OpenAttestationEthereumTokenRegistryStatusCode.BAD_RESPONSE],
message: `Unable to connect to the Ethereum network, please try again later`,
};
};

export const getErrorReason = (error: EthersError, address: string, hash: Hash): Reason => {
const reason = error.reason && Array.isArray(error.reason) ? error.reason[0] : error.reason ?? "";
if (
@@ -50,7 +65,10 @@ export const getErrorReason = (error: EthersError, address: string, hash: Hash):
(reason.toLowerCase() === "invalid address".toLowerCase() && error.code === errors.INVALID_ARGUMENT)
) {
return contractAddressInvalid(address);
} else if (reason.toLowerCase() === "bad response".toLowerCase()) {
return badResponse();
}

return {
message: `Error with smart contract ${address}: ${error.reason}`,
code: OpenAttestationEthereumTokenRegistryStatusCode.ETHERS_UNHANDLED_ERROR,