Prevent errors from index and paroles pages

This commit is contained in:
Cédric FAMIBELLE-PRONZOLA
2022-05-14 03:37:04 +04:00
parent c1e8960290
commit 44e0329c64
5 changed files with 130 additions and 35 deletions
+45 -16
View File
@@ -5,12 +5,24 @@ import {jwennTeksEpiSlug, jwennTeks, jwennKomanteEpiTeksId} from '../../lib/oki-
import TeksDrawer from '../../components/teks/teks-drawer'
import HeadLayout from '../../components/head-layout'
import Custom500 from '../500'
import Custom404 from '../404'
const jwennAwtis = awtis => {
const alias = awtis.map(({alias}) => alias)
return new Intl.ListFormat('fr').format(alias)
}
export default function SlugTeks({teks, anTeks, slug, komante}) {
export default function SlugTeks({hasError, errorMessage, teks, anTeks, slug, komante}) {
if (hasError) {
console.log('⚠️ error :', errorMessage)
return <Custom500 />
}
if (!anTeks) {
return <Custom404 />
}
const awtis = anTeks.awtis.length === 1 ? anTeks.awtis[0].alias : jwennAwtis(anTeks.awtis)
const {kouveti} = anTeks
const formatKouveti = () => {
@@ -48,29 +60,46 @@ export default function SlugTeks({teks, anTeks, slug, komante}) {
export async function getServerSideProps({query}) {
const {slug} = query
const teks = await jwennTeks()
const anTeks = await jwennTeksEpiSlug(slug)
let teks
let anTeks
let hasError
let errorMessage
let komante
if (!anTeks) {
throw new Error('San répons')
try {
teks = await jwennTeks()
anTeks = await jwennTeksEpiSlug(slug)
komante = await jwennKomanteEpiTeksId(anTeks?.id)
} catch (error) {
errorMessage = error
hasError = true
}
const {id} = anTeks
const komante = await jwennKomanteEpiTeksId(id)
return {
props: {
teks,
anTeks,
hasError: hasError || null,
errorMessage: errorMessage ? errorMessage.message : null,
teks: teks || null,
anTeks: anTeks || null,
slug,
komante
komante: komante || null
}
}
}
SlugTeks.propTypes = {
teks: PropTypes.array.isRequired,
anTeks: PropTypes.object.isRequired,
slug: PropTypes.string.isRequired,
komante: PropTypes.array.isRequired
SlugTeks.defaultProps = {
hasError: null,
errorMessage: null,
teks: null,
anTeks: null,
komante: null
}
SlugTeks.propTypes = {
hasError: PropTypes.bool,
errorMessage: PropTypes.string,
teks: PropTypes.array,
anTeks: PropTypes.object,
slug: PropTypes.string.isRequired,
komante: PropTypes.array
}