From 8231d58c824e21722b2224458a14d73fc109a0c1 Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Tue, 4 Aug 2020 23:49:50 +0200 Subject: [PATCH] fix(Public): handle URL erros --- src/controllers/PublicController.ts | 78 +++++++++++++++++------------ 1 file changed, 47 insertions(+), 31 deletions(-) diff --git a/src/controllers/PublicController.ts b/src/controllers/PublicController.ts index 395c6c7..061debc 100644 --- a/src/controllers/PublicController.ts +++ b/src/controllers/PublicController.ts @@ -51,7 +51,7 @@ export default class PublicController { return { _id: o._id, name: version.name, - description: version.descriptionShort.replace(/\n/g, ''), + description: version.descriptionShort.replace(/\n/g, ' '), thumbnail: version.thumbnail.key, tags: version.tags === null ? 'tags_not_found' : version.tags, slug: o.get('slugs')[o.get('slugs').length -1] @@ -70,11 +70,11 @@ export default class PublicController { organizationsJSON: JSON.stringify(organizationsData) }) }).catch(err => () => { - console.log(err) + console.error(err) return ErrorController.handleServerError(err, req, res, []) }) }).catch(err => () => { - console.log(err) + console.error(err) return ErrorController.handleServerError(err, req, res, []) }) } @@ -138,13 +138,28 @@ export default class PublicController { } if (Utils.isStrUsable(version.contacts, 'facebook')) { - version.contacts.facebookLabel = new URL(version.contacts.facebook).pathname.replace('/', '') + try { + version.contacts.facebookLabel = new URL(version.contacts.facebook).pathname.replace('/', '') + } catch (err) { + console.error(err) + version.contacts.facebook = version.contacts.facebookLabel = "OULA, c'est pas bon :(" + } } if (Utils.isStrUsable(version.contacts, 'twitter')) { - version.contacts.twitterLabel = new URL(version.contacts.twitter).pathname.replace('/', '') + try { + version.contacts.twitterLabel = new URL(version.contacts.twitter).pathname.replace('/', '') + } catch (err) { + console.error(err) + version.contacts.twitter = version.contacts.twitterLabel = "OULA, c'est pas bon :(" + } } if (Utils.isStrUsable(version.contacts, 'instagram')) { - version.contacts.instagramLabel = new URL(version.contacts.instagram).pathname.replace('/', '') + try { + version.contacts.instagramLabel = new URL(version.contacts.instagram).pathname.replace('/', '') + } catch (err) { + console.error(err) + version.contacts.instagram = version.contacts.instagramLabel = "OULA, c'est pas bon :(" + } } } if (Array.isArray(version.gallery)) { @@ -153,45 +168,46 @@ export default class PublicController { return media }) } - version.cutDescription = Utils.isStrUsable(version, 'descriptionLong') && version.descriptionLong.length > 200 - - // TODO: Rassembler les horaires qui sont dans le même jour, uniquement pour le front + if (Utils.isUsable(version, 'schedule')) { + // TODO: Rassembler les horaires qui sont dans le même jour, uniquement pour le front + version.schedule = version.schedule.map((s: any) => { + let days = [...new Set(s.when.map((w: any) => w.day))] + return { + when: days.map((day: any) => { + return { + day, + wow: 'wow', + hours: s.when + .filter((w: any) => w.day === day) + .map((hour: any) => { return { from: hour.from, to: hour.to } }) + } + }), + name: s.name, + description: s.description + } + }) + } - version.schedule = version.schedule.map((s: any) => { - let days = [...new Set(s.when.map((w: any) => w.day))] - return { - when: days.map((day: any) => { - return { - day, - wow: 'wow', - hours: s.when - .filter((w: any) => w.day === day) - .map((hour: any) => { return { from: hour.from, to: hour.to } }) - } - }), - name: s.name, - description: s.description - } - }) + version.cutDescription = Utils.isStrUsable(version, 'descriptionLong') && version.descriptionLong.length > 200 // if (version.cutDescription) { // version.descriptionFirstHalf = version.descriptionLong.substr(0, 200) // not gonna lie // version.descriptionSecondHalf = version.descriptionLong.substr(200) // } - let hour = lastPublished.toLocaleTimeString('fr-FR', { timezone: '+2' }) - if (hour.charAt(1) === ':') { - hour = '0' + hour - } + // let hour = lastPublished.toLocaleTimeString('fr-FR', { timezone: '+2' }) + // if (hour.charAt(1) === ':') { + // hour = '0' + hour + // } res.render('organization.twig', { layout: 'standalone', data: version, - lastPublished: lastPublished.toLocaleDateString('fr-FR') + ' ' + hour.substr(0, 5), + //lastPublished: lastPublished.toLocaleDateString('fr-FR') + ' ' + hour.substr(0, 5), isProposed, id: org.get('_id') }) } }).catch(err => () => { - console.log(err) + console.error(err) return ErrorController.handleServerError(err, req, res, []) }) }