     res.send "Socialworker is running with version: #{KONFIG.version}"
 
   app.listen argv.p or KONFIG.social.port
+
+  # Generic error handler
+  app.use (err, req, res, next) ->
+
+    console.error '[SocialWorker] Something went wrong', err, err?.stack
+
+    error = {}
+    if err.name and err.body
+    then error[err.name] = err.body
+    else error = apiErrors.internalError
+
+    if res.headersSent
+      next error
+    else
+      res.status(500)
+        .send { ok: false, error }
+        .end()

  helmet.defaults app
  app.use cors()

  # Generic error handler
  app.use (err, req, res, next) ->
    error = {}
    error[err.name] = err.body
    if res.headersSent
      next error
    else
      res.status(500)
        .send { ok: false, error }
        .end()

  options = { rateLimitOptions : KONFIG.nodejsRateLimiter }

  app.post '/remote.api/:model/:id?', (require './remoteapi') koding


usertracker = require '../../../usertracker'
datadog     = require '../../../datadog'
apiErrors   = require './apiErrors'

process.on 'uncaughtException', (err) ->
  console.log 'Something went wrong:', err, err?.stack
  process.exit 1

Bongo = require 'bongo'
