71 lines
1.5 KiB
TypeScript
71 lines
1.5 KiB
TypeScript
import {
|
|
ApplicationRequestError,
|
|
DocxExportServiceError
|
|
} from "@md-to-pdf/application";
|
|
import type {
|
|
DocxCapability,
|
|
DocxExportDiagnostics,
|
|
DocxExportErrorCode,
|
|
DocxGenerationTimings
|
|
} from "@md-to-pdf/core";
|
|
import { docxExportErrorCodeSchema } from "@md-to-pdf/core";
|
|
|
|
export interface DesktopDocxExportSuccess {
|
|
ok: true;
|
|
saved: boolean;
|
|
fileName: string;
|
|
diagnostics: DocxExportDiagnostics;
|
|
timings: DocxGenerationTimings;
|
|
}
|
|
|
|
export interface DesktopDocxExportFailure {
|
|
ok: false;
|
|
error: DocxExportErrorCode;
|
|
message: string;
|
|
retryable: boolean;
|
|
}
|
|
|
|
export type DesktopDocxExportOutcome =
|
|
| DesktopDocxExportSuccess
|
|
| DesktopDocxExportFailure;
|
|
|
|
export type DesktopDocxCapability = DocxCapability;
|
|
|
|
export function toDesktopDocxExportFailure(
|
|
error: unknown
|
|
): DesktopDocxExportFailure {
|
|
if (error instanceof ApplicationRequestError) {
|
|
const parsedCode = docxExportErrorCodeSchema.safeParse(
|
|
error.code
|
|
);
|
|
if (!parsedCode.success) {
|
|
return {
|
|
ok: false,
|
|
error: "DOCX_GENERATION_FAILED",
|
|
message: "DOCX 导出失败",
|
|
retryable: false
|
|
};
|
|
}
|
|
return {
|
|
ok: false,
|
|
error: parsedCode.data,
|
|
message: error.message,
|
|
retryable: false
|
|
};
|
|
}
|
|
if (error instanceof DocxExportServiceError) {
|
|
return {
|
|
ok: false,
|
|
error: error.code,
|
|
message: error.message,
|
|
retryable: error.retryable
|
|
};
|
|
}
|
|
return {
|
|
ok: false,
|
|
error: "DOCX_GENERATION_FAILED",
|
|
message: "DOCX 导出失败",
|
|
retryable: false
|
|
};
|
|
}
|