SteveSanderson/knockout — Issue #288: Don't throw bare strings
Author: joewhite
Created: 2012-01-21 20:54:19+00:00
State: open

Most places where Knockout wants to `throw` something, it constructs a `new Error`, e.g.:

throw new Error("Browser doesn't support addEventListener or attachEvent");

However, there are several places that throw a plain string instead of an `Error`, e.g.:

throw "Cannot write a value to a dependentObservable unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters.";

JavaScript allows this, but it has disadvantages -- the biggest one being that, when a string is thrown, no stack trace is generated, at least in Node. (Which makes sense -- `Error` appears to generate its `stack` property when it's constructed, whereas strings do nothing of the sort.) So I might know that I've got some code that's trying to write to a computed observable, but I don't have any good way of knowing where to find that code, or which property it's writing to.

Knockout should consistently use `throw new Error("message")`, and not `throw "message"`.