Skip to content

Commit

Permalink
fix: don't forward error from decodeURI to next middleware
Browse files Browse the repository at this point in the history
Instead of forwarding error from `decodeuRI` further, triggering
error middleware and subsequently 500 response, just ignore it.

Ignoring the error will likely crash in Nuxt later anyway but that
is IMO better as:
 - This package won't get the blame when error shows up locally in production
 - Error triggered within Nuxt doesn't trigger 500 but 400 response
   which is better as this is input error rather than server error.
  • Loading branch information
rchl committed Apr 20, 2020
1 parent a796553 commit 74b5148
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ You can set decode.

### `onDecodeError`

- Default: `(error, req, res, next) => next(error)`
- Default: `(error, req, res, next) => next()`

You can set callback when there is an error in the decode.

Expand Down
2 changes: 1 addition & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ async function redirectModule (moduleOptions) {
const defaults = {
rules: [],
onDecode: (req, res, next) => decodeURI(req.url),
onDecodeError: (error, req, res, next) => next(error),
onDecodeError: (_error, _req, _res, next) => next(),
statusCode: 302
}

Expand Down
9 changes: 9 additions & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ const testSuite = () => {
expect(html).toContain('ab')
})

test('redirect with malformed URI', async () => {
await expect(get('/%83')).rejects.toMatchObject({
statusCode: 400
})
})

test('redirect error due to malformatted target url', async () => {
const requestOptions = {
uri: url('/errorInTo'),
Expand Down Expand Up @@ -195,6 +201,9 @@ describe('error', () => {
if (req.url === '/error') {
throw e
}
},
onDecodeError: (error, _req, _res, next) => {
next(error)
}
}
})
Expand Down

0 comments on commit 74b5148

Please sign in to comment.