Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
fix: return HTTP 500 on unhandled errors during SSR
Browse files Browse the repository at this point in the history
Fix #133. Add regression tests.
  • Loading branch information
IlyaSemenov committed Dec 8, 2018
1 parent 051a673 commit d488fe2
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ class Ream extends Event {
}
}

res.status(500)
if (this.options.dev) {
res.end(err.stack)
} else {
Expand Down
28 changes: 28 additions & 0 deletions tap-snapshots/test-projects-errors-index.test.js-TAP.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* IMPORTANT
* This snapshot file is auto-generated, but designed for humans.
* It should be checked into source control and tracked carefully.
* Re-generate by setting TAP_SNAPSHOT=1 and running tests.
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[
`test/projects/errors/index.test.js TAP test/projects/errors server-side 404 > undefined 1`
] = `
<!DOCTYPE html><html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui" />
<title data-ream-head="true"></title>
<link rel="preload" href="/_ream/chunk-vendors.js" as="script"><link rel="preload" href="/_ream/client.js" as="script"><link rel="prefetch" href="/_ream/chunk--base-pages-error.js"><link rel="prefetch" href="/_ream/chunk--base-pages-index.js">
</head>
<body>
<div id="_ream" data-server-rendered="true"><div><h1>404: page not found</h1></div></div>
<script>window.__REAM__={"initialData":{}}</script><script src="/_ream/chunk-vendors.js" defer></script><script src="/_ream/client.js" defer></script>
</body>
</html>
`
28 changes: 28 additions & 0 deletions test/projects/errors/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const testProject = require('../../lib/testProject')

testProject(__dirname, async (t, c) => {
await t.test('server-side 404', async t => {
const res = await c.axios.get('/not-found', {
validateStatus: status => status === 404
})
t.matchSnapshot(res.data)
})

await t.test('client-side 404', async t => {
const page = await c.loadPage('/')
await page.waitForXPath('//a[text()="Not Found"]').then(el => el.click())
await page
.waitForSelector('#_ream')
.then(el => page.evaluate(el => el.innerText, el))
.then(text => t.equal(text, '404: page not found'))
})

await t.test('server-side unhandled error', async t => {
const res = await c.axios.get('/error', {
validateStatus: status => status === 500
})
t.equal(res.data, 'server error')
})

// Don't test unhandled client-side error for now, as it's (ha-ha) not handled.
})
12 changes: 12 additions & 0 deletions test/projects/errors/pages/error.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<template>
<div>test</div>
</template>

<script>
export default {
getInitialData ({ req }) {
const type = req ? 'server' : 'client'
throw new Error(`unhandled ${type}-side error`)
}
}
</script>
6 changes: 6 additions & 0 deletions test/projects/errors/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<template>
<div>
<router-link to="/not-found">Not Found</router-link>
<router-link to="/error">Error</router-link>
</div>
</template>
3 changes: 3 additions & 0 deletions test/projects/errors/ream.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
fsRoutes: true
}

0 comments on commit d488fe2

Please sign in to comment.