Cedric Martin
Cedric Martin

Reputation: 6014

Customizing repl/nrepl error messages

There's an error I keep doing in Clojure once in a while and I don't do it often enough yet to know immediately what I forgot but I still do it often enough that it gets really annoying.

Basically after using nrepl-jack-in, I often forgot to change to the correct namespace.

So at the nrepl user> prompt I enter a function and I get:

CompilerException java.lang.RuntimeException: Unable to resolve symbol: foo-bar-baz in this context, compiling:(NO_SOURCE_PATH:1)

printed in the nrepl buffer before giving me back the user> prompt.

So I figured out that instead of trying to mess up with Java / Clojure there was probably an easy way to give an hint directly from Emacs, using only some Emacs magic.

How can I add a hook (?) or something similar to Emacs / nrepl-mode so that when a specific message gets printed (like, say, a message containing "Unable to resolve symbol" and "in this context") I could add one line saying something like:

"Didn't you forget to change namespace?"

or even:

"There's a .clj buffer opened using namespace abc.defk, didn't you forget to enter: (ns  abc.def) ?"

There are quite some error messages which I find quite cryptic in Clojure and it usually takes me a while to figure out what I'm doing wrong. If I could "personalize" these error messages directly from Emacs I'd win quite some time.

So how can I "intercept" nrepl outputs and tailor them to my needs?

Upvotes: 0

Views: 176

Answers (1)

Bozhidar Batsov
Bozhidar Batsov

Reputation: 56655

You'll have to alter nrepl-default-err-handler in nrepl.el to achieve the desired effect. Alternative you can advice (with defadvice) nrepl-default-err-handler to replace its output if it matches a certain pattern. Doing this from nrepl.el doesn't seem particularly good idea to me, though. Maybe a nREPL middleware would be a better approach.

Upvotes: 1

Related Questions