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
Show file tree
Hide file tree
Changes from 16 commits
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
217 changes: 180 additions & 37 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@govtechsg/token-registry": "^2.0.0",
"debug": "^4.1.1",
"ethers": "^5.0.4",
"msw": "^0.20.5",
Nebulis marked this conversation as resolved.
Show resolved Hide resolved
"snyk": "^1.364.2"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions src/types/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export enum OpenAttestationEthereumDocumentStoreStatusCode {
ETHERS_UNHANDLED_ERROR = 3,
SKIPPED = 4,
DOCUMENT_REVOKED = 5,
INVALID_ARGUMENT = 6,
CONTRACT_NOT_FOUND = 404,
SERVER_ERROR = 500,
}
export enum OpenAttestationDocumentSignedCode {
UNEXPECTED_ERROR = 0,
Expand All @@ -21,7 +23,9 @@ export enum OpenAttestationEthereumTokenRegistryStatusCode {
CONTRACT_ADDRESS_INVALID = 2,
ETHERS_UNHANDLED_ERROR = 3,
SKIPPED = 4,
INVALID_ARGUMENT = 6,
CONTRACT_NOT_FOUND = 404,
SERVER_ERROR = 500,
}
export enum OpenAttestationDnsTxtCode {
UNEXPECTED_ERROR = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -39,6 +41,27 @@ export const contractRevoked = (merkleRoot: string, address: string): Reason =>
};
};

// This function handles ALL of Ethers SERVER_ERRORs, most likely caused by HTTP 4xx or 5xx errors.
export const serverError = (): Reason => {
return {
code: OpenAttestationEthereumDocumentStoreStatusCode.SERVER_ERROR,
codeString:
OpenAttestationEthereumDocumentStoreStatusCode[OpenAttestationEthereumDocumentStoreStatusCode.SERVER_ERROR],
message: `Unable to connect to the Ethereum network, please try again later`,
};
};

// This function handles all INVALID_ARGUMENT errors likely due to invalid hex string,
// hex data is odd-length or incorrect data length
export const invalidArgument = (error: EthersError, address: string): Reason => {
return {
code: OpenAttestationEthereumDocumentStoreStatusCode.INVALID_ARGUMENT,
codeString:
OpenAttestationEthereumDocumentStoreStatusCode[OpenAttestationEthereumDocumentStoreStatusCode.INVALID_ARGUMENT],
message: `Error with smart contract ${address}: ${error.reason}`,
};
};

export const getErrorReason = (error: EthersError, address: string): Reason | null => {
const reason = error.reason && Array.isArray(error.reason) ? error.reason[0] : error.reason ?? "";
if (
Expand All @@ -55,7 +78,12 @@ export const getErrorReason = (error: EthersError, address: string): Reason | nu
(reason.toLowerCase() === "invalid address".toLowerCase() && error.code === errors.INVALID_ARGUMENT)
) {
return contractAddressInvalid(address);
} else if (error.code === errors.SERVER_ERROR) {
return serverError();
} else if (error.code === errors.INVALID_ARGUMENT) {
return invalidArgument(error, address);
}

return {
message: `Error with smart contract ${address}: ${error.reason}`,
code: OpenAttestationEthereumDocumentStoreStatusCode.ETHERS_UNHANDLED_ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions src/verifiers/tokenRegistryStatus/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -31,6 +33,27 @@ export const contractNotMinted = (merkleRoot: Hash, address: string): Reason =>
};
};

// This function handles ALL of Ethers SERVER_ERRORs, most likely caused by HTTP 4xx or 5xx errors.
export const serverError = (): Reason => {
return {
code: OpenAttestationEthereumTokenRegistryStatusCode.SERVER_ERROR,
codeString:
OpenAttestationEthereumTokenRegistryStatusCode[OpenAttestationEthereumTokenRegistryStatusCode.SERVER_ERROR],
message: `Unable to connect to the Ethereum network, please try again later`,
};
};

// This function handles all INVALID_ARGUMENT errors likely due to invalid hex string,
// hex data is odd-length or incorrect data length
export const invalidArgument = (error: EthersError, address: string): Reason => {
return {
code: OpenAttestationEthereumTokenRegistryStatusCode.INVALID_ARGUMENT,
codeString:
OpenAttestationEthereumTokenRegistryStatusCode[OpenAttestationEthereumTokenRegistryStatusCode.INVALID_ARGUMENT],
message: `Error with smart contract ${address}: ${error.reason}`,
};
};

export const getErrorReason = (error: EthersError, address: string, hash: Hash): Reason => {
const reason = error.reason && Array.isArray(error.reason) ? error.reason[0] : error.reason ?? "";
if (
Expand All @@ -50,7 +73,12 @@ export const getErrorReason = (error: EthersError, address: string, hash: Hash):
(reason.toLowerCase() === "invalid address".toLowerCase() && error.code === errors.INVALID_ARGUMENT)
) {
return contractAddressInvalid(address);
} else if (error.code === errors.SERVER_ERROR) {
return serverError();
} else if (error.code === errors.INVALID_ARGUMENT) {
return invalidArgument(error, address);
}

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