This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #810 from hermeznetwork/include-some-unmanaged-err…
…ors-and-avoid-reporting-denied-signature-errors Include some unmanaged errors and avoid reporting denied signature errors
- Loading branch information
Showing
33 changed files
with
515 additions
and
472 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.