Skip to content

Commit 046c751

Browse files
authored
Merge pull request #272 from developmentseed/fix/error-handling-middleware
Improve request error handling
2 parents 5b9298d + efbe97e commit 046c751

File tree

5 files changed

+47
-13
lines changed

5 files changed

+47
-13
lines changed

app/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ async function init () {
5858
* Error handler
5959
*/
6060
app.use(function (err, req, res, next) {
61+
if (err.message === 'Forbidden') {
62+
return nextApp.render(req, res, '/uh-oh')
63+
}
6164
res.status(err.status || 500)
6265
console.error('error', err)
6366
res.boom.internal('An internal error occurred.')

app/lib/osm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function openstreetmap (req, res) {
8585
hydra.acceptLoginRequest(challenge, {
8686
subject: user.id,
8787
remember: true,
88-
remember_for: 9999
88+
remember_for: 0
8989
}).then(response => {
9090
if (response.redirect_to) {
9191
return res.redirect(response.redirect_to)

app/manage/permissions/clients.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ const db = require('../../db')
1111
* @returns {boolean} can the request go through?
1212
*/
1313
async function clients (uid) {
14-
let conn = await db()
15-
const [user] = await conn('users').where('id', uid)
16-
if (user) {
17-
return true
14+
try {
15+
let conn = await db()
16+
const [user] = await conn('users').where('id', uid)
17+
if (user) {
18+
return true
19+
}
20+
} catch (error) {
21+
throw Error('Forbidden')
1822
}
1923
}
2024

app/manage/permissions/index.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ const permissions = mergeAll([
4343
organizationPermissions
4444
])
4545

46+
function isApiRequest ({ path }) {
47+
return path.indexOf('/api') === 0
48+
}
49+
4650
/**
4751
* Check if a user has a specific permission
4852
*
@@ -159,18 +163,27 @@ function check (ability) {
159163
if (allowed) {
160164
next()
161165
} else {
162-
res.boom.unauthorized('Forbidden')
166+
if (isApiRequest(req)) {
167+
res.boom.unauthorized('Forbidden')
168+
} else {
169+
next(new Error('Forbidden'))
170+
}
163171
}
164172
} catch (e) {
165173
console.error('error checking permission', e)
166-
// An error occurred checking the permissions
167-
// if user id is missing it's an authentication problem
168-
if (e.message.includes('osm id is required')) {
169-
return res.boom.unauthorized('Forbidden')
170-
}
171174

172-
// otherwise it could be the resource not existing, we send 404
173-
res.boom.notFound('Could not find resource')
175+
if (isApiRequest(req)) {
176+
// Handle API request errors
177+
if (e.message.includes('osm id is required')) {
178+
return res.boom.unauthorized('Forbidden')
179+
}
180+
181+
// otherwise it could be the resource not existing, we send 404
182+
res.boom.notFound('Could not find resource')
183+
} else {
184+
// This should be web page errors, which are handled at app/index.js#L60
185+
next(new Error('Forbidden'))
186+
}
174187
}
175188
}
176189
}

pages/uh-oh.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React, { Component } from 'react'
2+
3+
export default class UhOh extends Component {
4+
render () {
5+
return (
6+
<article className='inner page'>
7+
<h1>Page not found</h1>
8+
<div>Sorry, the page you are looking for is not available.</div>
9+
<br />
10+
<div>Still having problems? Try logging out and back in or contacting a system administrator.</div>
11+
</article>
12+
)
13+
}
14+
}

0 commit comments

Comments
 (0)