Skip to content

Commit

Permalink
fix(#323): add try/catch to prevent error (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher authored Oct 9, 2021
1 parent 916eea3 commit ee15810
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-rabbits-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"druxt-menu": patch
---

Fixed bug when menu endpoint fails
16 changes: 11 additions & 5 deletions packages/menu/src/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class DruxtMenu {

/**
* Builds the JSON:API query.
*
*
* @private
*
*
* @param {string} resource - The JSON:API resource type.
* @param {string} menuName - The menu name.
* @param {string[]} requiredFields - An array of required fields for the menu resource.
Expand Down Expand Up @@ -135,7 +135,7 @@ class DruxtMenu {
* requiredOnly: true,
* }
* )
*
*
* @param {string} menuName - The menu name.
* @param {object} settings - The Druxt Menu query settings object.
*/
Expand Down Expand Up @@ -163,10 +163,16 @@ class DruxtMenu {
}

const entities = []
const collections = await this.druxt.getCollectionAll(menuItemsResource, query)
let collections = []
try {
collections = await this.druxt.getCollectionAll(menuItemsResource, query)
} catch(e) {
console.log("ASDASDOOO")
return { entities }
}
for (const collection of collections) {
for (const entity of collection.data) {
entities.push({
entities.push({
...entity,
attributes: {
...entity.attributes,
Expand Down
8 changes: 5 additions & 3 deletions packages/menu/test/menu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ describe('DruxtMenu class', () => {
})

test('get - getMenuLinkContent', async () => {
const { entities } = await menu.get('main')

// TODO - Add mock test data, Umami Profile has none.
// expect(entities.length).toBe(4)
expect((await menu.get('main')).entities.length).toBe(0)
})

test('get - getJsonApiMenuItems', async () => {
const jsonApiMenu = new DruxtMenu(baseUrl, { menu: { jsonApiMenuItems: true } })

// Ensure main menu returns three items.
expect((await jsonApiMenu.get('main')).entities.length).toBe(3)

// Ensure non-existant menu return no items.
expect((await jsonApiMenu.get('error')).entities.length).toBe(0)

// TODO - Add mock test data, Umami Profile doesn't have deep enough menu items.
// const settings = {
// max_depth: 3,
Expand Down
57 changes: 57 additions & 0 deletions test/__fixtures__/get/5d48ed0bf122a98f9a00f3dc4d754dd5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"data": {
"jsonapi": {
"version": "1.0",
"meta": {
"links": {
"self": {
"href": "http://jsonapi.org/format/1.0/"
}
}
}
},
"errors": [
{
"title": "Not Found",
"status": "404",
"detail": "The \"menu\" parameter was not converted for the path \"/jsonapi/menu_items/{menu}\" (route name: \"jsonapi_menu_items.menu\")",
"links": {
"via": {
"href": "https://demo-api.druxtjs.org/jsonapi/menu_items/error?filter%5Benabled%5D=1&filter%5Bmenu_name%5D=error"
},
"info": {
"href": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5"
}
}
}
]
},
"status": 404,
"statusText": "Not Found",
"headers": {
"content-type": "application/vnd.api+json",
"cache-control": "must-revalidate, no-cache, private",
"content-language": "en",
"expires": "Sun, 19 Nov 1978 05:00:00 GMT"
},
"config": {
"url": "/jsonapi/menu_items/error?filter%5Benabled%5D=1&filter%5Bmenu_name%5D=error",
"method": "get",
"headers": {
"Accept": "application/json, text/plain, */*"
},
"baseURL": "https://demo-api.druxtjs.org",
"transformRequest": [
null
],
"transformResponse": [
null
],
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"maxBodyLength": -1
},
"request": {}
}

0 comments on commit ee15810

Please sign in to comment.