Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #810 from hermeznetwork/include-some-unmanaged-err…
Browse files Browse the repository at this point in the history
…ors-and-avoid-reporting-denied-signature-errors

Include some unmanaged errors and avoid reporting denied signature errors
  • Loading branch information
amonsosanz authored Feb 1, 2022
2 parents 710d99f + 75cc3a5 commit 9233e99
Show file tree
Hide file tree
Showing 33 changed files with 515 additions and 472 deletions.
70 changes: 70 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"redux-devtools-extension": "^2.13.8",
"redux-thunk": "^2.3.0",
"serve": "^11.3.2",
"stacktrace-js": "^2.0.2",
"typechain": "^6.0.5",
"typescript": "^4.3.5",
"workbox-cacheable-response": "^6.2.4",
Expand Down
6 changes: 3 additions & 3 deletions src/adapters/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function getEnv(): Either<Env, ZodError<Env>> {
if (parsedEnvLiteral.data.REACT_APP_ENV === "production") {
const parsedProductionEnv = productionEnvParser.safeParse(process.env);
if (!parsedProductionEnv.success) {
adapters.logDecodingError(
adapters.errors.logDecodingError(
parsedProductionEnv.error,
"Could not decode the production env from the function getEnv."
);
Expand All @@ -74,15 +74,15 @@ export function getEnv(): Either<Env, ZodError<Env>> {
} else {
const parsedDevelopmentEnv = developmentEnvParser.safeParse(process.env);
if (!parsedDevelopmentEnv.success) {
adapters.logDecodingError(
adapters.errors.logDecodingError(
parsedDevelopmentEnv.error,
"Could not decode the development env from the function getEnv."
);
}
return parsedDevelopmentEnv;
}
} else {
adapters.logDecodingError(
adapters.errors.logDecodingError(
parsedEnvLiteral.error,
"Could not decode the var REACT_APP_ENV from the function getEnv."
);
Expand Down
90 changes: 90 additions & 0 deletions src/adapters/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { z, ZodError } from "zod";
import * as StackTrace from "stacktrace-js";
import axios from "axios";

import { StrictSchema } from "src/utils/type-safety";

// Error decoding and message extraction helper
interface MessageKeyError {
message: string;
}

const messageKeyErrorParser = StrictSchema<MessageKeyError>()(
z.object({
message: z.string(),
})
);

export interface MetamaskUserRejectedRequestError {
code: 4001;
message: string;
}

export const metamaskUserRejectedRequestError = StrictSchema<MetamaskUserRejectedRequestError>()(
z.object({
code: z.literal(4001),
message: z.string(),
})
);

function sanitizeErrorMessage(errorMessage: string): string {
try {
return JSON.stringify(JSON.parse(errorMessage));
} catch (error) {
const selectMultipleTabsAndSpaces = /[^\S\r\n]{2,}/g;
return errorMessage.replaceAll(selectMultipleTabsAndSpaces, " ");
}
}

export function parseError(error: unknown): Promise<string> {
const unknownError = Promise.resolve(`An unknown error has occurred: ${JSON.stringify(error)}`);
if (typeof error === "string") {
return Promise.resolve(error);
} else if (error instanceof Error) {
const maxErrorLength = 4096;
return StackTrace.fromError(error)
.then((stackframes) =>
[
sanitizeErrorMessage(error.message),
">>>>>>>>>> Stringification >>>>>>>>>>",
JSON.stringify(error),
">>>>>>>>>> Stack >>>>>>>>>>",
...stackframes.map((sf) => sf.toString()),
]
.join("\n")
.substring(0, maxErrorLength)
)
.catch((e) => {
console.error(e);
return unknownError;
});
} else {
const parsedMessageKeyError = messageKeyErrorParser.safeParse(error);
if (parsedMessageKeyError.success) {
return Promise.resolve(parsedMessageKeyError.data.message);
} else {
console.error(error);
return unknownError;
}
}
}

export function logDecodingError<T>(error: ZodError<T>, details: string): void {
error.errors.forEach((issue) => {
switch (issue.code) {
case "invalid_union": {
issue.unionErrors.forEach((e) => logDecodingError(e, details));
break;
}
default: {
console.error(`A decoding error occurred: ${details}`);
console.error(JSON.stringify(issue, null, 4));
break;
}
}
});
}

export function isAxiosCancelRequestError(error: unknown): boolean {
return axios.isCancel(error);
}
Loading

0 comments on commit 9233e99

Please sign in to comment.