-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added rudimentary support for basilisp.stacktrace/print-cause-trace (#…
…730) Hi, could you please review patch to introduce a new namespace `basilisp.stacktrace` and a `print-cause-trace`. It partly addresses #721. This is a requirement of the upcoming nbb nrepl-server port implemented in #723. CIDER specifically requests this function when an exception is thrown to display to the user, making it essential. thanks Co-authored-by: ikappaki <[email protected]>
- Loading branch information
Showing
3 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
(ns basilisp.stacktrace | ||
"Prints stacktraces." | ||
(:require [basilisp.string :as str]) | ||
(:import [traceback :as tb])) | ||
|
||
(defn print-cause-trace | ||
"Prints the stacktrace of chained ``exc`` (cause), using ``n`` stack | ||
frames (defaults to all)." | ||
([exc] | ||
(print-cause-trace exc nil)) | ||
([exc n] | ||
(print (str/join " " (tb/format_exception (python/type exc) exc (.-__traceback__ exc) | ||
** :limit n :chain true))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
(ns tests.basilisp.stacktrace-test | ||
(:require [basilisp.stacktrace :as s] | ||
[basilisp.string :as str] | ||
[basilisp.test :refer [deftest are is testing]])) | ||
|
||
|
||
(defn- exception-test [] | ||
(/ 5 0)) | ||
|
||
(deftest stacktrace-basic | ||
(try | ||
(exception-test) | ||
(catch python/Exception e | ||
;; one stack frame | ||
(let [trace1 (-> (with-out-str (s/print-cause-trace e 1)) | ||
(str/split-lines))] | ||
(is (= 4 (count trace1)) trace1) | ||
(is (= "Traceback (most recent call last):" (first trace1))) | ||
(is (= [" (try" " ZeroDivisionError: Fraction(5, 0)" ] (take-last 2 trace1)))) | ||
|
||
;; full stack | ||
(let [trace (-> (with-out-str (s/print-cause-trace e)) | ||
(str/split-lines))] | ||
(is (< 4 (count trace)) trace) | ||
(is (= "Traceback (most recent call last):" (first trace))) | ||
(is (= [" raise ZeroDivisionError('Fraction(%s, 0)' % numerator)" | ||
" ZeroDivisionError: Fraction(5, 0)" ] (take-last 2 trace))))))) |