Catching Errors in Factory Method with express.js My express.js application uses a lot of promises for interacting with a variety of services. Instead of including try/catch with each one, I've setup a fail safe factory method for all my express controllers. This should only be called when an unexpected error occurred. For example, 404 when trying a public API. I'm skeptical about how useful this is or if there is a much easier way to do this. Also looking for any other comments on this code or question.
//index.ts

import * as Express from 'express';
import { Auth } from '../class/Auth';

/**
 * Handler for async static functions recognizing that errors
 * should always be sent to the error handler. This is where
 * our error stone drops. This will eventuall replace go().
 * @param  callback function to be run
 * @return function for express to run
 */
function $(callback:Function):Function {
  return async (req:Express.Request, res:Express.Response, next:Express.NextFunction) =&gt; {
    try {
      await callback(req, res, next);
    } catch(e) {
      console.error(&quot;Something strange occurred&quot;,e);
      res.status(500).render('error', {
        error: &quot;An internal error has occurred&quot;,
        errorCode: 500
      });
    }
  }
}

/**
 * Controller to log the user in
 * @param  req Express Request
 * @param  res Express Response
 */
function async loginController(req:Express.Request, res:Express.Response) {
  // verify parameters
  if(!(req.body['username'] &amp;&amp; req.body['password'])){
    return res.status(400).end(&quot;Please include username and password&quot;);
  }

  var user = await Auth.login(req.body['username'], req.body['password']);
  
  if (user) {
    var authToken = Auth.generateAuthToken(user);
    res.json({
      authToken:authToken
    });
  } else {
    return res.status(401).end(&quot;Wrong username or password&quot;);
  }
}

var app = Express();
app.post('/api/login', $(loginController));
app.listen(8080);


// Auth.ts

import { User } from './User';

/**
 * Authentication base class
 */
export class Auth {
  /**
   * Log user in
   * @param email 
   * @param password 
   * @returns user logged in. Returns null if user is not found or authentication fails 
   */
  public static async login(email:string, password:string):Promise&lt;User&gt; {
    var response = await fetch(&quot;example.com/api/login&quot;, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        email: email
        password: password
      })
    });

    if(response.ok){
      return await response.json() as User;
    } 
 
    // password or username was wrong
    if(response.status == 401){
      return null;
    }

    // an unknown error occurred
    throw Error(response.status);
  }

  /**
   * Generate AuthToken
   * @param user 
   * @return token
   */
  public static generateAuthToken(user:User):string {
    return jwt.sign({ userid: user.id }, process.env.SECRET, {
      expiresIn: process.env.JWT_EXPIRY
    });
  }
}

```
First of all, by default ExpressJS does not support async routes. To solve that you can use express-async-errors plugin. Just add the following at the beginning of the file:
import 'express-async-errors'

Now ExpressJS will await promises and handle promise rejections. Then you can create custom errors handler:
export function handleApiErrors(): (err: unknown, req: Request, resp: Response, next: NextFunction) =&gt; unknown {
    return (err, _req, resp, next) =&gt; {
        if (err) {
          console.error(String(err))
          return resp.status(500).json({
              error: 'unknown error',
          })
        }
        next(err)
    }
}

Then include custom error handler just before app.listen:
app.use(handleApiErrors())


Now each route can return a promise and rejected promises will be returned as 500 response. You can also include http-errors to further improve error handling.

You can check the live example on torrent-stream-server

Other observations
null return
login has Promise&lt;User&gt; type, but you return null. If you don't get typescript error, you should consider enabling strictNullChecks option.
res.end and res.json
You mix res.json with res.end. Maybe some middleware handles, that res.end would return JSON. Or maybe you just return plain text response on error? In any case, it's confusing, so you should always use res.json() (you can chain it with end if you like, but I don't see a reason).
No spaces
I see that you don't like spaces. But it's hard to read with that style. You should at least add spaces after :. You should find a styleguide you like and follow it. Or you can just use prettier