Skip to content

Commit 887651d

Browse files
author
Stephen Belanger
authored
Fix Next.js getServerSideProps error handling (#2971)
1 parent 22f7db7 commit 887651d

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

packages/datadog-plugin-next/src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ class NextPlugin extends Plugin {
6868
const span = store.span
6969
const req = this._requests.get(span)
7070

71+
// Only use error page names if there's not already a name
72+
const current = span.context()._tags['next.page']
73+
if (current && (page === '/404' || page === '/500' || page === '/_error')) {
74+
return
75+
}
76+
7177
span.addTags({
7278
[COMPONENT]: this.constructor.id,
7379
'resource.name': `${req.method} ${page}`.trim(),

packages/datadog-plugin-next/test/index.spec.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const getPort = require('get-port')
77
const { execSync, spawn } = require('child_process')
88
const agent = require('../../dd-trace/test/plugins/agent')
99
const { writeFileSync } = require('fs')
10+
const { satisfies } = require('semver')
1011

1112
describe('Plugin', function () {
1213
let server
@@ -209,25 +210,28 @@ describe('Plugin', function () {
209210
.catch(done)
210211
})
211212

213+
const pkg = require(`${__dirname}/../../../versions/next@${version}/node_modules/next/package.json`)
214+
212215
const pathTests = [
213216
['/hello', '/hello'],
214217
['/hello/world', '/hello/[name]'],
215-
['/hello/other', '/hello/other']
218+
['/hello/other', '/hello/other'],
219+
['/error/not_found', '/error/not_found', satisfies(pkg.version, '>=11') ? 404 : 500],
220+
['/error/get_server_side_props', '/error/get_server_side_props', 500]
216221
]
217-
pathTests.forEach(([url, expectedPath]) => {
222+
pathTests.forEach(([url, expectedPath, statusCode]) => {
218223
it(`should infer the corrrect resource (${expectedPath})`, done => {
219224
agent
220225
.use(traces => {
221226
const spans = traces[0]
222227

223228
expect(spans[0]).to.have.property('resource', `GET ${expectedPath}`)
229+
expect(spans[0].meta).to.have.property('http.status_code', `${statusCode || 200}`)
224230
})
225231
.then(done)
226232
.catch(done)
227233

228-
axios
229-
.get(`http://localhost:${port}${url}`)
230-
.catch(done)
234+
axios.get(`http://localhost:${port}${url}`)
231235
})
232236
})
233237

@@ -285,7 +289,7 @@ describe('Plugin', function () {
285289
.catch(done)
286290

287291
axios
288-
.get(`http://localhost:${port}/boom`)
292+
.get(`http://localhost:${port}/api/error/boom`)
289293
.catch((response) => {
290294
expect(response.statusCode).to.eql(500)
291295
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export async function getServerSideProps (ctx) {
2+
throw new Error('fail')
3+
}
4+
5+
export default function GetServerSideProps () {
6+
return (
7+
<div>
8+
Get Server Side Props!
9+
</div>
10+
)
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export async function getServerSideProps (ctx) {
2+
return {
3+
notFound: true
4+
}
5+
}
6+
7+
export default function NotFound () {
8+
return (
9+
<div>
10+
Not found!
11+
</div>
12+
)
13+
}

0 commit comments

Comments
 (0)