jrburke/almond — pull request #70 by joliss (2013-11-08 19:45:18+00:00)
Title: Add error.code = 'MODULE_NOT_FOUND' on exceptions

This is analogous to Node.

--------------------

Here is the use case: I'm writing a build tool for Ember apps, which makes all modules available synchronously, in a single compiled `app.js` blob. When the Ember app gets loaded in the browser, it would be very useful to be able to detect if certain modules are available, with a function like so:

```js
// Return module or null
function tryRequire(deps) {
try {
return require(deps, null, null, true /* force sync */);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND')
return null;
throw err;
}
}
```

But I don't want to catch errors indiscriminately, and regex matching on the error message seems like a bad idea. So I'd like to check `err.code`.

Node has a similar `.code` property:

```js
try { require('asdf') } catch (e) { console.log(e.code) }
// => MODULE_NOT_FOUND
```

Is a bad idea altogether?

I'm also not entirely sure if both errors should get the same error code (see diff) -- I don't know RequireJS and almond well enough.

Let me know what you think.