Skip to content

Commit cd56081

Browse files
author
JJ Allaire
committed
report errors during devserver render
1 parent 74690aa commit cd56081

5 files changed

Lines changed: 22 additions & 30 deletions

File tree

‎src/command/preview/preview.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
httpContentResponse,
2121
httpFileRequestHandler,
2222
HttpFileRequestOptions,
23-
maybeDisplaySocketError,
2423
} from "../../core/http.ts";
2524
import { HttpDevServer, httpDevServer } from "../../core/http-devserver.ts";
2625
import { isHtmlContent, isPdfContent } from "../../core/mime.ts";
@@ -408,7 +407,7 @@ function htmlFileRequestHandlerOptions(
408407
file = format.formatPreviewFile(file, format);
409408
}
410409
const fileContents = await Deno.readFile(file);
411-
return reloader.injectClient(fileContents, inputFile, format);
410+
return reloader.injectClient(fileContents, inputFile);
412411
}
413412
},
414413
};

‎src/command/serve/serve.ts‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
*
66
*/
77

8-
import { error, warning } from "log/mod.ts";
8+
import { warning } from "log/mod.ts";
99
import { existsSync } from "fs/mod.ts";
1010
import { basename, dirname, join, relative } from "path/mod.ts";
1111

12-
import { listenAndServe } from "http/mod.ts";
13-
1412
import { ld } from "lodash/mod.ts";
1513
import { DOMParser } from "deno_dom/deno-dom-wasm-noinit.ts";
1614

@@ -48,7 +46,6 @@ import {
4846
import {
4947
httpFileRequestHandler,
5048
HttpFileRequestOptions,
51-
maybeDisplaySocketError,
5249
} from "../../core/http.ts";
5350
import { ServeOptions } from "./types.ts";
5451
import { copyProjectForServe } from "./serve-shared.ts";
@@ -256,6 +253,7 @@ export async function serveProject(
256253
);
257254
}
258255
let result: RenderResult | undefined;
256+
let renderError: Error | undefined;
259257
if (inputFile) {
260258
const renderFlags = { ...flags, quiet: true };
261259
// remove 'to' argument to allow the file to be rendered in it's default format
@@ -281,27 +279,28 @@ export async function serveProject(
281279
);
282280
if (result.error) {
283281
logError(result.error);
282+
renderError = result.error;
284283
}
285284
} catch (e) {
286285
logError(e);
286+
renderError = e;
287287
}
288288
}
289289

290290
// read the output file
291-
const fileContents = Deno.readFileSync(file);
291+
const fileContents = renderError
292+
? renderErrorPage(renderError)
293+
: Deno.readFileSync(file);
292294

293295
// inject watcher client for html
294-
if (
295-
isHtmlContent(file) && inputFile && result && result.files.length > 0
296-
) {
296+
if (isHtmlContent(file) && inputFile) {
297297
const projInputFile = join(
298298
project!.dir,
299299
relative(watcher.serveProject().dir, inputFile),
300300
);
301301
return watcher.injectClient(
302302
fileContents,
303303
projInputFile,
304-
result.files[0].format,
305304
);
306305
} else {
307306
return fileContents;
@@ -394,6 +393,11 @@ export async function serveProject(
394393
}
395394
}
396395

396+
function renderErrorPage(e: Error) {
397+
const content = e.message;
398+
return new TextEncoder().encode(content);
399+
}
400+
397401
async function serveFiles(
398402
project: ProjectContext,
399403
): Promise<{ files: string[]; resourceFiles: string[] }> {

‎src/command/serve/types.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*
66
*/
77

8-
import { Format } from "../../config/types.ts";
9-
108
import { ProjectContext } from "../../project/types.ts";
119

1210
export interface ProjectWatcher {
@@ -15,7 +13,6 @@ export interface ProjectWatcher {
1513
injectClient: (
1614
file: Uint8Array,
1715
inputFile?: string,
18-
format?: Format,
1916
) => Uint8Array;
2017
project: () => ProjectContext;
2118
serveProject: () => ProjectContext;

‎src/command/serve/watch.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import { copyProjectForServe } from "./serve-shared.ts";
2424

2525
import { ProjectWatcher, ServeOptions } from "./types.ts";
2626
import { httpDevServer } from "../../core/http-devserver.ts";
27-
import { Format } from "../../config/types.ts";
2827
import { RenderFlags, RenderResult } from "../render/types.ts";
2928
import { renderProject } from "../render/project.ts";
3029
import { PromiseQueue } from "../../core/promise.ts";
@@ -330,8 +329,8 @@ export function watchProject(
330329
return devServer.handle(req);
331330
},
332331
connect: devServer.connect,
333-
injectClient: (file: Uint8Array, inputFile?: string, format?: Format) => {
334-
return devServer.injectClient(file, inputFile, format);
332+
injectClient: (file: Uint8Array, inputFile?: string) => {
333+
return devServer.injectClient(file, inputFile);
335334
},
336335
project: () => project,
337336
serveProject: () => serveProject,

‎src/core/http-devserver.ts‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
import { LogRecord } from "log/mod.ts";
99

10-
import { isRevealjsOutput } from "../config/format.ts";
11-
import { Format } from "../config/types.ts";
1210
import { renderEjs } from "./ejs.ts";
1311
import { maybeDisplaySocketError } from "./http.ts";
1412
import { LogEventsHandler } from "./log.ts";
@@ -21,7 +19,6 @@ export interface HttpDevServer {
2119
injectClient: (
2220
file: Uint8Array,
2321
inputFile?: string,
24-
format?: Format,
2522
) => Uint8Array;
2623
reloadClients: (reloadTarget?: string) => Promise<void>;
2724
}
@@ -68,9 +65,9 @@ export function httpDevServer(
6865
return Promise.resolve(undefined);
6966
}
7067
},
71-
injectClient: (file: Uint8Array, inputFile?: string, format?: Format) => {
68+
injectClient: (file: Uint8Array, inputFile?: string) => {
7269
const scriptContents = new TextEncoder().encode(
73-
"\n" + devServerClientScript(port, inputFile, format, isPresentation),
70+
"\n" + devServerClientScript(port, inputFile, isPresentation),
7471
);
7572
const fileWithScript = new Uint8Array(
7673
file.length + scriptContents.length,
@@ -106,7 +103,6 @@ export function httpDevServer(
106103
function devServerClientScript(
107104
port: number,
108105
inputFile?: string,
109-
format?: Format,
110106
isPresentation?: boolean,
111107
): string {
112108
// core devserver
@@ -116,13 +112,10 @@ function devServerClientScript(
116112
port,
117113
}),
118114
];
119-
if (isPresentation && format && isRevealjsOutput(format.pandoc)) {
120-
// revealjs devserver
121-
if (isRevealjsOutput(format.pandoc)) {
122-
devserver.push(
123-
renderEjs(resourcePath("editor/devserver/devserver-revealjs.html"), {}),
124-
);
125-
}
115+
if (isPresentation) {
116+
devserver.push(
117+
renderEjs(resourcePath("editor/devserver/devserver-revealjs.html"), {}),
118+
);
126119
} else {
127120
// viewer devserver
128121
devserver.push(

0 commit comments

Comments
 (0)