Merge branch 'hotfix/21.56.9' into master
[gitter.git] / server / handlers / uri-context / redirect-error-middleware.js
blobbfe2fc47eae6126c8eb2ada753247ea8d3a949ad
1 'use strict';
3 var normalizeRedirect = require('./normalise-redirect');
4 var StatusError = require('statuserror');
6 /**
7 * Handles various 404 and 301 error codes and deals with the
8 * in a reasonable manner
9 */
10 function redirectErrorMiddleware(err, req, res, next) {
11 switch (err.status) {
12 case 301:
13 // TODO: check this works for userhome....
14 if (err.path) {
15 res.redirect(normalizeRedirect(err.path, req));
16 // Terminate processing
17 return;
20 return next(new StatusError(500, 'Invalid redirect'));
22 case 404:
23 if (err.githubType === 'ORG' && err.uri) {
24 var url = '/orgs/' + err.uri + '/rooms';
25 //test if we are trying to load the org page in the chat frame.
26 //fixes: https://github.com/troupe/gitter-webapp/issues/628
27 if (/~chat$/.test(req.route.path)) {
28 url = url += '/~iframe';
30 res.redirect(url);
32 // Terminate processing
33 return;
37 return next(err);
40 module.exports = redirectErrorMiddleware;