solidusjs/solidus — pull request #125 by joanniclaborde (2014-11-05 16:15:20+00:00)
Title: 117 handle resource 404s

This PR handles broken resources and preprocessors.

**Resources**

An error will be rendered instead of the requested page if a resource:
- Cannot be retrieved because of a connection error, or the returned JSON is invalid. Returned status code: 500
- Is valid but has a >=300 status code (https://github.com/solidusjs/solidus-client/pull/3 can also change the status code). Returned status code: the resource's status code

Note: if a resource has the `optional=true` option set, then any error described above will be ignored, and the page will render as if the resource never existed.

**Preprocessors**

If a preprocessor throws an exception, a 500 error page will be rendered. Instead of throwing an exception, the preprocessors can now also:
- Return the status code to render instead of the current page: `return 404;`
- Return a url to redirect to (with status code 302): `return '/home';`
- Return an array containing the status code and url to redirect to: `return [301, '/home'];`

**Rendering errors**

When an error occurs, Solidus will first try to find a corresponding error template to use (`views/404.hbs`, `views/500.hbs`, etc.). If the template cannot be found, the standard status code message will be rendered (`404 Not Found`, `500 Internal Server Error`, etc.).

When in `dev` mode, or when a `.json` page is requested, a JSON object will be rendered instead of the error page described above. For example:

```javascript
{
"status": 500,
"error": "Error running preprocessor 'signup_page.js'",
"message": [
"TypeError: Cannot read property 'b' of undefined",
" at module.exports (/vagrant/mysite/preprocessors/signup_page.js:5:10)",
" at module.exports (/vagrant/solidus/lib/preprocessor_worker.js:13:15)",
" at handle (/vagrant/solidus/node_modules/worker-farm/lib/child/index.js:37:8)",
" at process. (/vagrant/solidus/node_modules/worker-farm/lib/child/index.js:43:3)",
" at process.EventEmitter.emit (events.js:98:17)",
" at handleMessage (child_process.js:318:10)",
" at Pipe.channel.onread (child_process.js:345:11)"
],
"context": {
"url": {
"host": "...",
...
```

Fixes #117

Note: this branch is based on #116