Skip to content

Commit

Permalink
Throw an error if no route is found (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzzy authored Oct 20, 2016
1 parent c08ae00 commit a26b222
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 65 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.

### [v2.0.0] - [unreleased]

- Throws an error if no route found ([#62](/~https://github.com/kriasoft/universal-router/pull/62))
- Remove obsolete `context.end()` method ([#60](/~https://github.com/kriasoft/universal-router/pull/60))
- Remove obsolete `match` alias for `resolve` function ([#59](/~https://github.com/kriasoft/universal-router/pull/59))
- Do not throw an error for malformed URI params ([#54](/~https://github.com/kriasoft/universal-router/pull/54))
Expand Down
10 changes: 5 additions & 5 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ resolve(routes, { path: '/one' })
// => Page One
```

..where `action` is just a regular function that may, or may not, return any arbitrary data
Where `action` is just a regular function that may, or may not, return any arbitrary data
— a string, a React component, anything!


### Nested Routes
## Nested Routes

Each route may have an optional `children: [ ... ]` property containing the list of child routes:

Expand Down Expand Up @@ -67,7 +67,7 @@ resolve(routes, { path: '/admin/users/john' })
```


### URL Parameters
## URL Parameters

**Named route parameters** are captured and added to `context.params`.

Expand Down Expand Up @@ -104,7 +104,7 @@ and works the same way as the routing solutions in many popular JavaScript frame
Also check out online [router tester](http://forbeslindesay.github.io/express-route-tester/).


### Context
## Context

In addition to a URL path string, any arbitrary data can be passed to the router's `resolve()` method,
that becomes available inside action methods.
Expand All @@ -125,7 +125,7 @@ resolve(routes, { path: '/hello', user: 'admin' })
```


### Async Routes
## Async Routes

The router works great with asynchronous functions out of the box!

Expand Down
8 changes: 5 additions & 3 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import { resolve } from 'universal-router';

const routes = [
{ path: '/one', action: () => '<h1>Page One</h1>' },
{ path: '/two', action: () => '<h1>Page Two</h1>' }
{ path: '/two', action: () => '<h1>Page Two</h1>' },
{ path: '*', action: () => '<h1>Not Found</h1>' }
];

resolve(routes, { path: '/one' }).then(result => {
document.body.innerHTML = result || <h1>Not Found</h1>;
document.body.innerHTML = result;
// renders: <h1>Page One</h1>
});
```
Expand All @@ -44,7 +45,8 @@ import { resolve } from 'universal-router';

const routes = [
{ path: '/one', action: () => <h1>Page One</h1> },
{ path: '/two', action: () => <h1>Page Two</h1> }
{ path: '/two', action: () => <h1>Page Two</h1> },
{ path: '*', action: () => <h1>Not Found</h1> }
];

resolve(routes, { path: '/one' }).then(component => {
Expand Down
1 change: 1 addition & 0 deletions src/matchRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function* matchRoute(route, baseUrl, path, parentParams) {

if (route.children) {
match = matchBasePath(route.path, path, parentParams);

if (match) {
yield {
route,
Expand Down
10 changes: 9 additions & 1 deletion src/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ async function resolve(routes, pathOrContext) {

context.next = next;

return await next();
await next();

if (result === null || result === undefined) {
const error = new Error('Page not found');
error.status = error.statusCode = 404;
throw error;
}

return result;
}

export default resolve;
13 changes: 13 additions & 0 deletions test/matchRoute.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ describe('matchRoute(route, baseUrl, path)', () => {
expect(result).to.have.lengthOf(0);
});

it('should match 0 routes (3)', () => {
const route = {
path: '/a',
children: [
{
path: '/b',
},
],
};
const result = Array.from(matchRoute(route, '', '/b'));
expect(result).to.have.lengthOf(0);
});

it('should match 1 route (1)', () => {
const route = {
path: '/',
Expand Down
Loading

0 comments on commit a26b222

Please sign in to comment.