-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: centralize error mapping and add shared error notice #120
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,24 @@ | ||
| // Shared inline error banner: a consistent, styled notice (alert icon + tinted surface) for the | ||
| // ad-hoc red `<p className="error">` text that used to sit bare across pages and modals. Renders | ||
| // nothing when there's no message, so callers can pass a nullable value directly. `role="alert"` | ||
| // so screen readers announce it when it appears. | ||
|
|
||
| export function ErrorNotice({ | ||
| message, | ||
| className | ||
| }: { | ||
| message?: string | null | ||
| className?: string | ||
| }) { | ||
| if (!message) return null | ||
| return ( | ||
| <p className={className ? `error-notice ${className}` : 'error-notice'} role="alert"> | ||
| <svg className="error-notice__ico" viewBox="0 0 24 24" fill="currentColor" aria-hidden> | ||
| <path d="M12 2 1 21h22L12 2zm0 6a1 1 0 0 1 1 1v5a1 1 0 1 1-2 0V9a1 1 0 0 1 1-1zm0 8.25a1.25 1.25 0 1 1 0 2.5 1.25 1.25 0 0 1 0-2.5z" /> | ||
| </svg> | ||
| <span className="error-notice__msg">{message}</span> | ||
| </p> | ||
| ) | ||
| } | ||
|
|
||
| export default ErrorNotice | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,42 @@ | ||
| import { t } from '~/intl/i18n' | ||
| import { CURRENCY } from '~/lib/currency' | ||
|
|
||
| // Central, safe mapping from a thrown error to a localized, user-facing string. The golden rule: | ||
| // NEVER surface raw backend/exception text to the buyer (it's unpredictable, untranslated, and can | ||
| // leak internals) — every path returns a curated `t()` message or the caller's own `fallback`. | ||
|
|
||
| type ErrLike = { code?: number; status?: number; message?: string; name?: string } | ||
|
|
||
| /** User bailed out: wallet rejection (EIP-1193 4001), an aborted fetch, or a reject/deny/cancel message. */ | ||
| export function isRejection(e: unknown): boolean { | ||
| const err = e as ErrLike | ||
| return err.code === 4001 || err.name === 'AbortError' || /reject|denied|cancel/i.test(err.message ?? '') | ||
| } | ||
|
|
||
| /** | ||
| * A "not enough credits" failure (server 402 / "insufficient"). Purchase flows treat this as a normal | ||
| * top-up prompt (route to the pack picker) rather than an error state, so it's exposed separately. | ||
| */ | ||
| export function isInsufficient(e: unknown): boolean { | ||
| const err = e as ErrLike | ||
| return err.code === 402 || err.status === 402 || (err.message ?? '').toLowerCase().includes('insufficient') | ||
| } | ||
|
|
||
| /** | ||
| * Map a thrown error to a safe, localized string for display. | ||
| * - Wallet/abort rejection is handled universally. | ||
| * - Purchase flows pass `sale: true` to also map funds/availability failures (insufficient credits, | ||
| * sold/removed item, own listing) to their curated messages. | ||
| * - Anything unrecognized returns `fallback` — a context-specific generic the caller supplies | ||
| * (e.g. "Couldn't list your item…" vs "Couldn't complete checkout…"), never the raw error. | ||
| */ | ||
| export function friendlyError(e: unknown, fallback: string, opts: { sale?: boolean } = {}): string { | ||
| if (isRejection(e)) return t('errors.rejected') | ||
| if (opts.sale) { | ||
| const msg = ((e as ErrLike).message ?? '').toLowerCase() | ||
| if (msg.includes('insufficient')) return t('errors.insufficient', { currency: CURRENCY.name }) | ||
| if (/not for sale|not found|no active listing|404/.test(msg)) return t('errors.soldOrRemoved') | ||
| if (msg.includes('your own listing')) return t('errors.cantBuyOwn') | ||
| } | ||
| return fallback | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
[P2] All 11 consumers use the named import
{ ErrorNotice }— this default export is unused. Consider removing it to keep a single canonical import path.