Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE when using :handlers instead of meaningful error message #50

Open
borkdude opened this issue May 17, 2021 · 3 comments
Open

NPE when using :handlers instead of meaningful error message #50

borkdude opened this issue May 17, 2021 · 3 comments

Comments

@borkdude
Copy link

borkdude commented May 17, 2021

It seems transit is giving an NPE instead of a meaningful error message when using :handlers and you give it a type it can't handle:

clojure -Sdeps '{:deps {com.cognitect/transit-clj {:mvn/version "RELEASE"}}}' -M /tmp/transit.clj
(require '[cognitect.transit :as transit])

(def ldt-write-handler (transit/write-handler "pod.babashka.sql/local-date-time" str))

(defn write-transit [v]
  (let [baos (java.io.ByteArrayOutputStream.)]
    (transit/write (transit/writer baos :json {:handlers {java.time.LocalDateTime ldt-write-handler}}) v)
    (.toString baos "utf-8")))

(write-transit (into-array String ["foo"]))
Syntax error (NullPointerException) compiling at (/tmp/transit.clj:19:1).
null
@puredanger
Copy link
Contributor

Can you drop (pst *e) at that point too?

@borkdude
Copy link
Author

user=> (load-file "/tmp/array.clj")
"[\"~#pod.babashka.sql/array\",[\"foo\"]]"
Syntax error (NullPointerException) compiling at (/tmp/array.clj:10:51).
null
user=> (pst *e)
Note: The following stack trace applies to the reader or compiler, your code was not executed.
CompilerException Syntax error compiling at (/tmp/array.clj:10:51). #:clojure.error{:phase :compile-syntax-check, :line 10, :column 51, :source "/tmp/array.clj"}
	clojure.lang.Compiler.load (Compiler.java:7648)
	clojure.lang.Compiler.loadFile (Compiler.java:7574)
	clojure.lang.RT$3.invoke (RT.java:327)
	user/eval5 (NO_SOURCE_FILE:1)
	user/eval5 (NO_SOURCE_FILE:1)
	clojure.lang.Compiler.eval (Compiler.java:7177)
	clojure.lang.Compiler.eval (Compiler.java:7132)
	clojure.core/eval (core.clj:3214)
	clojure.core/eval (core.clj:3210)
	clojure.main/repl/read-eval-print--9086/fn--9089 (main.clj:437)
	clojure.main/repl/read-eval-print--9086 (main.clj:437)
	clojure.main/repl/fn--9095 (main.clj:458)
Caused by:
RuntimeException java.lang.NullPointerException
	com.cognitect.transit.impl.WriterFactory$1.write (WriterFactory.java:65)
	cognitect.transit/write (transit.clj:171)
	cognitect.transit/write (transit.clj:168)
	user/write-transit (array.clj:7)
	user/write-transit (array.clj:5)
	user/eval294 (array.clj:11)
	user/eval294 (array.clj:11)
	clojure.lang.Compiler.eval (Compiler.java:7177)
	clojure.lang.Compiler.load (Compiler.java:7636)
	clojure.lang.Compiler.loadFile (Compiler.java:7574)
Caused by:
NullPointerException
	com.cognitect.transit.impl.AbstractEmitter.marshalTop (AbstractEmitter.java:203)
	com.cognitect.transit.impl.JsonEmitter.emit (JsonEmitter.java:41)
	com.cognitect.transit.impl.WriterFactory$1.write (WriterFactory.java:62)
	cognitect.transit/write (transit.clj:171)

@myguidingstar
Copy link

@borkdude the above code will work if you provide the a default handler like this:

(deftype DefaultHandler []
  WriteHandler
  (tag [this v] "unknown")
  (rep [this v] (pr-str v)))

(defn write-transit [v]
  (let [baos (java.io.ByteArrayOutputStream.)
        default-handler (transit/write-handler
                          (fn [_] "atom")
                          (fn [a] @a))]
    (transit/write (transit/writer baos :json {:default-handler (DefaultHandler.)
                                               :handlers {java.time.LocalDateTime ldt-write-handler}}) v)
    (.toString baos "utf-8")))

(write-transit (into-array String ["foo"]))
;; => ["~#unknown" "#object[\"[Ljava.lang.String;\" 0x711ae4e6 \"[Ljava.lang.String;@711ae4e6\"]"]

I guess by design you have to provide :default-handler to writer for now. That being said I agree that NPE is bad dev experience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants