-
Notifications
You must be signed in to change notification settings - Fork 5
chore: utils errors refactor #38
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
base: main
Are you sure you want to change the base?
Conversation
…atch them 1:1 to existing errros
… move constants to dedicated file, and separate types onto another dedicated file
src/utils/errors/constants.ts
Outdated
|
|
||
| export const STRING_PREFIX_AFTER_UNICODE_REPLACEMENT = 'y %'; | ||
|
|
||
| // eslint-disable-next-line no-useless-escape |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this really needed?
src/utils/errors/error.ts
Outdated
| const validAscii = (str: string) => { | ||
| return str.replace( | ||
| INVALID_ASCII_REGEX, | ||
| '', | ||
| ); | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| const validAscii = (str: string) => { | |
| return str.replace( | |
| INVALID_ASCII_REGEX, | |
| '', | |
| ); | |
| }; | |
| const validAscii = (str: string) => str.replace(INVALID_ASCII_REGEX, ''); |
mattgle
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few comments, not really a MUST, but improvements IMO
src/utils/errors/error.ts
Outdated
| const isRailgunError = (cause: Error): boolean => cause.message.toLowerCase().includes('railgunsmartwallet') | ||
|
|
||
| export const sanitizeError = (cause: Error): Error => { | ||
| if (isDefined(cause) && cause.message) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, since the "else" here is shorter, early return would make the logic easier to read, eg.
if (cause?.message == null) return new Error('Unknown error. Please try again.', { cause });
if (isRailgunError(cause)) { /* ... */ }Another question is, do we even need to check if cause is defined and has a message property? The type here says it must be an Error, so we perhaps only need to check the message being defined
src/utils/errors/constants.ts
Outdated
| export const STRING_PREFIX_AFTER_UNICODE_REPLACEMENT = 'y %'; | ||
|
|
||
| // eslint-disable-next-line no-useless-escape | ||
| export const INVALID_ASCII_REGEX = /[^A-Za-z 0-9 \.,\?""!@#\$%\^&\*\(\)-_=\+;:<>\/\\\|\}\{\[\]`~]*/g; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A neat little trick (that begs a comment), but all the printable ascii characters are in the range from (space) to ~ (tilde), so the regex can be compressed to: /[^ -~]+/g. Is this better? I think so (with a comment explaining what it is) since it ensures no printable ascii chars were missed, despite being more obscure knowledge, where a list like the above requires you to check that all symbols were included.
| @@ -0,0 +1,92 @@ | |||
| import { CustomErrorMapping } from "./types"; | |||
|
|
|||
| export const STRING_PREFIX_AFTER_UNICODE_REPLACEMENT = 'y %'; | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we know what this means? Looking at the usage, I don't understand how the name of the constant here makes sense (I know you just migrated it from what it historically was)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
given the case that we keep it, a different name would be better IMHO, SANITIZED_MESSAGE_SEPARATOR or similar
but now that I've mentioned, I think we could remove this and be shortened to something that looks like:
const errorMessage = sanitizeAscii(cause.message);
since we're already properly sanitizing ASCII, and we don't need that much string manipulation besides this right??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no idea tbh, but @mesquka might know
src/utils/errors/error.ts
Outdated
| import { STRING_PREFIX_AFTER_UNICODE_REPLACEMENT, RAILGUN_ERRORS, CUSTOM_ERRORS, INVALID_ASCII_REGEX } from './constants'; | ||
| import { isDefined } from '../util'; | ||
|
|
||
| const validAscii = (str: string) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that we are refactoring, perhaps we should give this a more fitting name, perhaps sanitizeAscii
src/utils/index.ts
Outdated
| export * from './compare'; | ||
| export * from './fallback-provider'; | ||
| export * from './error'; | ||
| export * from './errors/error'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this is an "internal module" so changing the path is insignificant, but if something is importing the error formatter directly, this is a breaking change
…e inside cause since its type Error
src/utils/errors/constants.ts
Outdated
|
|
||
| // Matches any characters that are NOT in the printable ASCII range (space to tilde) | ||
| // Printable ASCII characters are in the range of 32 (space) to 126 (tilde) | ||
| export const INVALID_ASCII_REGEX = /[^ -~]+/g; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking a bit more about this regex, we also remove LF (ie \n). It was not in the original regex, but I wonder if we actually would want to keep new lines. If so, we can add it to the range as \n
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets keep it as it was before then 👍🏼
src/utils/index.ts
Outdated
| export * from './compare'; | ||
| export * from './fallback-provider'; | ||
| export * from './error'; | ||
| export * from './errors'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean to call this error per my comment about a potential breaking change?
|
What should we do with this? |
What
Current is a stab to refactor existing
utils/errors.ts,Why
In an effort to make the sdk more readable, this provides an enhancement for legibility of the current errors file, without the need of hardcoding existing/new errors and make it more extendable for future releases.
How