有没有更好的方法来捕获clojurescript中的javascript错误?

时间:2017-02-01 11:33:27

标签: error-handling interop firebase-authentication clojurescript

我一直在使用clojurescript中的firebase,我在这里尝试了这个建议:

How to catch any Javascript exception in Clojurescript?

我到目前为止的代码是:

(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))))

我怀疑它没有被发现的原因是评估可能是后来发生的,这是为了测试这个想法。

我也尝试过:

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

我用这个来打电话:

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

如果我通过了我已经注册的电子邮件,我会收到auth/email-already-in-use错误,但我无法理解。

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

我如何抓住它?或者是使用原始javascript处理此问题的唯一方法吗?

firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // ...
});

1 个答案:

答案 0 :(得分:2)

Firebase身份验证是异步的。 Javascript(以及一般来说,Clojurescript的)try...catch在回调中不起作用。使用您的回调函数调用Firebase处理程序提供的catch函数,如下所示 - 这是您上面帖子中JS代码的字面翻译:

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