From 504b7e06b56c3ce289124575a5c8b893f4a55f9c Mon Sep 17 00:00:00 2001 From: RemixDev Date: Tue, 1 Mar 2022 11:17:37 +0100 Subject: [PATCH] Catch errors in search Closes #53 --- server/src/routes/api/get/mainSearch.ts | 103 +++++++++++++++++++++++- server/src/routes/api/get/search.ts | 56 ++++++++----- webui | 2 +- 3 files changed, 137 insertions(+), 24 deletions(-) diff --git a/server/src/routes/api/get/mainSearch.ts b/server/src/routes/api/get/mainSearch.ts index ba3ca1a..c115943 100644 --- a/server/src/routes/api/get/mainSearch.ts +++ b/server/src/routes/api/get/mainSearch.ts @@ -5,12 +5,113 @@ import { sessionDZ } from '../../../app' const path: ApiHandler['path'] = '/mainSearch' +const emptyResult = { + QUERY: '', + FUZZINNESS: true, + AUTOCORRECT: false, + ORDER: ['TOP_RESULT', 'TRACK', 'ARTIST', 'ALBUM', 'PLAYLIST'], + TOP_RESULT: [], + ARTIST: { + data: [], + count: 0, + total: 0, + filtered_count: 0, + filtered_items: [], + next: 20 + }, + ALBUM: { + data: [], + count: 0, + total: 0, + filtered_count: 0, + filtered_items: [], + next: 20 + }, + TRACK: { + data: [], + count: 0, + total: 0, + filtered_count: 0, + filtered_items: [], + next: 10 + }, + PLAYLIST: { + data: [], + count: 0, + total: 0, + filtered_count: 0, + filtered_items: [], + next: 20 + }, + RADIO: { + data: [], + count: 0, + total: 0, + filtered_count: 0, + filtered_items: [], + next: 20 + }, + USER: { + data: [], + count: 0, + total: 0, + filtered_count: 0, + filtered_items: [], + next: 20 + }, + SHOW: { + data: [], + count: 0, + total: 0, + filtered_count: 0, + filtered_items: [], + next: 20 + }, + CHANNEL: { + data: [], + count: 0, + total: 0 + }, + LIVESTREAM: { + data: [], + count: 0, + total: 0, + filtered_count: 0, + filtered_items: [], + next: 20 + }, + EPISODE: { + data: [], + count: 0, + total: 0, + filtered_count: 0, + filtered_items: [], + next: 20 + }, + LYRICS: { + data: [], + count: 0, + total: 0, + filtered_count: 0, + filtered_items: [], + next: 20 + }, + ERROR: '' +} + const handler: ApiHandler['handler'] = async (req, res) => { if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() const dz = sessionDZ[req.session.id] const term = String(req.query.term) - const results = await dz.gw.search(term) + let results + try { + results = await dz.gw.search(term) + } catch (e) { + results = { ...emptyResult } + results.QUERY = term + results.ERROR = e.message + } const order: string[] = [] results.ORDER.forEach((element: string) => { if (['TOP_RESULT', 'TRACK', 'ALBUM', 'ARTIST', 'PLAYLIST'].includes(element)) order.push(element) diff --git a/server/src/routes/api/get/search.ts b/server/src/routes/api/get/search.ts index 2a7707b..1292058 100644 --- a/server/src/routes/api/get/search.ts +++ b/server/src/routes/api/get/search.ts @@ -5,6 +5,13 @@ import { sessionDZ } from '../../../app' const path: ApiHandler['path'] = '/search' +const emptyResult = { + data: [], + total: 0, + type: '', + error: '' +} + const handler: ApiHandler['handler'] = async (req, res) => { if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() const dz = sessionDZ[req.session.id] @@ -16,28 +23,33 @@ const handler: ApiHandler['handler'] = async (req, res) => { let data - switch (type) { - case 'track': - data = await dz.api.search_track(term, { limit: nb, index: start }) - break - case 'album': - data = await dz.api.search_album(term, { limit: nb, index: start }) - break - case 'artist': - data = await dz.api.search_artist(term, { limit: nb, index: start }) - break - case 'playlist': - data = await dz.api.search_playlist(term, { limit: nb, index: start }) - break - case 'radio': - data = await dz.api.search_radio(term, { limit: nb, index: start }) - break - case 'user': - data = await dz.api.search_user(term, { limit: nb, index: start }) - break - default: - data = await dz.api.search(term, { limit: nb, index: start }) - break + try { + switch (type) { + case 'track': + data = await dz.api.search_track(term, { limit: nb, index: start }) + break + case 'album': + data = await dz.api.search_album(term, { limit: nb, index: start }) + break + case 'artist': + data = await dz.api.search_artist(term, { limit: nb, index: start }) + break + case 'playlist': + data = await dz.api.search_playlist(term, { limit: nb, index: start }) + break + case 'radio': + data = await dz.api.search_radio(term, { limit: nb, index: start }) + break + case 'user': + data = await dz.api.search_user(term, { limit: nb, index: start }) + break + default: + data = await dz.api.search(term, { limit: nb, index: start }) + break + } + } catch (e) { + data = { ...emptyResult } + data.error = e.message } data.type = type diff --git a/webui b/webui index 0363294..79ab849 160000 --- a/webui +++ b/webui @@ -1 +1 @@ -Subproject commit 036329432597bc5211926a9c527b4448a4080efe +Subproject commit 79ab849df0ac4183c48d3916effe7ebf5b1b4f46