Skip to content

Commit ef9f9e2

Browse files
authored
Merge pull request #8 from geodatagouv/tusbar/improve-error-handling
Improve error handling and upgrade @sentry/node
2 parents 73ac583 + caeed89 commit ef9f9e2

File tree

8 files changed

+57
-47
lines changed

8 files changed

+57
-47
lines changed

lib/api/errors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Http500 extends HttpError {
4141
}
4242

4343
module.exports = {
44+
HttpError,
4445
Http400,
4546
Http404,
4647
Http500

lib/api/index.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const cors = require('cors')
55

66
const sentry = require('../sentry')
77

8-
const {Http404} = require('./errors')
8+
const {HttpError, Http404} = require('./errors')
99
const links = require('./providers/links')
1010
const services = require('./providers/services')
1111

@@ -36,17 +36,15 @@ app.use((req, res, next) => {
3636

3737
app.use(sentry.Handlers.errorHandler())
3838
app.use((error, req, res, next) => { // eslint-disable-line no-unused-vars
39-
const {statusCode = 500} = error
40-
41-
if (typeof error.toJSON === 'function') {
42-
return res.status(statusCode).send(error.toJSON())
39+
if (error instanceof HttpError) {
40+
res.status(error.statusCode).send(error.toJSON())
41+
} else {
42+
res.status(500).send({
43+
code: 500,
44+
sentry: res.sentry,
45+
error: 'An unexpected error happened'
46+
})
4347
}
44-
45-
res.status(statusCode).send({
46-
code: statusCode,
47-
sentry: res.sentry,
48-
error: 'An unexpected error happened'
49-
})
5048
})
5149

5250
module.exports = app

lib/api/middlewares/error.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const {HttpError, Http500} = require('../errors')
2+
3+
module.exports = (error, req, res, next) => {
4+
if (error instanceof HttpError) {
5+
next(error)
6+
} else {
7+
next(new Http500('Failed transcoding dataset'))
8+
}
9+
}

lib/api/middlewares/ogr2ogr.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const ogr2ogr = require('ogr2ogr')
33
const ms = require('ms')
44

55
const got = require('../../got')
6-
const {Http400, Http500} = require('../errors')
6+
const {Http400} = require('../errors')
77

88
const defaultTimeout = ms('20m')
99

@@ -98,7 +98,7 @@ function getOptions(opts) {
9898
}
9999

100100
function downloadDataset(input, opts) {
101-
return (req, res, next) => {
101+
return (req, res) => {
102102
const projection = getProjection(req.query.projection)
103103
if (!projection) {
104104
throw new Http400('No valid projection given')
@@ -135,11 +135,7 @@ function downloadDataset(input, opts) {
135135
ogr._inDriver.output = opts.format
136136
}
137137

138-
ogr.stream().pipe(res).on('error', () => {
139-
if (!res.headersSent) {
140-
next(new Http500('Failed transcoding dataset'))
141-
}
142-
})
138+
ogr.stream().pipe(res)
143139
}
144140
}
145141

lib/api/providers/links/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const {Router} = require('express')
22

33
const am = require('../../middlewares/async')
44
const ogr2ogr = require('../../middlewares/ogr2ogr')
5+
const errorHandler = require('../../middlewares/error')
56
const {Http404} = require('../../errors')
67

78
const {getLink} = require('./link-proxy')
@@ -32,4 +33,6 @@ router.get(
3233
})
3334
)
3435

36+
router.use(errorHandler)
37+
3538
module.exports = router

lib/api/providers/services/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const splitOnFirst = require('split-on-first')
33

44
const am = require('../../middlewares/async')
55
const ogr2ogr = require('../../middlewares/ogr2ogr')
6+
const errorHandler = require('../../middlewares/error')
67
const {Http404} = require('../../errors')
78

89
const {getFeatureType} = require('./geoplatform')
@@ -31,4 +32,6 @@ router.get(
3132
})
3233
)
3334

35+
router.use(errorHandler)
36+
3437
module.exports = router

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"author": "Bertrand Marron <[email protected]>",
99
"license": "MIT",
1010
"dependencies": {
11-
"@sentry/node": "^4.5.3",
11+
"@sentry/node": "^4.5.4",
1212
"cors": "^2.8.5",
1313
"dotenv": "^6.2.0",
1414
"express": "^4.16.4",

yarn.lock

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,44 +31,44 @@
3131
resolved "https://registry.yarnpkg.com/@nodelib/fs.stat/-/fs.stat-1.1.3.tgz#2b5a3ab3f918cca48a8c754c08168e3f03eba61b"
3232
integrity sha512-shAmDyaQC4H92APFoIaVDHCx5bStIocgvbwQyxPRrbUY20V1EYTbSDchWbuwlMG3V17cprZhA6+78JfB+3DTPw==
3333

34-
"@sentry/[email protected].3":
35-
version "4.5.3"
36-
resolved "https://registry.yarnpkg.com/@sentry/core/-/core-4.5.3.tgz#e55a42c694deec903aec2479e4a282d5a7a43878"
37-
integrity sha512-off7XkyyiPXJTQ1XYPzxsY3zLHztMdK1kuIeHzyoIzu4kvs8H75eqou+fADo04JmfaMNy+OZSoW+5Aq2SZR2Ng==
34+
"@sentry/[email protected].4":
35+
version "4.5.4"
36+
resolved "https://registry.yarnpkg.com/@sentry/core/-/core-4.5.4.tgz#31c9a0b66d33cbd5b4d7de2b69d6c7951c7c06a3"
37+
integrity sha512-Nj5VZjAVntroHn9tuqez8KnRgv91KWQfESzN9/DA/k9orZ8vI4+8bm78Zpq6IteumaxLxGdXw8H1s8xVGlU3/Q==
3838
dependencies:
39-
"@sentry/hub" "4.5.3"
40-
"@sentry/minimal" "4.5.3"
39+
"@sentry/hub" "4.5.4"
40+
"@sentry/minimal" "4.5.4"
4141
"@sentry/types" "4.5.3"
42-
"@sentry/utils" "4.5.3"
42+
"@sentry/utils" "4.5.4"
4343
tslib "^1.9.3"
4444

45-
"@sentry/[email protected].3":
46-
version "4.5.3"
47-
resolved "https://registry.yarnpkg.com/@sentry/hub/-/hub-4.5.3.tgz#54ce5988ac6dedbe9bba99e2c00f8f26eb677dfe"
48-
integrity sha512-4b8oPVAGw/hf11CQN9J7hAk2wzD2HIZsQBl/PcB/p5r1UyHw8cibpVobJulkHJMBJMiZ/twIBGZ1G1qK3LJdhw==
45+
"@sentry/[email protected].4":
46+
version "4.5.4"
47+
resolved "https://registry.yarnpkg.com/@sentry/hub/-/hub-4.5.4.tgz#7e0980dd2f3ead60ce1393e4e108fd4fe65e40b7"
48+
integrity sha512-ghvfydSTDHh6pVuLpcubI6ksELplStORhsklOSw+fq7HtDPWNyr9Cv2USgd341whnURXvuT+TQuPnZnOMuBqSg==
4949
dependencies:
5050
"@sentry/types" "4.5.3"
51-
"@sentry/utils" "4.5.3"
51+
"@sentry/utils" "4.5.4"
5252
tslib "^1.9.3"
5353

54-
"@sentry/[email protected].3":
55-
version "4.5.3"
56-
resolved "https://registry.yarnpkg.com/@sentry/minimal/-/minimal-4.5.3.tgz#7b69481df2d61e4b935d067be62a52824190f616"
57-
integrity sha512-W9+eEZosoDKTAJkab/bxsSRim7I4lqeWHLhmCchDimKcdmpBzC+gOCT0PywwOWRLW08LhTHfmnNSAJv6PaZiCA==
54+
"@sentry/[email protected].4":
55+
version "4.5.4"
56+
resolved "https://registry.yarnpkg.com/@sentry/minimal/-/minimal-4.5.4.tgz#9c3203feae39e4172fdf4c7d34bc650383555a66"
57+
integrity sha512-KuR7PmpGQcOLkMV7IMXo9R4EjkPoQYsLRf5WkswacKnaFNLwrfxU0/0Mv+eyY/JqRN8+vewPQ1hKkAWsbzBtqg==
5858
dependencies:
59-
"@sentry/hub" "4.5.3"
59+
"@sentry/hub" "4.5.4"
6060
"@sentry/types" "4.5.3"
6161
tslib "^1.9.3"
6262

63-
"@sentry/node@^4.5.3":
64-
version "4.5.3"
65-
resolved "https://registry.yarnpkg.com/@sentry/node/-/node-4.5.3.tgz#f6b57747991c300d4f5f70add673ea3d1e78c873"
66-
integrity sha512-glBIGsoYDcUEjZWDjbkDWt87cmuO/lcxpfz4zfTq9JfIfzI3WunJqABhFDc8tQSqUdHOlWb2QKvoNPvz+0EDfg==
63+
"@sentry/node@^4.5.4":
64+
version "4.5.4"
65+
resolved "https://registry.yarnpkg.com/@sentry/node/-/node-4.5.4.tgz#c6963b519fe3e21b8c3aa6b94526ebe60fec8dbd"
66+
integrity sha512-JXMbbB+gNzomyJIoq4nVHRP3URVDb0eta5DTsIxM6ZVf7fOgHt+yrOQ7FAOz3qzT68xxY9wlGRxAnPEsiqPIXA==
6767
dependencies:
68-
"@sentry/core" "4.5.3"
69-
"@sentry/hub" "4.5.3"
68+
"@sentry/core" "4.5.4"
69+
"@sentry/hub" "4.5.4"
7070
"@sentry/types" "4.5.3"
71-
"@sentry/utils" "4.5.3"
71+
"@sentry/utils" "4.5.4"
7272
"@types/stack-trace" "0.0.29"
7373
cookie "0.3.1"
7474
https-proxy-agent "2.2.1"
@@ -82,10 +82,10 @@
8282
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-4.5.3.tgz#3350dce2b7f9b936a8c327891c12e3aef7bd8852"
8383
integrity sha512-7ll1PAFNjrBNX9rzy3P2qAQrpQwHaDO3uKj735qsnGw34OtAS8Xr8WYrjI14f9fMPa/XIeWvMPb4GMic28V/ag==
8484

85-
"@sentry/[email protected].3":
86-
version "4.5.3"
87-
resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-4.5.3.tgz#6893fca9fde230b230360779362752e9b5570852"
88-
integrity sha512-yIYqnuq9cd9nAUJ2MPmq++Mgy81qB2fglsDB8TiBfk2eaba79qFcKp8xg3w3EMDHZMlfmlx4fkTmZJ/+V02oWQ==
85+
"@sentry/[email protected].4":
86+
version "4.5.4"
87+
resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-4.5.4.tgz#fc074b0c16850f39685a6c17f96407f56ed36b03"
88+
integrity sha512-LhWnCuTEzmbQKyc0qyahs5PYPpj9qkY3BpMOyEIoYz79RU5lMD1iPR62wG8cqO09JmrCHuqfWeQIQIwUFQTaBQ==
8989
dependencies:
9090
"@sentry/types" "4.5.3"
9191
tslib "^1.9.3"

0 commit comments

Comments
 (0)