Skip to content

Commit

Permalink
error.status
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzzy committed Oct 10, 2018
1 parent 1f50f29 commit ff9f268
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

- [BREAKING] The router no longer mutates errors to avoid issues with non-extensible objects.
- [BREAKING] The router no longer mutate errors to avoid issues with non-extensible objects.
([#158](https://github.com/kriasoft/universal-router/pull/158)).

**Migration from v6 to v7:**
- If your code relies on `error.context` or `error.code` you still can access the same variables using `errorHandler` option:
- If your code relies on `error.context` or `error.code` you still can access them
using `errorHandler` option:
```js
errorHandler(error, context) {
const code = error.message === 'Route not found' ? 404 : 500
console.log(error, context, code)
const code = error.status || 500
return code === 404 ? 'Not found' : 'Error'
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const options = {
errorHandler(error, context) {
console.error(error)
console.info(context)
return error.message === 'Route not found'
return error.status === 404
? '<h1>Page Not Found</h1>'
: '<h1>Oops! Something went wrong</h1>'
}
Expand Down
6 changes: 3 additions & 3 deletions src/UniversalRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import pathToRegexp from 'path-to-regexp'
import matchRoute from './matchRoute'

const notFound = 'Route not found'

function resolveRoute(context, params) {
if (typeof context.route.action === 'function') {
return context.route.action(context, params)
Expand Down Expand Up @@ -76,7 +74,9 @@ class UniversalRouter {
}

if (matches.done) {
return Promise.reject(new Error(notFound))
const error = new Error('Route not found')
error.status = 404
return Promise.reject(error)
}

currentContext = { ...context, ...matches.value }
Expand Down
4 changes: 4 additions & 0 deletions test/UniversalRouter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('new UniversalRouter(routes, options)', () => {
const context = errorHandler.mock.calls[0][1]
expect(error).toBeInstanceOf(Error)
expect(error.message).toBe('Route not found')
expect(error.status).toBe(404)
expect(context.pathname).toBe('/')
expect(context.router).toBe(router)
})
Expand Down Expand Up @@ -85,6 +86,7 @@ describe('router.resolve({ pathname, ...context })', () => {
}
expect(err).toBeInstanceOf(Error)
expect(err.message).toBe('Route not found')
expect(err.status).toBe(404)
})

it("should execute the matching route's action method and return its result", async () => {
Expand Down Expand Up @@ -136,6 +138,7 @@ describe('router.resolve({ pathname, ...context })', () => {
}
expect(err).toBeInstanceOf(Error)
expect(err.message).toBe('Route not found')
expect(err.status).toBe(404)
expect(action.mock.calls.length).toBe(0)
})

Expand Down Expand Up @@ -540,6 +543,7 @@ describe('router.resolve({ pathname, ...context })', () => {
expect(action.mock.calls.length).toBe(1)
expect(err).toBeInstanceOf(Error)
expect(err.message).toBe('Route not found')
expect(err.status).toBe(404)
})

it('should match routes with trailing slashes', async () => {
Expand Down

0 comments on commit ff9f268

Please sign in to comment.