     _.extend options, pageTitles.PageTitles[options.page]
     options.subnav ?= 'subnav'
     fs.readFile "client/template/#{options.subnav}.eco", (err, subnavTemplate) ->
+      if err?
+        console.warn "Template #{options.page} not found when rendering server side"
+        return resp.send 500, {error: "Template not found: #{err}"}
+
       fs.readFile "client/template/nav.eco", (err, navTemplate) ->
+        if err?
+          console.warn "Template #{options.page} not found when rendering server side"
+          return resp.send 500, {error: "Template not found: #{err}"}
+
         getSessionUsersFromDB req.user, (usersObj) ->
           resp.render 'index',
               title: options.title or 'ScraperWiki'


getToken = (req, resp) ->
  Token.find req.params.token, (err, token) ->
    if token?.shortName
      return resp.send 200, { token: token.token, shortName: token.shortName }
    else

switchContextIfRequiredAndAllowed = (req, resp, next) ->
  datasetID = req.params[0]
  Dataset.findOneById datasetID, (err, dataset) ->
    if err?
      resp.status 500
      return

    if dataset
      if dataset.user == req.user.effective.shortName
        return next()
      else
        User.findByShortName dataset.user, (err, switchingTo) ->
          if err?
            resp.status 500
            return

          if switchingTo
            if switchingTo?.canBeReally and req.user.real.shortName in switchingTo.canBeReally
              req.user.effective = getSessionUser switchingTo
