Skip to content

Commit

Permalink
Merge pull request #2 from ikappaki/imprv/exceptions-better
Browse files Browse the repository at this point in the history
Improved Exception formatting
  • Loading branch information
ikappaki authored Jun 15, 2024
2 parents f7e8250 + b7efe29 commit 144ff87
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Optimized output to stdout/stderr by removing proxy string intermediary.
- Improved Exception formatting (requires basilisp>=0.1.0b2).

## 1.0.0

Expand Down
13 changes: 12 additions & 1 deletion basilisp_kernel/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from basilisp.lang import reader as reader
from basilisp.lang import runtime as runtime
from basilisp.lang import symbol as sym
from basilisp.lang.exception import format_exception

import re
from typing import Any, Callable, Optional, Sequence, Type
Expand All @@ -27,7 +28,17 @@ def do_execute(code):
runtime.Var.find_safe(sym.symbol("*out*", ns=runtime.CORE_NS)) : sys.stdout,
runtime.Var.find_safe(sym.symbol("*err*", ns=runtime.CORE_NS)) : sys.stderr
}):
return cli.eval_str(code, ctx, user_ns, eof)
try:
return cli.eval_str(code, ctx, user_ns, eof)
except reader.SyntaxError as e:
msg = "".join(format_exception(e, reader.SyntaxError, e.__traceback__))
raise reader.SyntaxError(msg) from None
except compiler.CompilerException as e:
msg = "".join(format_exception(e, compiler.CompilerException, e.__traceback__))
raise compiler.CompilerException(msg, phase=e.phase, filename=e.filename) from None
except Exception as e:
msg = "".join(format_exception(e, Exception, e.__traceback__))
raise Exception(msg) from None


class BasilispKernel(IPythonKernel):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ requires-python = ">=3.8"
dependencies = [
"ipykernel",
"jupyter_client",
"basilisp"
"basilisp>=0.1.0b2"
]

[project.optional-dependencies]
Expand Down
13 changes: 13 additions & 0 deletions test_basilisp.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ class BasilispKernelTests(jkt.KernelTests):
{"code": "(map #(* % %) (range 1 4))", "result": "(1 4 9)"},
]

def test_basilisp_kernel_exceptions(self):
reply, output_msgs = self.execute_helper(code='#lmn')
self.assertEqual("SyntaxError", output_msgs[0]['content']['ename'])
self.assertIn("message: No data reader found for tag #lmn", output_msgs[0]['content']['evalue'])

reply, output_msgs = self.execute_helper(code='(xyz)')
self.assertEqual("CompilerException", output_msgs[0]['content']['ename'])
self.assertIn("message: unable to resolve symbol 'xyz' in this context", output_msgs[0]['content']['evalue'])

reply, output_msgs = self.execute_helper(code='(/ 2 0)')
self.assertEqual("Exception", output_msgs[0]['content']['ename'])
self.assertIn("Traceback (most recent call last):", output_msgs[0]['content']['evalue'])

# code which generates some sort of rich output
# for each `code` input a single rich display object with the specified
# `mime` type should be sent to the frontend
Expand Down

0 comments on commit 144ff87

Please sign in to comment.