@svadmin/core 0.66.1 → 0.66.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/query-session.svelte.ts +51 -2
package/package.json
CHANGED
|
@@ -19,8 +19,48 @@ const notificationSchema = Type.Object({
|
|
|
19
19
|
}, { additionalProperties: false });
|
|
20
20
|
|
|
21
21
|
const queryErrorNotificationKeys = new WeakMap<object, string>();
|
|
22
|
+
const reportedQueryFailures = new WeakMap<object, number>();
|
|
22
23
|
let nextQueryErrorNotificationId = 0;
|
|
23
24
|
|
|
25
|
+
function ownData(value: unknown, key: string): unknown {
|
|
26
|
+
try {
|
|
27
|
+
return typeof value === 'object' && value !== null
|
|
28
|
+
? Object.getOwnPropertyDescriptor(value, key)?.value
|
|
29
|
+
: undefined;
|
|
30
|
+
} catch {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 自定义通知提供方使用的安全技术上下文。内置 toast 会把 description 拼进主文案,因此不能传给它。 */
|
|
36
|
+
function queryFailureDescription(resource: string, value: unknown): string {
|
|
37
|
+
const status = ownData(value, 'statusCode');
|
|
38
|
+
const code = ownData(value, 'code');
|
|
39
|
+
return [
|
|
40
|
+
resource,
|
|
41
|
+
typeof code === 'string' && code.length > 0 ? code : undefined,
|
|
42
|
+
typeof status === 'number' ? `HTTP ${status}` : undefined,
|
|
43
|
+
].filter((part): part is string => typeof part === 'string').join(' · ');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Expose only the normalized failure fields; provider diagnostics stay private. */
|
|
47
|
+
function reportQueryFailure(resource: string, value: unknown, updatedAt: number, query: object | undefined): void {
|
|
48
|
+
try {
|
|
49
|
+
if (query && reportedQueryFailures.get(query) === updatedAt) return;
|
|
50
|
+
if (query) reportedQueryFailures.set(query, updatedAt);
|
|
51
|
+
const status = ownData(value, 'statusCode');
|
|
52
|
+
const code = ownData(value, 'code');
|
|
53
|
+
console.error('[svadmin] Query failed', {
|
|
54
|
+
resource,
|
|
55
|
+
...(typeof status === 'number' ? { status } : {}),
|
|
56
|
+
...(typeof code === 'string' ? { code } : {}),
|
|
57
|
+
message: value instanceof Error ? value.message : 'Resource query failed',
|
|
58
|
+
});
|
|
59
|
+
} catch {
|
|
60
|
+
console.error('[svadmin] Query failed', { resource });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
24
64
|
export interface QuerySessionOptions {
|
|
25
65
|
queryKey: QueryKey;
|
|
26
66
|
resource: string;
|
|
@@ -106,7 +146,13 @@ export function createQuerySession<O extends QuerySessionOptions>(context: Admin
|
|
|
106
146
|
const fallback = type === 'success' ? '' : t('common.loadFailed', { message: t('common.error') });
|
|
107
147
|
if (typeof config !== 'function' && !config && !fallback) return;
|
|
108
148
|
const candidate = snapshotPlainData(typeof config === 'function'
|
|
109
|
-
? config(value, undefined, origin.resource)
|
|
149
|
+
? config(value, undefined, origin.resource)
|
|
150
|
+
: {
|
|
151
|
+
message: config || fallback,
|
|
152
|
+
...(type === 'error' && !config && origin.notification
|
|
153
|
+
? { description: queryFailureDescription(origin.resource, value) }
|
|
154
|
+
: {}),
|
|
155
|
+
});
|
|
110
156
|
if (!checkExact(notificationSchema, candidate) || !current(origin) || origin.notification !== context.notificationProvider) return;
|
|
111
157
|
let key = candidate.key;
|
|
112
158
|
if (type === 'error' && !config) {
|
|
@@ -193,9 +239,12 @@ export function createQuerySession<O extends QuerySessionOptions>(context: Admin
|
|
|
193
239
|
} else if (result.isError && (lastError?.key !== key || result.errorUpdatedAt > lastError.at)) {
|
|
194
240
|
lastError = { key, at: result.errorUpdatedAt };
|
|
195
241
|
untrack(() => {
|
|
242
|
+
const failure = errorFor(origin, result.error);
|
|
243
|
+
const query = client.getQueryCache().find({ queryKey: origin.queryKey, exact: true });
|
|
244
|
+
reportQueryFailure(origin.resource, failure, result.errorUpdatedAt, query);
|
|
196
245
|
void handleAuthError(querySessionError(result.error), context, () => targetCurrent(origin),
|
|
197
246
|
() => clearAuthQueries(client, origin.provider), origin.auth).catch(() => {});
|
|
198
|
-
notify(origin, 'error',
|
|
247
|
+
notify(origin, 'error', failure);
|
|
199
248
|
});
|
|
200
249
|
}
|
|
201
250
|
});
|