0

I've been working with firebase in clojurescript and I've attempted the advice here:

How to catch any Javascript exception in Clojurescript?

The code I have so far is:

(defn register [email password]
  (try
    (let [reg (.createUserWithEmailAndPassword (.auth firebase) email password)
          _ (.log js/console reg)]
       reg)
      (catch :default e
        (.log js/console "Register Error:" e))))

As I suspected the reason it's not been caught is that the evaluation could have been occurring later and this was to test the idea.

I have also tried:

(defn register [email password]
  (try
    (.createUserWithEmailAndPassword (.auth firebase) email password)
      (catch :default e
        (.log js/console "Register Error:" e))))

I call this using:

(apply register ((juxt :email :password) @app-state))

If I pass it an email I've already registered with I get as expected an auth/email-already-in-use error, but I can't catch it.

www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=AIzaSyDzsfz98Y6Pjl1n-uAzfI6GHWKqShFdRI4:1
POST https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=AIzaSyDzsfz98Y6Pjl1n-uAzfI6GHWKqShFdRI4 400 ()

firebase.inc.js:81
Uncaught
  Qcode: "auth/email-already-in-use"
  message: "The email address is already in use by another account."
  __proto__: Error
(anonymous) @ firebase.inc.js:81

How do I catch it? Or is the only way of handling this by using the raw javascript?

firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // ...
});
Community
  • 1
  • 1
toofarsideways
  • 3,956
  • 2
  • 31
  • 51

1 Answers1

3

Firebase auth is asynchronous. Javascript's (and, generally, Clojurescript's) try...catch does not work in callbacks. Call the catch function provided by the Firebase handler with your callback on error, like this - this is the literal translation of the JS code in your post above:

(defn register [email password]
  (.. firebase auth (createUserWithEmailAndPassword email password)
      (catch (fn [e] (.log js/console "Register Error:" e))))
Aleph Aleph
  • 5,215
  • 2
  • 13
  • 28