|
| 1 | +--- |
| 2 | +head: |
| 3 | + - - meta |
| 4 | + - property: 'og:image' |
| 5 | + content: https://docs.sailscasts.com/boring-stack-social.png |
| 6 | +title: Error handling |
| 7 | +titleTemplate: The Boring JavaScript Stack |
| 8 | +description: Error handling in The Boring JavaScript Stack |
| 9 | +prev: |
| 10 | + text: Testing |
| 11 | + link: '/boring-stack/testing' |
| 12 | +next: |
| 13 | + text: Deployment |
| 14 | + link: '/boring-stack/deployment' |
| 15 | +editLink: true |
| 16 | +--- |
| 17 | + |
| 18 | +# Error handling |
| 19 | + |
| 20 | +The Boring JavaScript Stack provides built-in error handling that integrates seamlessly with Inertia.js, including a development error modal that shows stack traces without losing your page state. |
| 21 | + |
| 22 | +## The error modal |
| 23 | + |
| 24 | +When a server error occurs during an Inertia request in development, instead of showing a blank error page, The Boring Stack displays a styled error modal with the full stack trace. This lets you debug issues without losing your current page state or form data. |
| 25 | + |
| 26 | +The error modal is automatically enabled in development (`NODE_ENV !== 'production'`) and shows: |
| 27 | + |
| 28 | +- HTTP status code |
| 29 | +- Error name and message |
| 30 | +- Request method and URL |
| 31 | +- Full stack trace with syntax highlighting |
| 32 | + |
| 33 | +In production, errors redirect back to the previous page with a flash message. |
| 34 | + |
| 35 | +## Using the serverError response |
| 36 | + |
| 37 | +Define the `serverError` exit in your action: |
| 38 | + |
| 39 | +```js |
| 40 | +module.exports = { |
| 41 | + exits: { |
| 42 | + success: { |
| 43 | + responseType: 'inertia' |
| 44 | + }, |
| 45 | + serverError: { |
| 46 | + responseType: 'serverError' |
| 47 | + } |
| 48 | + }, |
| 49 | + |
| 50 | + fn: async function () { |
| 51 | + try { |
| 52 | + const data = await someRiskyOperation() |
| 53 | + return { page: 'dashboard', props: { data } } |
| 54 | + } catch (error) { |
| 55 | + throw { serverError: error } |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +## Direct usage |
| 62 | + |
| 63 | +You can also call `handleServerError` directly: |
| 64 | + |
| 65 | +```js |
| 66 | +module.exports = { |
| 67 | + fn: async function () { |
| 68 | + try { |
| 69 | + await dangerousOperation() |
| 70 | + } catch (error) { |
| 71 | + return sails.inertia.handleServerError(this.req, this.res, error) |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +## Behavior by environment |
| 78 | + |
| 79 | +| Environment | Inertia Request | Non-Inertia Request | |
| 80 | +| ----------- | ------------------------------ | -------------------------------- | |
| 81 | +| Development | Error modal with stack trace | HTML error page with stack trace | |
| 82 | +| Production | Redirect back with flash error | Generic error page | |
| 83 | + |
| 84 | +## Customizing the error modal |
| 85 | + |
| 86 | +The error modal HTML is generated by the `handleServerError` function. If you need to customize it, you can create your own response handler: |
| 87 | + |
| 88 | +```js |
| 89 | +// api/responses/serverError.js |
| 90 | +module.exports = function serverError(error) { |
| 91 | + const sails = this.req._sails |
| 92 | + const isDev = process.env.NODE_ENV !== 'production' |
| 93 | + const isInertia = this.req.header('X-Inertia') |
| 94 | + |
| 95 | + if (isInertia && isDev) { |
| 96 | + // Your custom error HTML |
| 97 | + return this.res.status(500).send(buildCustomErrorHtml(error)) |
| 98 | + } |
| 99 | + |
| 100 | + // Fall back to default handling |
| 101 | + return sails.inertia.handleServerError(this.req, this.res, error) |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +## Global error handling |
| 106 | + |
| 107 | +For catching unhandled errors globally, you can use Sails' `config/http.js` middleware: |
| 108 | + |
| 109 | +```js |
| 110 | +// config/http.js |
| 111 | +module.exports.http = { |
| 112 | + middleware: { |
| 113 | + order: [ |
| 114 | + // ... other middleware |
| 115 | + 'errorHandler' |
| 116 | + ], |
| 117 | + |
| 118 | + errorHandler: function (err, req, res, next) { |
| 119 | + if (req.header('X-Inertia')) { |
| 120 | + return sails.inertia.handleServerError(req, res, err) |
| 121 | + } |
| 122 | + return next(err) |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +## Error handling best practices |
| 129 | + |
| 130 | +1. **Use specific exits**: Define clear exit types for different error scenarios |
| 131 | + |
| 132 | +```js |
| 133 | +exits: { |
| 134 | + success: { responseType: 'inertia' }, |
| 135 | + notFound: { responseType: 'notFound' }, |
| 136 | + badRequest: { responseType: 'badRequest' }, |
| 137 | + serverError: { responseType: 'serverError' } |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +2. **Log errors**: Always log server errors for monitoring |
| 142 | + |
| 143 | +```js |
| 144 | +catch (error) { |
| 145 | + sails.log.error('Payment processing failed:', error) |
| 146 | + throw { serverError: error } |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +3. **User-friendly production errors**: Use flash messages for production |
| 151 | + |
| 152 | +```js |
| 153 | +catch (error) { |
| 154 | + sails.log.error('Operation failed:', error) |
| 155 | + sails.inertia.flash('error', 'Something went wrong. Please try again.') |
| 156 | + return '/dashboard' |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +## Validation errors vs server errors |
| 161 | + |
| 162 | +| Type | Response | Use case | |
| 163 | +| ------------- | -------------------------- | ------------------------ | |
| 164 | +| `badRequest` | 400 + redirect with errors | Form validation failures | |
| 165 | +| `serverError` | 500 + error modal/redirect | Unexpected exceptions | |
| 166 | + |
| 167 | +Use `badRequest` for validation errors (user can fix) and `serverError` for unexpected failures (bugs, external service failures). |
0 commit comments