Skip to content

Commit

Permalink
✍️ Report tracebacks during kernel execution (#1406)
Browse files Browse the repository at this point in the history
Co-authored-by: Rowan Cockett <rowanc1@gmail.com>
  • Loading branch information
agoose77 and rowanc1 authored Jul 23, 2024
1 parent 19e96e2 commit 28642fe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-impalas-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"myst-execute": patch
---

Add error messages for failed kernel execution
22 changes: 11 additions & 11 deletions packages/myst-execute/src/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { PageFrontmatter } from 'myst-frontmatter';
import type { Kernel, KernelMessage, Session, SessionManager } from '@jupyterlab/services';
import type { Code, InlineExpression } from 'myst-spec-ext';
import type { IOutput } from '@jupyterlab/nbformat';
import type { GenericNode, GenericParent, IExpressionResult } from 'myst-common';
import { NotebookCell, fileError, fileWarn } from 'myst-common';
import type { GenericNode, GenericParent, IExpressionResult, IExpressionError } from 'myst-common';
import { NotebookCell, fileError } from 'myst-common';
import type { VFile } from 'vfile';
import path from 'node:path';
import assert from 'node:assert';
Expand Down Expand Up @@ -190,9 +190,13 @@ async function computeExecutableNodes(
// Check for errors
const allowErrors = !!matchedNode.data?.tags?.includes?.('raises-exception');
if (status === 'error' && !allowErrors) {
fileWarn(
const errorMessage = outputs
.map((item) => item.traceback)
.flat()
.join('\n');
fileError(
opts.vfile,
'An exception occurred during code execution, halting further execution',
`An exception occurred during code execution, halting further execution:\n\n${errorMessage}`,
{
node: matchedNode,
},
Expand All @@ -207,9 +211,10 @@ async function computeExecutableNodes(

// Check for errors
if (status === 'error') {
fileWarn(
const errorMessage = (result as IExpressionError).traceback.join('\n');
fileError(
opts.vfile,
'An exception occurred during expression evaluation, halting further execution',
`An exception occurred during expression evaluation, halting further execution:\n\n${errorMessage}`,
{ node: matchedNode },
);
// Make a note of the failure
Expand Down Expand Up @@ -336,11 +341,6 @@ export async function kernelExecutionTransform(tree: GenericParent, vfile: VFile
// Populate cache if things were successful
if (!errorOccurred) {
opts.cache.set(cacheKey, results);
} else {
// Otherwise, keep tabs on the error
fileError(vfile, 'An error occurred during kernel execution', {
fatal: opts.errorIsFatal,
});
}
// Refer to these computed results
cachedResults = results;
Expand Down
6 changes: 3 additions & 3 deletions packages/myst-execute/tests/execute.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ cases:
text/plain: '3'
metadata: {}
- title: tree with bad inline expression throws
throws: An error occurred during kernel execution
throws: An exception occurred during expression evaluation
before:
type: root
children:
Expand All @@ -108,7 +108,7 @@ cases:
name: expr-0
value: 1 + llama
- title: tree with bad executable code is evaluated and throws
throws: An error occurred during kernel execution
throws: An exception occurred during code execution
before:
type: root
children:
Expand Down Expand Up @@ -233,7 +233,7 @@ cases:
id: T7FMDqDm8dM2bOT1tKeeM
identifier: nb-cell-0-output
html_id: nb-cell-0-output
data:
data:
after:
type: root
children:
Expand Down

0 comments on commit 28642fe

Please sign in to comment.