From 9f4898db10b2ae10c66b77bd8711d38c49ffed77 Mon Sep 17 00:00:00 2001
From: aurieh <auriteh@gmail.com>
Date: Wed, 28 Dec 2016 12:52:45 +0200
Subject: [PATCH] Clean up, fix things

---
 .eslintrc.json |  3 +++
 index.js       | 29 +++++++++++++++++++++++++----
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/.eslintrc.json b/.eslintrc.json
index a9c0046..a653b56 100644
--- a/.eslintrc.json
+++ b/.eslintrc.json
@@ -1,5 +1,8 @@
 {
   "extends": "standard",
+  "parserOptions": {
+    "ecmaVersion": 6
+  },
   "installedESLint": true,
   "plugins": [
     "standard",
diff --git a/index.js b/index.js
index 1b36498..801818a 100644
--- a/index.js
+++ b/index.js
@@ -15,13 +15,34 @@ for (let env of [
 
 // Create Koa app
 const app = koa();
-const route = require("koa-route");
+const route = require('koa-route');
+
+// Error handler
+app.use(function* (next) {
+  try {
+    yield next;
+  } catch (err) {
+    if (err.isBoom) {
+      this.status = err.output.statusCode || 500;
+      this.body = err.output.payload || '';
+      this.set('content-type', 'application/json; charset=utf-8');
+      for (const header in err.output.headers || {}) {
+        this.set(header, err.output.headers[header]);
+      }
+    } else {
+      this.status = err.status || 500;
+      this.body = err.message || '';
+    }
+
+    if (this.status > 499) this.app.emit('error', err, this);
+  }
+});
 
 /**
  * Parse request body.
  */
-app.use(next => {
-  if (req.method === 'GET' || req.method === 'DELETE') {
+app.use(function* (next) {
+  if (this.req.method === 'GET' || this.req.method === 'DELETE') {
     yield next;
     return;
   }
@@ -43,7 +64,7 @@ app.use(next => {
   this.req.on('end', () => {
     try {
       this.req.body = JSON.parse(rawData);
-      yield next;
+      next();
       return;
     } catch (err) {
       this.body = {
-- 
GitLab