{"record":{"id":"ca2f0a6d074dd5f3","repo":"iflytek/astron-agent","slug":"err-as-error-message","errorCode":null,"errorMessage":"${(err as Error)?.message || '分享失败，请稍后再试~'}","messagePattern":"\\$\\{\\(err as Error\\)\\?\\.message \\|\\| '分享失败，请稍后再试~'\\}","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"console/frontend/src/utils/index.ts","lineNumber":649,"sourceCode":"): Promise<void> => {\n  try {\n    // 1. 获取分享key\n    const res = await getShareAgentKey({\n      relateType: 0,\n      relateId: botId,\n    });\n\n    const shareUrl = t('shareModal.shareOriginModal.shareText', {\n      botName: botName,\n      origin: window.location.origin,\n      botId: botId,\n      shareKey: res.shareAgentKey,\n    });\n\n    // 2. 复制分享链接\n    copyText({ text: shareUrl, successText: t('home.copyLinkDone') });\n  } catch (err) {\n    message.error((err as Error)?.message || '分享失败，请稍后再试~');\n    console.warn('分享失败:', err);\n  }\n};\n\n/**\n * 根据文件类型设置文件图标\n */\nconst getFileIcon = (file: UploadFileInfo, isLoading?: boolean) => {\n  const extension = file?.fileName?.split('.')?.pop()?.toLowerCase();\n  if (isLoading) {\n    return fileIconConfig.loading;\n  }\n  // 遍历所有分类查找文件扩展名\n  for (const category of Object.values(fileIconConfig)) {\n    if (category[extension as keyof typeof category]) {\n      return category[extension as keyof typeof category];\n    }\n  }","sourceCodeStart":631,"sourceCodeEnd":667,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/console/frontend/src/utils/index.ts#L631-L667","documentation":"handleShare in console/frontend/src/utils/index.ts catches any error thrown while creating an agent share link (the share API request or subsequent copyText step) and surfaces it via message.error, falling back to the generic Chinese message '分享失败，请稍后再试~' when the thrown value has no message property. It is a UI-level aggregation of upstream API failures, not a single error type.","triggerScenarios":"The shareAgent API call rejects (network failure, 4xx/5xx, auth token expiry, rate limit) or any code between try and catch throws; the catch reads (err as Error)?.message and shows it, defaulting to the generic fallback when message is undefined.","commonSituations":"Session expired so the share endpoint returns 401; backend unavailable or gateway timeout; the agent isn't shareable due to permission policy; backend returns an error body whose message field is empty so the fallback text shows; non-Error rejection values (plain strings/undefined) causing the fallback.","solutions":["Check the browser network tab for the share API response status and body to find the real cause.","Refresh/re-login if the response was 401 — expired auth is the most frequent trigger.","Retry after a moment if the backend returned 5xx or a timeout; confirm the share service is healthy.","Improve error normalization: use AxiosError.response.data.message or instance of Error checks so users see actionable text.","Disable the share button while the request is in flight to prevent duplicate failing calls."],"exampleFix":"// before\n} catch (err) {\n  message.error((err as Error)?.message || '分享失败，请稍后再试~');\n}\n\n// after\n} catch (err: unknown) {\n  const msg = err instanceof Error\n    ? err.message\n    : (err as { message?: string })?.message || t('home.shareFailed');\n  message.error(msg);\n  console.warn('分享失败:', err);\n}","handlingStrategy":"try-catch","validationCode":"async function shareGuarded(agentId: string) {\n  if (!agentId) { message.warning('Agent not saved yet'); return false; }\n  if (!isLoggedIn()) { message.warning('Please sign in to share'); return false; }\n  return true;\n}","typeGuard":"function isErrorWithMessage(err: unknown): err is Error & { message: string } {\n  return err instanceof Error && typeof err.message === 'string' && err.message.length > 0;\n}","tryCatchPattern":"try {\n  await handleShare(agent);\n} catch (err: unknown) {\n  const msg = isErrorWithMessage(err) ? err.message : t('home.shareFailed');\n  message.error(msg);\n  console.warn('分享失败:', err);\n}","preventionTips":["Check auth state before calling share APIs; re-authenticate on 401.","Disable the share button while the request is pending.","Normalize API error bodies into Error objects with user-readable messages.","Log the full error (console.warn) for diagnosis while showing a friendly toast."],"tags":["error-handling","typescript","frontend","share","react"],"backgroundTag":"api-request-failed","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}