title: How to make koa ctx.throw() use application/json rather than text/plain
body: <p>I have made a custom error handler for my koa app which works beautifully (except for one sticking point) - using <code>ctx.throw()</code> means any stacktrace is emitted to the server logs and also any custom error message is sent in the response.</p>
  
  <p>The <em>one</em> problem is that <code>Content-Type</code> header is <code>text/plain</code> but I really need it to be <code>application/json</code>.</p>
  
  <p><code>app.js</code>:</p>
  
  <pre><code>import Koa from 'koa';
  import bodyParser from 'koa-bodyparser';
  import logger from 'koa-morgan';
  
  import authentication from './middleware/authentication';
  import config from './config';
  import errorHandler from './middleware/error-handler';
  import notificationsRoutes from './routes/notifications';
  
  const app = new Koa();
  
  app.use(errorHandler);
  app.use(bodyParser());
  app.use(logger(config.logLevel));
  app.use(authentication);
  app.use(notificationsRoutes.routes());
  
  export default app;
  </code></pre>
  
  <p><code>error-handler.js</code>:</p>
  
  <pre><code>export default async (ctx, next) =&gt; {
    return next().catch(({ statusCode, message }) =&gt; {
      ctx.throw(statusCode, JSON.stringify({ message }));
    });
  };
  </code></pre>
  
  <p>(I thought <code>(statusCode, JSON.stringify({ message }));</code> might coerce the response into <code>application/json</code> but it doesn't.</p>
  
  <p>I have googled to no avail. Please help!</p>