-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vue-demo-store): api error resolver (#463)
* feat: add api errors resolver * feat: add api errors resolver * feat: add api errors resolver * feat: add api errors resolver * feat: add api errors resolver --------- Co-authored-by: patzick <13100280+patzick@users.noreply.github.com>
- Loading branch information
1 parent
57e99f1
commit 543a8e1
Showing
9 changed files
with
150 additions
and
14 deletions.
There are no files selected for viewing
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,11 @@ | ||
--- | ||
"vue-demo-store": minor | ||
--- | ||
|
||
Add api error resolver for: | ||
|
||
- Login page | ||
- Account registration | ||
- Update product qty (cart) | ||
|
||
Add `useApiErrorsResolver` composable |
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,5 @@ | ||
--- | ||
"@shopware-pwa/types": minor | ||
--- | ||
|
||
Add Meta type for the global Error type |
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
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
64 changes: 64 additions & 0 deletions
64
templates/vue-demo-store/composables/useApiErrorsResolver.ts
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,64 @@ | ||
import type { ApiError } from "@shopware/api-client"; | ||
|
||
type ContextErrors = { | ||
[key: string]: { | ||
[key: string]: string; | ||
}; | ||
}; | ||
|
||
export type UseApiErrorsResolver = { | ||
resolveApiErrors(errors: ApiError[]): string[]; | ||
}; | ||
|
||
/** | ||
* List of the unsupported/broken backend errors | ||
*/ | ||
const contextErrors: ContextErrors = { | ||
account_login: { | ||
"0": "login_no_matching_customer_internal", | ||
}, | ||
}; | ||
|
||
export function useApiErrorsResolver(context?: string): UseApiErrorsResolver { | ||
const { $i18n } = useNuxtApp(); | ||
const { t, te } = $i18n; | ||
|
||
const resolveApiErrors = (errors: ApiError[]) => { | ||
const errorsTable = errors.map(({ detail, code, meta }) => { | ||
/** | ||
* In some cases, parameters errors | ||
* comes with additional special characters. | ||
* We have to remove them | ||
* | ||
* Example: | ||
* "meta": { | ||
* "parameters": { | ||
* "{{ email }}": "<value>>" | ||
* } | ||
* } | ||
*/ | ||
if (meta?.parameters) { | ||
const pureMeta: { [key: string]: string } = {}; | ||
for (const [key, value] of Object.entries(meta?.parameters)) { | ||
pureMeta[key.replace(/[^a-zA-Z0-9 ]/g, "")] = value; | ||
} | ||
meta.parameters = pureMeta; | ||
} | ||
|
||
if (code && te(`errors.${code}`)) { | ||
return t(`errors.${code}`, { ...meta?.parameters }); | ||
} | ||
if (context && code && contextErrors[context][code]) { | ||
return t(`errors.${contextErrors[context][code]}`); | ||
} | ||
|
||
return detail || "No details provided"; | ||
}); | ||
|
||
return errorsTable; | ||
}; | ||
|
||
return { | ||
resolveApiErrors, | ||
}; | ||
} |
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
543a8e1
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.
Successfully deployed to the following URLs:
shopware-frontends-docs – ./apps/docs/
shopware-frontends-docs-shopware-frontends.vercel.app
shopware-frontends-docs.vercel.app
shopware-frontends-docs-git-main-shopware-frontends.vercel.app
frontends.shopware.com
543a8e1
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.
Successfully deployed to the following URLs:
frontends-demo – ./templates/vue-demo-store
frontends-demo-git-main-shopware-frontends.vercel.app
frontends-demo-shopware-frontends.vercel.app
frontends-demo.vercel.app