Skip to content

Commit 4ed990a

Browse files
committed
Exception Improvements
- simplify parsing - throw exception with message - add simple tests
1 parent dd1e89a commit 4ed990a

File tree

5 files changed

+400
-33
lines changed

5 files changed

+400
-33
lines changed

src/core/handlers/include-notebook.ts

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,24 @@ export interface NotebookInclude {
4040
params: Record<string, string>;
4141
}
4242

43-
const resolveCellIds = (hash: string) => {
44-
if (hash.indexOf(",") > 0) {
43+
const resolveCellIds = (hash?: string) => {
44+
if (hash && hash.indexOf(",") > 0) {
4545
return hash.split(",");
4646
} else {
4747
return hash;
4848
}
4949
};
5050

5151
// If the path is a notebook path, then process it separately.
52-
const kPlaceholderProtocol = "ipynb://";
5352
export function parseNotebookPath(path: string) {
54-
if (!path.startsWith(kPlaceholderProtocol)) {
55-
path = `${kPlaceholderProtocol}/${path}`;
56-
}
57-
const url = new URL(path);
58-
const pathname = url.pathname;
59-
if (extname(pathname) === ".ipynb") {
60-
const hash = url.hash;
53+
const hasHash = path.indexOf("#") !== -1;
54+
const hash = hasHash ? path.split("#")[1] : undefined;
55+
path = path.split("#")[0];
56+
57+
if (extname(path) === ".ipynb") {
6158
const cellIds = resolveCellIds(hash);
6259
return {
63-
path: pathname,
60+
path,
6461
cellIds,
6562
} as NotebookInclude;
6663
} else {
@@ -71,34 +68,38 @@ export function parseNotebookPath(path: string) {
7168
export function notebookForInclude(
7269
nbInclude: NotebookInclude,
7370
) {
74-
const nb = jupyterFromFile(nbInclude.path);
75-
const cells: JupyterCell[] = [];
71+
try {
72+
const nb = jupyterFromFile(nbInclude.path);
73+
const cells: JupyterCell[] = [];
7674

77-
// If cellIds are present, filter the notebook to only include
78-
// those cells
79-
if (nbInclude.cellIds) {
80-
for (const cell of nb.cells) {
81-
// cellId can either by a literal cell Id, or a tag with that value
82-
const hasId = cell.id ? nbInclude.cellIds.includes(cell.id) : false;
83-
if (hasId) {
84-
// It's an ID
85-
cells.push(cell);
86-
} else {
87-
// Check tags
88-
const hasTag = cell.metadata.tags
89-
? cell.metadata.tags.find((tag) =>
90-
nbInclude.cellIds?.includes(tag)
91-
) !==
92-
undefined
93-
: false;
94-
if (hasTag) {
75+
// If cellIds are present, filter the notebook to only include
76+
// those cells
77+
if (nbInclude.cellIds) {
78+
for (const cell of nb.cells) {
79+
// cellId can either by a literal cell Id, or a tag with that value
80+
const hasId = cell.id ? nbInclude.cellIds.includes(cell.id) : false;
81+
if (hasId) {
82+
// It's an ID
9583
cells.push(cell);
84+
} else {
85+
// Check tags
86+
const hasTag = cell.metadata.tags
87+
? cell.metadata.tags.find((tag) =>
88+
nbInclude.cellIds?.includes(tag)
89+
) !==
90+
undefined
91+
: false;
92+
if (hasTag) {
93+
cells.push(cell);
94+
}
9695
}
9796
}
97+
nb.cells = cells;
9898
}
99-
nb.cells = cells;
99+
return nb;
100+
} catch (ex) {
101+
throw new Error(`Failed to read included notebook ${nbInclude.path}`, ex);
100102
}
101-
return nb;
102103
}
103104

104105
export async function notebookMarkdown(

0 commit comments

Comments
 (0)