update and error handling
This commit is contained in:
parent
fa6bc5f3f3
commit
1bb080f7f1
12 changed files with 203 additions and 30 deletions
|
|
@ -67,7 +67,7 @@ export default class MediaService {
|
|||
// @ts-ignore
|
||||
contentType: file.contentType,
|
||||
// @ts-ignore
|
||||
location: file.location,
|
||||
location: file.location.replace('http://', 'https://'),
|
||||
// @ts-ignore
|
||||
size: file.size,
|
||||
// @ts-ignore
|
||||
|
|
|
|||
17
src/app.ts
17
src/app.ts
|
|
@ -14,6 +14,7 @@ import PublicController from './controllers/PublicController'
|
|||
import cors from 'cors'
|
||||
import twig from 'twig'
|
||||
import EmailService from './EmailService'
|
||||
import ErrorController from './controllers/ErrorController'
|
||||
|
||||
dotenv.config({
|
||||
path: __dirname + '/../.env'
|
||||
|
|
@ -42,6 +43,7 @@ let main = async () => {
|
|||
console.log('> App: Connected to mongodb')
|
||||
})
|
||||
|
||||
|
||||
app.set("twig options", {
|
||||
allow_async: true,
|
||||
strict_variables: false
|
||||
|
|
@ -49,11 +51,6 @@ let main = async () => {
|
|||
app.use(cors())
|
||||
app.use(bodyParser.json())
|
||||
|
||||
app.use((err: any, req: express.Request, res: express.Response, next: express.RequestHandler) => {
|
||||
console.error(err.stack)
|
||||
res.status(res.statusCode).json({ success: false, error: err.stack })
|
||||
})
|
||||
|
||||
app.get('/', PublicController.home)
|
||||
app.get('/association/:slug', PublicController.organization)
|
||||
app.get('/a-propos', PublicController.about)
|
||||
|
|
@ -111,6 +108,16 @@ let main = async () => {
|
|||
|
||||
app.use(express.static(path.resolve('./static')))
|
||||
|
||||
app.get('/500', ErrorController.internalError)
|
||||
|
||||
app.use((err: any, req: express.Request, res: express.Response, next: express.RequestHandler) => {
|
||||
console.error(err.stack)
|
||||
//res.status(res.statusCode).json({ success: false, error: err.stack })
|
||||
ErrorController.handle(500, 'Erreur interne', 'Ouups je sais pas quoi dire', '💥', err.stack)(req, res)
|
||||
})
|
||||
|
||||
app.use(ErrorController.notFoundHandler())
|
||||
|
||||
app.listen(port, host, () => {
|
||||
console.log(`> App: API listening on ${host}:${port}`)
|
||||
})
|
||||
|
|
|
|||
23
src/controllers/ErrorController.ts
Normal file
23
src/controllers/ErrorController.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import * as express from 'express'
|
||||
|
||||
export default class ErrorController {
|
||||
static handle(code: number, title: string, subTitle: string, emoji: string, details = '') {
|
||||
return (req: express.Request, res: express.Response) => {
|
||||
res.status(code).render('error.twig', {
|
||||
title,
|
||||
subTitle,
|
||||
emoji,
|
||||
layout: -1,
|
||||
details
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
static notFoundHandler() {
|
||||
return ErrorController.handle(404, 'Page introuvable', 'Mais où peut donc se trouver cette page ?', '🔍 🕵️')
|
||||
}
|
||||
|
||||
static internalError(req: express.Request, res: express.Response) {
|
||||
console.log(res.locals.blabla.blabla)
|
||||
}
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ import fs from 'fs'
|
|||
import { IconService, IconInterface } from '../IconService'
|
||||
import IORedis from 'ioredis'
|
||||
import Tag from '../models/Tag'
|
||||
import ErrorController from './ErrorController'
|
||||
|
||||
export default class PublicController {
|
||||
|
||||
|
|
@ -43,14 +44,14 @@ export default class PublicController {
|
|||
static async organization(req: express.Request, res: express.Response) {
|
||||
Organization.find({ slug: req.params.slug }).then(data => {
|
||||
if (data.length === 0) {
|
||||
return res.status(404).render('not-found.twig')
|
||||
return ErrorController.notFoundHandler()(req, res)
|
||||
} else {
|
||||
const org = data[0]
|
||||
let version = org.get('publishedVersion')
|
||||
if (req.query.version === 'proposed') {
|
||||
version = org.get('proposedVersion')
|
||||
} else if (org.get('publishedAt') === undefined || org.get('publishedAt') === null) {
|
||||
return res.status(404).render('not-found.twig')
|
||||
return ErrorController.notFoundHandler()(req, res)
|
||||
}
|
||||
if (version.contacts !== null && version.contacts !== undefined) {
|
||||
if (typeof version.contacts.address === 'string') {
|
||||
|
|
@ -74,6 +75,12 @@ export default class PublicController {
|
|||
version.contacts.phoneSplit = phoneSplit
|
||||
}
|
||||
}
|
||||
if (Array.isArray(version.gallery)) {
|
||||
version.gallery = version.gallery.slice(0, 5).map((media: any) => {
|
||||
media.isVideo = media.contentType.indexOf('video/') !== -1
|
||||
return media
|
||||
})
|
||||
}
|
||||
res.render('organization.twig', {
|
||||
layout: 'standalone',
|
||||
data: version
|
||||
|
|
|
|||
|
|
@ -74,14 +74,14 @@ const OrganizationVersion = {
|
|||
priceLabel: { type: String, required: true },
|
||||
description: { type: String }
|
||||
}],
|
||||
tag: { type: Tag }
|
||||
tag: [Tag]
|
||||
}
|
||||
|
||||
const Organization = new Schema({
|
||||
adminName: { type: String, required: true },
|
||||
email: { type: String, required: true },
|
||||
token: { type: String, required: true },
|
||||
slug: { type: String, required: true },
|
||||
slug: [{ type: String }], // aliases system
|
||||
validationState: {
|
||||
type: AllowedString,
|
||||
required: true,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue