From 62db3939692cfee996d7f4389a0f821c64d5ce96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Losada=20Hern=C3=A1ndez?= Date: Thu, 15 Feb 2024 15:50:27 +0100 Subject: [PATCH] proper error handling(i think) --- .../pages/archivePage/archivePage.jsx | 54 +++++++++++-------- server/archive.api.js | 10 +++- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/client/homebrew/pages/archivePage/archivePage.jsx b/client/homebrew/pages/archivePage/archivePage.jsx index 365bb5f2a..07eae0311 100644 --- a/client/homebrew/pages/archivePage/archivePage.jsx +++ b/client/homebrew/pages/archivePage/archivePage.jsx @@ -243,27 +243,8 @@ const ArchivePage = createClass({ renderFoundBrews() { const { title, brewCollection, page, totalPages, error, searching } = this.state; - if(searching === false && title === '' && error === null) {return (

Whenever you want, just start typing...

);} - - if(searching === false && error === 'Error: Service Unavailable') { - return ( -
-

I'm sorry, your request didn't work

-

Your search is not specific enough. Too many brews meet this criteria for us to display them.

-
- ); - }; - console.log(searching, !brewCollection, error); - console.log('404: ', searching === false && (!brewCollection || error === 'Error: Not found')) - if(searching === false && (!brewCollection || error === 'Error: Not found')) { - return ( -
-

We haven't found brews meeting your request.

-
- ); - }; - - if(searching === true) { + + if(searching) { return (

Searching

...

@@ -271,6 +252,37 @@ const ArchivePage = createClass({ ); }; + if(title === '') {return (

Whenever you want, just start typing...

);} + + if (error) { + let errorMessage; + switch (error.errorCode) { + case '404': + errorMessage = "We didn't find any brew"; + break; + case '503': + errorMessage = 'Your search is not specific enough. Too many brews meet this criteria for us to display them.'; + break; + case '500': + default: + errorMessage = 'An unexpected error occurred'; + } + + return ( +
+

Error: {errorMessage}

+
+ ); + }; + + if (!brewCollection || brewCollection.length === 0) { + return ( +
+

No brews found

+
+ ); + }; + return (
Brews found: {this.state.totalBrews} diff --git a/server/archive.api.js b/server/archive.api.js index 1c00d7f0a..3a8dd41b2 100644 --- a/server/archive.api.js +++ b/server/archive.api.js @@ -46,6 +46,10 @@ const archive = { // No published documents found with the given title return res.status(404).json({ error: 'Published documents not found' }); } + if (!brews || brews.length === 0) { + return res.status(404).json({ errorCode: '404', message: 'Published documents not found' }); + } + const totalBrews = await HomebrewModel.countDocuments(titleQuery, projection); const totalPages = Math.ceil(totalBrews / pageSize); @@ -54,7 +58,11 @@ const archive = { return res.json({ brews, page, totalPages, totalBrews}); } catch (error) { console.error(error); - return res.status(500).json({ error: 'Internal Server Error' }); + if (error.response && error.response.status === 503) { + return res.status(503).json({ errorCode: '503', message: 'Service Unavailable' }); + } else { + return res.status(500).json({ errorCode: '500', message: 'Internal Server Error' }); + } } } };