I
I
Icic Bender2014-11-09 14:23:55
JavaScript
Icic Bender, 2014-11-09 14:23:55

What is the best way to catch errors inside Object.observe()?

Hello.
Errors inside Object.observe() are not displayed to the console, and if you wrap the insides of the callback function in try...catch , then these errors are caught, but without stopping the code execution.
Example without try...catch :

var test = [0,1];

function observer(){
    'use strict';
    a = 0; // должно вызвать ошибку
}

Object.observe(test, observer);
test.push(2); // не вызовет ошибку

Example with try...catch :
var test = [0,1];

function observer(){
    'use strict';
    try {
        a = 0; // должно вызвать ошибку
    } catch (error) {
        console.error(error)
    }
}

observer() // вызовет сообщение console.error(error)

Object.observe(test, observer);
test.push(2); // вызовет сообщение console.error(error)

Interested in: why errors inside Object.observe () do not cause the code to stop and are not displayed in the console? Is it possible to do without try...catch ?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Icic Bender, 2014-11-10
@icw82

I found a commit on the topic: https://github.com/v8/v8/commit/db0a63d2c08a2404a8... → the bug report was back in January 2014: https://code.google.com/p/chromium/issues/detail?i ...
I think in the near future this problem will not become.

A
aen, 2014-11-09
@aen

Isn't it too early for you to start using Object.observe? It's still a draft.

D
Denis Pushkarev, 2014-11-09
@rock

The observer function is executed asynchronously and, accordingly, does not cause the synchronous code to stop.
Google debugging errors in asynchronous code, as I don't understand exactly what you want to achieve. Would Node.js - would advise domains. Promises are poorly suited for debugging errors in events.
If you want something similar, but synchronously - try Proxy . Although they are completely raw, they are far from always suitable for the key.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question