 
             next()
         }
+
+        router.error { request, response, next in
+            response.setHeader("Content-Type", value: "text/plain; charset=utf-8")
+            do {
+                try response.send("Caught the error: \(response.error!.localizedDescription)").end()
+            }
+            catch {}
+            next()
+        }
+
 	return router
     }
 }

            next()
        }


        router.use("/bodytest", middleware: BodyParser())
                

        }
    }

    func testErrorHandler() {
        performServerTest(router) {
            self.performRequest("get", path: "/error", callback: {response in 
                XCTAssertNotNil(response, "ERROR!!! ClientRequest response object was nil")
                do {
                    let body = try response!.readString()
                    XCTAssertEqual(body!,"Caught the error: The operation couldn’t be completed. (RouterTestDomain error 1.)")
                }
                catch{
                    XCTFail("No respose body")
                }
            })
        }
    }

    static func setupRouter() -> Router {
        let router = Router()
        // the same router definition is used for all these test cases
