Skip to content

Commit 3d17514

Browse files
committed
feat(notification): add error handling component
1 parent 30db673 commit 3d17514

File tree

16 files changed

+203
-4
lines changed

16 files changed

+203
-4
lines changed

app/src/components/Notification/detailRender.tsx

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,86 @@
11
import type { CustomRenderArgs } from '@uozi-admin/curd'
2+
import type { PropType } from 'vue'
3+
import type { CosyError } from '@/lib/http/types'
4+
import { defineComponent, ref } from 'vue'
25
import { NotificationTypeT } from '@/constants'
6+
import { translateError } from '@/lib/http/error'
37
import notifications from './notifications'
48

9+
// Helper function to parse and translate error
10+
async function parseError(response: string): Promise<string | null> {
11+
try {
12+
const errorData = JSON.parse(response) as CosyError
13+
if (errorData.scope && errorData.code) {
14+
return await translateError(errorData)
15+
}
16+
}
17+
catch (error) {
18+
console.error('Failed to parse error response:', error)
19+
}
20+
return null
21+
}
22+
23+
// Create a component for error details to properly handle async translation
24+
const ErrorDetails = defineComponent({
25+
props: {
26+
response: {
27+
type: [String, Object] as PropType<string | object>,
28+
required: true,
29+
},
30+
},
31+
setup(props) {
32+
const translatedError = ref<string>('')
33+
const isLoading = ref(true)
34+
35+
// Convert response to string if it's an object
36+
const responseString = typeof props.response === 'string'
37+
? props.response
38+
: JSON.stringify(props.response)
39+
40+
// Immediately start translation
41+
parseError(responseString).then(result => {
42+
if (result) {
43+
translatedError.value = result
44+
}
45+
isLoading.value = false
46+
})
47+
48+
return () => {
49+
const parsedResponse = typeof props.response === 'string'
50+
? JSON.parse(props.response)
51+
: props.response
52+
53+
return (
54+
<div class="mt-2">
55+
{/* 显示翻译后的错误信息(如果有) */}
56+
{translatedError.value && (
57+
<div class="text-red-500 font-medium mb-2">
58+
{translatedError.value}
59+
</div>
60+
)}
61+
62+
{/* 显示翻译状态 */}
63+
{isLoading.value && (
64+
<div class="text-gray-500 text-sm mb-2">
65+
{$gettext('Translating error...')}
66+
</div>
67+
)}
68+
69+
{/* 默认显示原始错误信息 */}
70+
<details class="mt-2">
71+
<summary class="cursor-pointer text-sm text-gray-600 hover:text-gray-800">
72+
{$gettext('Error details')}
73+
</summary>
74+
<pre class="mt-2 p-2 bg-gray-100 rounded text-xs overflow-hidden whitespace-pre-wrap break-words max-w-full">
75+
{JSON.stringify(parsedResponse, null, 2)}
76+
</pre>
77+
</details>
78+
</div>
79+
)
80+
}
81+
},
82+
})
83+
584
export function detailRender(args: Pick<CustomRenderArgs, 'record' | 'text'>) {
685
try {
786
return (
@@ -14,7 +93,7 @@ export function detailRender(args: Pick<CustomRenderArgs, 'record' | 'text'>) {
1493
</div>
1594
{args.record.details?.response && args.record.type !== NotificationTypeT.Success && (
1695
<div>
17-
{ JSON.stringify(args.record.details.response) }
96+
<ErrorDetails response={args.record.details.response} />
1897
</div>
1998
)}
2099
</div>

app/src/language/ar/app.po

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,6 +1891,10 @@ msgstr "البيئات"
18911891
msgid "Error"
18921892
msgstr "خطأ"
18931893

1894+
#: src/components/Notification/detailRender.tsx:72
1895+
msgid "Error details"
1896+
msgstr "تفاصيل الخطأ"
1897+
18941898
#: src/components/ConfigHistory/DiffViewer.vue:138
18951899
msgid "Error initializing diff viewer"
18961900
msgstr "خطأ في تهيئة عارض الاختلافات"
@@ -5707,6 +5711,10 @@ msgstr ""
57075711
"TOTP هو طريقة مصادقة ثنائية تستخدم خوارزمية كلمة مرور لمرة واحدة تعتمد على "
57085712
"الوقت."
57095713

5714+
#: src/components/Notification/detailRender.tsx:65
5715+
msgid "Translating error..."
5716+
msgstr "جاري ترجمة الخطأ..."
5717+
57105718
#: src/language/curd.ts:20
57115719
msgid "Trash"
57125720
msgstr "مهملات"

app/src/language/de_DE/app.po

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,10 @@ msgstr "Umgebungen"
19151915
msgid "Error"
19161916
msgstr "Fehler"
19171917

1918+
#: src/components/Notification/detailRender.tsx:72
1919+
msgid "Error details"
1920+
msgstr "Fehlerdetails"
1921+
19181922
#: src/components/ConfigHistory/DiffViewer.vue:138
19191923
msgid "Error initializing diff viewer"
19201924
msgstr "Fehler beim Initialisieren des Diff-Viewers"
@@ -5801,6 +5805,10 @@ msgstr ""
58015805
"TOTP ist eine Zwei-Faktor-Authentifizierungsmethode, die einen "
58025806
"zeitbasierten Einmalpasswortalgorithmus verwendet."
58035807

5808+
#: src/components/Notification/detailRender.tsx:65
5809+
msgid "Translating error..."
5810+
msgstr "Fehler wird übersetzt..."
5811+
58045812
#: src/language/curd.ts:20
58055813
msgid "Trash"
58065814
msgstr "Mülleimer"

app/src/language/en/app.po

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,10 @@ msgstr ""
18241824
msgid "Error"
18251825
msgstr ""
18261826

1827+
#: src/components/Notification/detailRender.tsx:72
1828+
msgid "Error details"
1829+
msgstr ""
1830+
18271831
#: src/components/ConfigHistory/DiffViewer.vue:138
18281832
msgid "Error initializing diff viewer"
18291833
msgstr ""
@@ -5534,6 +5538,10 @@ msgid ""
55345538
"password algorithm."
55355539
msgstr ""
55365540

5541+
#: src/components/Notification/detailRender.tsx:65
5542+
msgid "Translating error..."
5543+
msgstr ""
5544+
55375545
#: src/language/curd.ts:20
55385546
msgid "Trash"
55395547
msgstr ""

app/src/language/es/app.po

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,10 @@ msgstr "Entornos"
19261926
msgid "Error"
19271927
msgstr "Error"
19281928

1929+
#: src/components/Notification/detailRender.tsx:72
1930+
msgid "Error details"
1931+
msgstr "Detalles del error"
1932+
19291933
#: src/components/ConfigHistory/DiffViewer.vue:138
19301934
msgid "Error initializing diff viewer"
19311935
msgstr "Error al inicializar el visor de diferencias"
@@ -5809,6 +5813,10 @@ msgstr ""
58095813
"TOTP es un método de autenticación de dos factores que utiliza un algoritmo "
58105814
"de contraseña de un solo uso basado en el tiempo."
58115815

5816+
#: src/components/Notification/detailRender.tsx:65
5817+
msgid "Translating error..."
5818+
msgstr "Traduciendo error..."
5819+
58125820
#: src/language/curd.ts:20
58135821
msgid "Trash"
58145822
msgstr "Basura"

app/src/language/fr_FR/app.po

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,10 @@ msgstr "Environnements"
19221922
msgid "Error"
19231923
msgstr "Erreur"
19241924

1925+
#: src/components/Notification/detailRender.tsx:72
1926+
msgid "Error details"
1927+
msgstr "Détails de l'erreur"
1928+
19251929
#: src/components/ConfigHistory/DiffViewer.vue:138
19261930
msgid "Error initializing diff viewer"
19271931
msgstr "Erreur lors de l'initialisation du visualiseur de différences"
@@ -5818,6 +5822,10 @@ msgstr ""
58185822
"TOTP est une méthode d'authentification à deux facteurs qui utilise un "
58195823
"algorithme de mot de passe à usage unique basé sur le temps."
58205824

5825+
#: src/components/Notification/detailRender.tsx:65
5826+
msgid "Translating error..."
5827+
msgstr "Traduction de l'erreur..."
5828+
58215829
#: src/language/curd.ts:20
58225830
msgid "Trash"
58235831
msgstr "Corbeille"

app/src/language/ja_JP/app.po

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,6 +1858,10 @@ msgstr "環境"
18581858
msgid "Error"
18591859
msgstr "エラー"
18601860

1861+
#: src/components/Notification/detailRender.tsx:72
1862+
msgid "Error details"
1863+
msgstr "エラーの詳細"
1864+
18611865
#: src/components/ConfigHistory/DiffViewer.vue:138
18621866
msgid "Error initializing diff viewer"
18631867
msgstr "差分ビューアの初期化エラー"
@@ -5602,6 +5606,10 @@ msgid ""
56025606
"password algorithm."
56035607
msgstr "TOTP は、時間ベースのワンタイムパスワードアルゴリズムを使用する二要素認証方法です。"
56045608

5609+
#: src/components/Notification/detailRender.tsx:65
5610+
msgid "Translating error..."
5611+
msgstr "エラーを翻訳中..."
5612+
56055613
#: src/language/curd.ts:20
56065614
msgid "Trash"
56075615
msgstr "ゴミ箱"

app/src/language/ko_KR/app.po

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,10 @@ msgstr "환경"
18571857
msgid "Error"
18581858
msgstr "오류"
18591859

1860+
#: src/components/Notification/detailRender.tsx:72
1861+
msgid "Error details"
1862+
msgstr "오류 세부 정보"
1863+
18601864
#: src/components/ConfigHistory/DiffViewer.vue:138
18611865
msgid "Error initializing diff viewer"
18621866
msgstr "차이점 뷰어 초기화 오류"
@@ -5138,7 +5142,7 @@ msgstr "인증서 동기화"
51385142

51395143
#: src/components/Notification/notifications.ts:62
51405144
msgid "Sync Certificate %{cert_name} to %{env_name} failed"
5141-
msgstr "인증서 %{cert_name}을(를) %{env_name}(으)로 동기화하지 못했습니다"
5145+
msgstr "인증서 %{cert_name}을(를) %{env_name} 동기화하지 못했습니다"
51425146

51435147
#: src/components/Notification/notifications.ts:66
51445148
msgid "Sync Certificate %{cert_name} to %{env_name} successfully"
@@ -5595,6 +5599,10 @@ msgid ""
55955599
"password algorithm."
55965600
msgstr "TOTP는 시간 기반의 일회용 비밀번호 알고리즘을 사용하는 이중 인증 방법입니다."
55975601

5602+
#: src/components/Notification/detailRender.tsx:65
5603+
msgid "Translating error..."
5604+
msgstr "오류 번역 중..."
5605+
55985606
#: src/language/curd.ts:20
55995607
msgid "Trash"
56005608
msgstr "휴지통"

app/src/language/messages.pot

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,10 @@ msgstr ""
18021802
msgid "Error"
18031803
msgstr ""
18041804

1805+
#: src/components/Notification/detailRender.tsx:72
1806+
msgid "Error details"
1807+
msgstr ""
1808+
18051809
#: src/components/ConfigHistory/DiffViewer.vue:138
18061810
msgid "Error initializing diff viewer"
18071811
msgstr ""
@@ -5421,6 +5425,10 @@ msgstr ""
54215425
msgid "TOTP is a two-factor authentication method that uses a time-based one-time password algorithm."
54225426
msgstr ""
54235427

5428+
#: src/components/Notification/detailRender.tsx:65
5429+
msgid "Translating error..."
5430+
msgstr ""
5431+
54245432
#: src/language/curd.ts:20
54255433
msgid "Trash"
54265434
msgstr ""

app/src/language/pt_PT/app.po

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,6 +1903,10 @@ msgstr "Ambientes"
19031903
msgid "Error"
19041904
msgstr "Erro"
19051905

1906+
#: src/components/Notification/detailRender.tsx:72
1907+
msgid "Error details"
1908+
msgstr "Detalhes do erro"
1909+
19061910
#: src/components/ConfigHistory/DiffViewer.vue:138
19071911
msgid "Error initializing diff viewer"
19081912
msgstr "Erro ao inicializar o visualizador de diferenças"
@@ -5776,6 +5780,10 @@ msgstr ""
57765780
"O TOTP é um método de autenticação de dois fatores que utiliza um algoritmo "
57775781
"de palavra-passe única baseado no tempo."
57785782

5783+
#: src/components/Notification/detailRender.tsx:65
5784+
msgid "Translating error..."
5785+
msgstr "A traduzir erro..."
5786+
57795787
#: src/language/curd.ts:20
57805788
msgid "Trash"
57815789
msgstr "Lixo"

0 commit comments

Comments
 (0)