2026-10-16 12:07:25 +07:00
|
|
|
/** ข้อความ error ที่อ่านได้สำหรับแสดงใน task pane — ไม่เคย throw (กติกา: ทุก Office.js/API call ต้อง catch) */
|
|
|
|
|
export function getErrorMessage(error: unknown, fallback: string): string {
|
|
|
|
|
if (error instanceof Error && error.message) {
|
|
|
|
|
return error.message;
|
|
|
|
|
}
|
|
|
|
|
if (typeof error === "string" && error) {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
return fallback;
|
|
|
|
|
}
|
2026-10-20 09:49:17 +07:00
|
|
|
|
|
|
|
|
/** เหมือน getErrorMessage แต่ต่อท้าย code/errorLocation จาก debugInfo ของ OfficeExtension.Error —
|
|
|
|
|
* สำหรับ error จาก Word.run ที่ต้องการสาเหตุละเอียด (เช่น ราย tag ที่เขียนค่าไม่สำเร็จ) */
|
|
|
|
|
export function getDetailedErrorMessage(
|
|
|
|
|
error: unknown,
|
|
|
|
|
fallback = "เกิดข้อผิดพลาดที่ไม่ทราบสาเหตุ",
|
|
|
|
|
): string {
|
|
|
|
|
if (error instanceof Error && error.message) {
|
|
|
|
|
const debugInfo = (error as { debugInfo?: { code?: string; errorLocation?: string } }).debugInfo;
|
|
|
|
|
const code = debugInfo?.code ? ` [${debugInfo.code}]` : "";
|
|
|
|
|
const location = debugInfo?.errorLocation ? ` (${debugInfo.errorLocation})` : "";
|
|
|
|
|
return `${error.message}${code}${location}`;
|
|
|
|
|
}
|
|
|
|
|
if (typeof error === "string" && error) {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
return fallback;
|
|
|
|
|
}
|