Skip to content

Commit

Permalink
chore(#328): throw Error on router errors (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher authored Oct 10, 2021
1 parent 897dcbc commit c7b267a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-eyes-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"druxt-router": minor
---

Throw Error on router errors
12 changes: 3 additions & 9 deletions packages/router/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,6 @@ class DruxtRouter {
*/
async get (path) {
const route = await this.getRoute(path)
if (route.error) {
return { route }
}

const redirect = this.getRedirect(path, route)

return { redirect, route }
Expand Down Expand Up @@ -358,11 +354,9 @@ class DruxtRouter {

// Process Axios error.
if (!(response.status >= 200 && response.status < 300)) {
route.error = {
statusCode: response.status,
message: response.statusText
}
route.component = 'error'
const error = new Error
error.response = response
throw error
}

return route
Expand Down
7 changes: 6 additions & 1 deletion packages/router/src/stores/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,12 @@ const DruxtRouterStore = ({ store }) => {
return state.routes[path]
}

const route = await this.$druxtRouter().getRoute(path)
let route
try {
route = await this.$druxtRouter().getRoute(path)
} catch (err) {
route = { error: { statusCode: err.response.status, message: err.response.data.message } }
}

commit('addRoute', { path, route })

Expand Down
10 changes: 6 additions & 4 deletions packages/router/test/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ describe('DruxtRouter', () => {
})

test('get - error', async () => {
const { route } = await router.get('/error')

expect(route.error).toHaveProperty('message')
expect(route.error).toHaveProperty('statusCode', 404)
try {
await router.get('/error')
} catch(err) {
expect(err.response.status).toBe(404)
expect(err.response.data.message).toBe('Unable to resolve path /error.')
}
})

// @deprecated
Expand Down
2 changes: 1 addition & 1 deletion packages/router/test/stores/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('DruxtRouterStore', () => {

// Test failed request.
await store.dispatch('druxtRouter/get', '/error')
expect(store.app.context.error).toHaveBeenCalledWith({ message: 'Not Found', statusCode: 404 })
expect(store.app.context.error).toHaveBeenCalledWith({ message: 'Unable to resolve path /error.', statusCode: 404 })
})

test('getEntity', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/__mocks__/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const mockData = async (request, file) => {
response = response ? response : require(file)

// Throw error if error data is present.
if (response.data.errors) {
const error = new Error
if (response.data.errors || (!(response.status >= 200 && response.status < 300) && request.validateStatus && !request.validateStatus(response.status))) {
const error = new Error()
error.response = response
throw error
}
Expand Down

0 comments on commit c7b267a

Please sign in to comment.