Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add errorCallback config option to VivliostylePrint/printHTML() #1287

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions packages/core/src/vivliostyle/core-viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ import * as Constants from "./constants";
import * as Epub from "./epub";
import * as Profile from "./profile";
import * as Toc from "./toc";
import { ErrorInfo } from "./logging";

export interface Payload {
type: string;
internal: boolean;
href: string;
content: string;
content: ErrorInfo;
cfi: string;
first: boolean;
last: boolean;
Expand Down Expand Up @@ -251,11 +252,20 @@ export class CoreViewer {
opt_documentOptions?: DocumentOptions,
opt_viewerOptions?: CoreViewerOptions,
) {
if (!singleDocumentOptions) {
if (
!singleDocumentOptions ||
(Array.isArray(singleDocumentOptions)
? !singleDocumentOptions[0] ||
(typeof singleDocumentOptions[0] !== "string" &&
!singleDocumentOptions[0].url)
: typeof singleDocumentOptions !== "string" &&
!singleDocumentOptions.url)
) {
this.eventTarget.dispatchEvent({
type: "error",
content: "No URL specified",
content: { error: new Error("No URL specified") },
});
return;
}
this.loadDocumentOrPublication(
singleDocumentOptions,
Expand All @@ -276,8 +286,9 @@ export class CoreViewer {
if (!pubUrl) {
this.eventTarget.dispatchEvent({
type: "error",
content: "No URL specified",
content: { error: new Error("No URL specified") },
});
return;
}
this.loadDocumentOrPublication(
null,
Expand Down
16 changes: 5 additions & 11 deletions packages/core/src/vivliostyle/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type ErrorInfo = {
* Class logging error, warning, information or debug messages.
*/
export class Logger {
private listeners: { [key in LogLevel]?: ((p1: ErrorInfo) => void)[] } = {};
private listeners: { [key in LogLevel]?: (p1: ErrorInfo) => void } = {};

constructor(private opt_console?: Console) {}

Expand Down Expand Up @@ -91,11 +91,9 @@ export class Logger {
}

private triggerListeners(level: LogLevel, args: ErrorInfo) {
const listeners = this.listeners[level];
if (listeners) {
listeners.forEach((listener) => {
listener(args);
});
const listener = this.listeners[level];
if (listener) {
listener(args);
}
}

Expand All @@ -104,11 +102,7 @@ export class Logger {
* occurs.
*/
addListener(level: LogLevel, listener: (p1: ErrorInfo) => void) {
let listeners = this.listeners[level];
if (!listeners) {
listeners = this.listeners[level] = [];
}
listeners.push(listener);
this.listeners[level] = listener;
}

debug(...var_args: any[]) {
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/vivliostyle/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface IFrameWindowForPrint {
export interface PrintConfig {
title: string;
printCallback: (iframeWin: Window) => void;
errorCallback: (message: string) => void | null;
hideIframe: boolean;
removeIframe: boolean;
}
Expand All @@ -18,6 +19,7 @@ class VivliostylePrint {
htmlDoc: string;
title: string;
printCallback: (iframeWin: Window) => void;
errorCallback: (message: string) => void;
hideIframe: boolean;
removeIframe: boolean;
iframe: HTMLIFrameElement;
Expand All @@ -29,13 +31,15 @@ class VivliostylePrint {
{
title = "",
printCallback = (iframeWin: Window) => iframeWin.print(),
errorCallback = null,
hideIframe = true,
removeIframe = true,
}: PrintConfig,
) {
this.htmlDoc = htmlDoc;
this.title = title;
this.printCallback = printCallback;
this.errorCallback = errorCallback;
this.hideIframe = hideIframe;
this.removeIframe = removeIframe;
}
Expand Down Expand Up @@ -110,6 +114,15 @@ class VivliostylePrint {
}
});

if (this.errorCallback) {
Viewer.addListener("error", (payload) => {
const message =
payload.content.error?.toString() ??
payload.content.messages.join("\n");
this.errorCallback(message);
});
}

Viewer.loadDocument({
url: docURL,
});
Expand Down
7 changes: 5 additions & 2 deletions packages/react/src/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,19 @@ export const Renderer = ({
}

function registerEventHandlers() {
const getMessage = (payload: Payload) =>
payload.content.error?.toString() ?? payload.content.messages.join("\n");

function handleMessage(payload: Payload, type: MessageType) {
onMessage && onMessage(payload.content, type);
onMessage && onMessage(getMessage(payload), type);
}

const handleDebug = (payload: Payload) => handleMessage(payload, "debug");
const handleInfo = (payload: Payload) => handleMessage(payload, "info");
const handleWarn = (payload: Payload) => handleMessage(payload, "warn");

function handleError(payload: Payload) {
onError && onError(payload.content);
onError && onError(getMessage(payload));
}

function handleReadyStateChange() {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/stories/Renderer.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const Basic = () => (
onLoad={action("loaded")}
onError={action("error")}
onNavigation={action("navigation")}
onMessage={(msg, type) => action("message")(type, msg.messages[0])}
onMessage={(msg, type) => action("message")(type, msg)}
onReadyStateChange={action("readyStateChange")}
onHyperlink={action("hyperlink")}
/>
Expand Down
10 changes: 1 addition & 9 deletions packages/viewer/src/viewmodels/message-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,7 @@ class MessageDialog {
}

getDisplayMessage(errorInfo: ErrorInfo): string {
const e = errorInfo.error;
let msg = e && (e.toString() || e.frameTrace || e.stack);
if (msg) {
msg = msg.split("\n", 1)[0];
}
if (!msg) {
msg = errorInfo.messages.join("\n");
}
return msg;
return errorInfo.error?.toString() ?? errorInfo.messages.join("\n");
}
}

Expand Down
Loading