Merge pull request #41 from coralproject/error-middleware

Added error middleware
This commit is contained in:
David Jay
2016-11-08 19:00:19 -05:00
committed by GitHub
5 changed files with 105 additions and 5 deletions
+1
View File
@@ -55,6 +55,7 @@
"no-var": [2],
"no-lonely-if": [2],
"curly": [2],
"no-unused-vars": ["error", { "argsIgnorePattern": "next" }],
"no-multiple-empty-lines": [
"error",
{"max": 1}
+48 -5
View File
@@ -11,14 +11,57 @@ app.use(bodyParser.json());
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// API Routes.
// Routes.
app.use('/api/v1', require('./routes/api'));
app.use('/client', express.static(path.join(__dirname, 'dist')));
app.use('/admin', require('./routes/admin'));
// Static Routes.
app.use('/client/', express.static(path.join(__dirname, 'dist')));
//==============================================================================
// ERROR HANDLING
//==============================================================================
app.get('/admin*', (req, res) => {
res.render('admin', {basePath: '/client/coral-admin'});
const ErrNotFound = new Error('Not Found');
ErrNotFound.status = 404;
// Catch 404 and forward to error handler.
app.use((req, res, next) => {
next(ErrNotFound);
});
// General error handler. Respond with the message and error if we have it while
// returning a status code that makes sense.
if (app.get('env') === 'development') {
app.use('/api', (err, req, res, next) => {
res.status(err.status || 500);
res.json({
message: err.message,
error: err
});
});
app.use('/', (err, req, res, next) => {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
app.use('/api', (err, req, res, next) => {
res.status(err.status || 500);
res.json({
message: err.message,
error: {}
});
});
app.use('/', (err, req, res, next) => {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
+13
View File
@@ -0,0 +1,13 @@
const express = require('express');
const router = express.Router();
router.get('/embed/stream/preview', (req, res) => {
res.render('embed-stream', {basePath: '/client/embed/stream'});
});
router.get('*', (req, res) => {
res.render('admin', {basePath: '/client/coral-admin'});
});
module.exports = router;
+23
View File
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
<title>Talk - Coral Admin</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.2.1/material.indigo-pink.min.css">
<link rel="stylesheet" href="<%= basePath %>/default.css">
<style media="screen">
body, #root {
width: 100%;
height: 100%;
margin: 0;
background: #fff;
}
</style>
</head>
<body>
<div id="coralStream"></div>
<script src="<%= basePath %>/bundle.js" charset="utf-8"></script>
</body>
</html>
+20
View File
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Error</title>
</head>
<body>
<div class="container">
<pre><%= message %></pre>
<pre><%= error.stack %></pre>
</div>
</body>
</html>