tbranyen/backbone.layoutmanager — pull request #405 by jugglinmike (2013-11-08 00:35:03+00:00)
Title: Implement a custom Error

This occurred to me when reviewing #404. It would be easy to simply update the test to ensure that some Error was thrown, but I think we can do better. Not only does this change make writing this test easier, it also makes using LayoutManager easier:

Currently, if a developer has some special logic to recover from (or log) LayoutManager issues, there's no obvious way to do this:

```javascript
function safeInsert(layout, view) {
try {
layout.insertView(view);
} catch (err) {
if (/* how do I write this safely? */) {
// Recover from (or log) the LayoutManager error
} else {
throw err;
}
}
}
```

With a custom Error class, this is simple:
```diff
} catch (err) {
- if (/* how do I write this safely? */) {
+ if (err instanceof Backbone.Layout.Error) {
// Recover from the LayoutManager error
```

I'll admit that it does seem a little unlikely that people will need this for LayoutManager's one and only Error. This also isn't a pattern I've seen in other JavaScript libraries. That said, having it around (and documented) means that we can easily reach for it in the future (where Errors thrown from more dynamic code paths would make it more important).