Skip to content

Commit

Permalink
[pyro] Add support for custom whitelists
Browse files Browse the repository at this point in the history
This commit adds a new option to Pyro to enable you to specify a
whitelist regular expression that will be used to filter stacktrace
frames on the basis of their namespace or className.
  • Loading branch information
venantius committed Mar 8, 2018
1 parent 35200c6 commit 3a6ce76
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.1.2
* Add better support for Clojure 1.8.0+ by fixing initialization to hook into clojure.test properly instead of being reset by the clojure.stacktrace import.
* Added support for custom whitelists.

# 0.1.1
* Exclude hiccup and garden as transitive dependencies for Glow since they're large dependencies and Pyro doesn't rely on any of Glow's HTML/CSS features.

Expand Down
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,37 @@ You can sub Pyro's exception printer in for Clojure's default stacktrace invocat
(printer/swap-stacktrace-engine!)
```

`swap-stacktrace-engine!` can also be provided with an options map with the following options:
### Configuration

* `:show-source`
* `:drop-nrepl-elements`
* `:hide-clojure-elements`
* `:hide-lein-elements`
`swap-stacktrace-engine!` can be provided with an options map with the following options:

By default, all of these options are set to true.
```clojure
{:show-source true
:drop-nrepl-elements true
:hide-clojure-elements true
:hide-lein-elements true
:ns-whitelist nil}
```

##### `:show-source`

Boolean. If true, prints syntax-highlighted source code as part of the stacktrace.

##### `:drop-nrepl-elements`

Boolean. If true, drops all stackframe elements until a `clojure.main/repl/read-eval-print` frame.

##### `:hide-clojure-elements`

Boolean. If true, removes all stacktrace frames belonging to `clojure.core`, `clojure.lang`, etc.

##### `:hide-lein-elements`

Boolean. If true, removes all stacktrace frames belonging to `leiningen.core.eval`, `leiningen.test`, and `leiningen.core.main`.

##### `:ns-whitelist`

A regex literal. If present, will filter for only those stacktrace frames where the namespace matches one or more of the regular expressions.

## Special Thanks

Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject venantius/pyro "0.1.1"
(defproject venantius/pyro "0.1.2"
:description "Pyro: light up your stacktraces"
:url "https://github.com/venantius/pyro"
:license {:name "Eclipse Public License"
Expand Down
11 changes: 10 additions & 1 deletion src/pyro/stacktrace.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,21 @@
(remove #(element/is-lein-element? (.getClassName %)) st)
st))

(defn filter-ns-whitelist
"Given a seq of stacktrace element maps, remove all elements that don't
fit the whitelist."
[st {:keys [ns-whitelist]}]
(if ns-whitelist
(filter #(element/matches-whitelist? ns-whitelist (.getClassName %)) st)
st))

(defn clean-stacktrace
"Clean up our stacktrace."
[st opts]
{:added "0.1.0"}
(let [filtered-elements (-> st
(filter-repl opts)
(remove-clojure opts)
(remove-leiningen opts))]
(remove-leiningen opts)
(filter-ns-whitelist opts))]
(map st/parse-trace-elem filtered-elements)))
8 changes: 8 additions & 0 deletions src/pyro/stacktrace/element.clj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
#"(leiningen\.core\.eval.*|leiningen\.core\.main.*|leiningen\.test.*)"
classname))

(defn matches-whitelist?
"Does this stacktrace element match one of the regular expressions in the
whitelist?"
[whitelist classname]
(re-matches
whitelist
classname))

(defn element->map
"Take a stacktrace element and turn it into a map"
{:added "0.1.0"}
Expand Down
8 changes: 8 additions & 0 deletions test/pyro/stacktrace/element_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
(is (nil? (element/is-read-eval-print-element?
"pyro.core-test/a-test"))))

(deftest matches-whitelist?-works
(is (some? (element/matches-whitelist?
#"(gateway\..*)"
"gateway.middleware.auth-test/fn")))
(is (nil? (element/matches-whitelist?
#"(gateway\..*)"
"common.http.client/post"))))

(deftest element->map-works
(try
(contains? 5 5)
Expand Down

0 comments on commit 3a6ce76

Please sign in to comment.