Skip to content

Commit

Permalink
add catch to ensure we handle all server errors (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
thabti authored Nov 24, 2018
1 parent a7f94f2 commit faed687
Show file tree
Hide file tree
Showing 9 changed files with 1,791 additions and 234 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:6.3
FROM node:10.13.0

ENV API_URL http://localhost:8080
ENV USE_LOCAL_ASSETS true
Expand Down
10 changes: 0 additions & 10 deletions api/api.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import express from 'express';
import session from 'express-session';
import bodyParser from 'body-parser';
import config from '../src/config';
import morgan from 'morgan';

import routes from './routes';

const app = express();

app.use(
session({
secret: 'react and redux rule!!!!',
resave: false,
saveUninitialized: false,
cookie: { maxAge: 60000 }
})
);
app.use(bodyParser.json());
app.use(morgan('dev'));

Expand Down
26 changes: 17 additions & 9 deletions api/routes/audio_files.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,29 @@ router.get('/', (req, res) => {
return models.audioFile
.all({ include: [models.qari] })
.then(files => res.send(files))
.catch(error => res.status(500).send({ error: error }));
.catch(error => res.status(500).send({ error }));
});

router.get('/:id', (req, res) => {
return models.audioFile
.findById(req.params.id)
.then(files => res.send(files))
.catch(error => res.status(500).send({ error: error }));
if (req.params.id) {
return models.audioFile
.findById(req.params.id)
.then(files => res.send(files))
.catch(error => res.status(500).send({ error }));
}

res.status(500).send({ error: 'missing or invalid id' });
});

router.get('/download/:id', (req, res) => {
return models.audioFile
.findOne({ where: { id: req.params.id, extension: 'mp3' } })
.then(files => res.send(files))
.catch(error => res.status(500).send({ error: error }));
if (req.params.id) {
return models.audioFile
.findOne({ where: { id: req.params.id, extension: 'mp3' } })
.then(files => res.send(files))
.catch(error => res.status(500).send({ error }));
}

res.status(500).send({ error: 'missing or invalid id' });
});

export default router;
62 changes: 46 additions & 16 deletions api/routes/qaris.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,70 @@ const routerInit = Router;
const router = routerInit();

router.get('/', (req, res) => {
return models.qari.all().then(qaris => res.send(qaris));
return models.qari
.all()
.then(qaris => res.send(qaris))
.catch(error => res.status(500).send({ error }));
});

router.get('/audio_files', (req, res) => {
return models.qari
.all({ include: [models.audioFile] })
.then(qaris => res.send(qaris));
.then(qaris => res.send(qaris))
.catch(error => res.status(500).send({ error }));
});

router.get('/:id', (req, res) => {
return models.qari.findById(req.params.id).then(qari => res.send(qari));
if (req.params.id) {
return models.qari
.findById(req.params.id)
.then(qari => res.send(qari))
.catch(error => res.status(500).send({ error }));
}

res.status(500).send({ error: 'missing or invalid id' });
});

router.get('/:id/audio_files', (req, res) => {
models.qari.findById(req.params.id).then(qari => {
qari.getAudioFiles({ order: 'surah_id' }).then(files => res.send(files));
});
if (req.params.id) {
return models.qari
.findById(req.params.id)
.then(qari => {
return qari
.getAudioFiles({ order: 'surah_id' })
.then(files => res.send(files));
})
.catch(error => res.status(500).send({ error }));
}

res.status(500).send({ error: 'missing or invalid id' });
});

router.get('/:id/audio_files/:type', (req, res) => {
models.qari.findById(req.params.id).then(qari => {
qari
.getAudioFiles({
order: 'surah_id',
where: { extension: req.params.type }
if (req.params.id) {
return models.qari
.findById(req.params.id)
.then(qari => {
return qari
.getAudioFiles({
order: 'surah_id',
where: { extension: req.params.type }
})
.then(files => res.send(files));
})
.then(files => res.send(files));
});
.catch(error => res.status(500).send({ error }));
}
res.status(500).send({ error: 'missing or invalid id' });
});

router.get('/related/:id', (req, res) => {
models.related
.findAll({ where: { qari: req.params.id } })
.then(related => res.send(related));
if (req.params.id) {
return models.related
.findAll({ where: { qari: req.params.id } })
.then(related => res.send(related))
.catch(error => res.status(500).send({ error }));
}
res.status(500).send({ error: 'missing or invalid id' });
});

export default router;
5 changes: 4 additions & 1 deletion api/routes/sections.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ const routerInit = Router;
const router = routerInit();

router.get('/', (req, res) => {
models.section.all().then(sections => res.send(sections));
models.section
.all()
.then(sections => res.send(sections))
.catch(err => res.status(500).send({ err }));
});

export default router;
37 changes: 29 additions & 8 deletions api/routes/surahs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,44 @@ const routerInit = Router;
const router = routerInit();

router.get('/', (req, res) => {
models.surah.all().then(surahs => res.send(surahs));
models.surah
.all()
.then(surahs => res.send(surahs))
.catch(error => res.status(500).send({ error }));
});

router.get('/:id', (req, res) => {
models.surah.findById(req.params.id).then(surah => res.send(surah));
if (req.params.id) {
return models.surah
.findById(req.params.id)
.then(surah => res.send(surah))
.catch(error => res.status(500).send({ error }));
}
res.status(500).send({ error: 'missing or invalid id' });
});

router.get('/:id/audio_files', (req, res) => {
models.surah.findById(req.params.id).then(surah => {
surah.getAudioFiles().then(files => res.send(files));
});
if (req.params.id) {
return models.surah
.findById(req.params.id)
.then(surah => {
return surah.getAudioFiles().then(files => res.send(files));
})
.catch(error => res.status(500).send({ error }));
}
res.status(500).send({ error: 'missing or invalid id' });
});

router.get('/:id/qaris', (req, res) => {
models.surah.findById(req.params.id).then(surah => {
surah.getQaris().then(qaris => res.send(qaris));
});
if (req.params.id) {
return models.surah
.findById(req.params.id)
.then(surah => {
surah.getQaris().then(qaris => res.send(qaris));
})
.catch(error => res.status(500).send({ error }));
}
res.status(500).send({ error: 'missing or invalid id' });
});

export default router;
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@
"compression": "^1.6.0",
"css-loader": "^0.23.1",
"express": "^4.13.3",
"express-session": "^1.12.1",
"express-useragent": "^0.2.4",
"extract-text-webpack-plugin": "2.0.0-beta.3",
"file-loader": "^0.9.0",
Expand All @@ -129,7 +128,7 @@
"morgan": "^1.7.0",
"multireducer": "^2.0.0",
"node-ffprobe": "^1.2.2",
"node-sass": "^3.4.2",
"node-sass": "^4.10.0",
"normalizr": "^2.1.0",
"pg": "^6.0.3",
"piping": "^0.3.0",
Expand Down Expand Up @@ -169,7 +168,7 @@
"babel-preset-react-hmre": "^1.1.1",
"chai": "^3.3.0",
"concurrently": "^0.1.1",
"prettier": "^1.1.0",
"prettier": "^1.15.2",
"eslint": "1.10.3",
"eslint-config-airbnb": "0.1.0",
"eslint-loader": "^1.0.0",
Expand Down Expand Up @@ -211,6 +210,6 @@
"webpack-hot-middleware": "^2.5.0"
},
"engines": {
"node": "6.3.1"
"node": "^10.13.0"
}
}
}
41 changes: 23 additions & 18 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,31 @@ app.use((req, res) => {
...renderProps,
store,
helpers: { client }
}).then(() => {
const component = (
<Provider store={store} key="provider">
<ReduxAsyncConnect {...renderProps} />
</Provider>
);
})
.then(() => {
const component = (
<Provider store={store} key="provider">
<ReduxAsyncConnect {...renderProps} />
</Provider>
);

res.status(200);
res.status(200);

res.send(
'<!doctype html>\n' +
ReactDOM.renderToString(
<Html
assets={webpackIsomorphicTools.assets()}
component={component}
store={store}
/>
)
);
});
res.send(
'<!doctype html>\n' +
ReactDOM.renderToString(
<Html
assets={webpackIsomorphicTools.assets()}
component={component}
store={store}
/>
)
);
})
.catch(err => {
console.error('ROUTER ERROR:', pretty.render(err));
res.status(404).send('Not found');
});
} else {
res.status(404).send('Not found');
}
Expand Down
Loading

0 comments on commit faed687

Please sign in to comment.