"use strict";

const express = require('express');
const bodyParser = require('body-parser');
const process = require('process');
const config = require('config');
const trivorSubtitles = config.get('TrivorSubtitles');
const eureka = require('./lib/eureka.js');
const routes = require('./routes');
const consumers = require('./consumers');
const logger = require('winston');

logger.add(logger.transports.File, { filename: `${config.get('Env')}.log` });

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

app.listen(trivorSubtitles.port, () => {
    logger.info(`Listening on port ${trivorSubtitles.port}`);
    if (config.get('Env') !== 'development') {
        eureka.start();
    }
});

app.use('/api', routes);
consumers.receiveSubtitleMessage();

let cleanUp = function () {
    if (config.get('Env') !== 'development') {
        eureka.stop();
    }
    process.exit();
};

let handleError = function (error, e) {
    console.error(error);
    cleanUp();
};

//executed when the process is finished
process.on('exit', cleanUp);

//catches ctrl+c event
process.on('SIGINT', cleanUp);

//catches uncaught exceptions
process.on('uncaughtException', handleError);

module.exports = app;

@@ -405,17 +405,16 @@
 .File, %7B
- 
 filename
@@ -441,17 +441,16 @@
 ')%7D.log%60
- 
 %7D);%0A%0Acon
@@ -589,18 +589,16 @@
 () =%3E %7B%0A
-  
   logger
@@ -649,18 +649,16 @@
 ort%7D%60);%0A
-  
   if (co
@@ -690,36 +690,32 @@
 lopment') %7B%0A    
- 
-   
 eureka.start();%0A
@@ -710,26 +710,24 @@
 ka.start();%0A
-  
   %7D%0A%7D);%0A%0Aapp
@@ -789,151 +789,233 @@
 );%0A%0A
-let cleanUp = function (
+// Error Handling%0A%0Alet clientErrorHandler = function (err, req, res, next
 ) %7B%0A
-  
   if (
-config.get('Env') !== 'development') %7B%0A        eureka.stop();%0A    %7D%0A    process.exit();%0A%7D;%0A%0Alet handleError
+req.xhr) %7B%0A    res.status(500).send(%7Berror: 'Something failed!'%7D);%0A  %7D else %7B%0A    next(err);%0A  %7D%0A%7D;%0A%0Aapp.use(clientErrorHandler);%0A%0Alet cleanUp
  = f
@@ -1027,57 +1027,94 @@
 on (
-error, e
 ) %7B%0A  
- 
- console.error(error);%0A    cleanUp
+if (config.get('Env') !== 'development') %7B%0A    eureka.stop();%0A  %7D%0A  process.exit
 ();%0A
@@ -1308,19 +1308,45 @@
 n', 
-handleError
+(error) =%3E %7B%0A  logger.error(error);%0A%7D
 );%0A%0A

Handle exception or retry when OpenSubtitles is not available (#176)