Skip to content

Commit

Permalink
feat: Add errorCallback config option to VivliostylePrint/printHTML()
Browse files Browse the repository at this point in the history
So far VivliostylePrint has no way to handle errors. This commit adds a new config option `errorCallback` to VivliostylePrint/`printHTML()` to handle errors.

Usage example:

```ts
  printHTML(htmlDoc, {
    errorCallback: (msg) => {
      alert(msg);
    },
  });
```

This commit also fixes the following issues on error handling:

- `Payload.content` type should be `ErrorInfo` instead of `string`.
- `loadDocument()` and `loadPublication()` in CoreViewer needed to fix the "No URL specified" error handling.
- `Logging.logger.addListener(level, listener)` used to push a listener to an array per level so that multiple listeners can be registered for the same level. However, that was problematic because the `Logging.logger` is a singleton instance but the CoreViewer instance is created each time `printHTML()` is called, and new listeners are added each time without removing the old ones, causing the listeners to be duplicated. This commit changes the `addListener` method to replace the listener for the level if it already exists.
  • Loading branch information
MurakamiShinyu committed Feb 22, 2024
1 parent b0eebea commit 65c5a5f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
11 changes: 7 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,12 @@ export class CoreViewer {
opt_documentOptions?: DocumentOptions,
opt_viewerOptions?: CoreViewerOptions,
) {
if (!singleDocumentOptions) {
if (!singleDocumentOptions || !singleDocumentOptions[0]) {
this.eventTarget.dispatchEvent({
type: "error",
content: "No URL specified",
content: { error: new Error("No URL specified") },
});
return;
}
this.loadDocumentOrPublication(
singleDocumentOptions,
Expand All @@ -276,8 +278,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: (msg: string) => void | null;
hideIframe: boolean;
removeIframe: boolean;
}
Expand All @@ -18,6 +19,7 @@ class VivliostylePrint {
htmlDoc: string;
title: string;
printCallback: (iframeWin: Window) => void;
errorCallback: (msg: 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 msg =
payload.content.error?.toString() ||
payload.content.messages.join("\n");
this.errorCallback(msg);
});
}

Viewer.loadDocument({
url: docURL,
});
Expand Down

0 comments on commit 65c5a5f

Please sign in to comment.