Issue #49: Add error handling middleware

Signed-off-by: Lucas Fernandes de Oliveira's avatarLucas Fernandes de Oliveira <lfo14@inf.ufpr.br>
parent 81b6b0eb
Pipeline #13060 passed with stage
in 41 seconds
/*
* Copyright (C) 2017 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana
*
* This file is part of blendb.
*
* blendb is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* blendb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with blendb. If not, see <http://www.gnu.org/licenses/>.
*/
import * as request from "supertest";
import { expect } from "chai";
import * as server from "../../main";
describe("API error middleware", () => {
it("should respond 404 when a route does not exists", (done) => {
request(server)
.get("/v1/inexistent")
.expect(404)
.expect((res: any) => {
const error = "Cannot GET /v1/inexistent";
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("error");
expect(res.body.error).to.be.eql(error);
})
.end(done);
});
it("should respond 400 for missing required parameters", (done) => {
request(server)
.get("/v1/data")
.query({metrics: "random"})
.expect(400)
.expect((res: any) => {
const error = "Bad request: Some problems with this request were found";
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("error");
expect(res.body).to.have.property("requestErrors");
expect(res.body.error).to.be.eql(error);
expect(res.body.requestErrors).to.have.length(1);
})
.end(done);
});
});
/*
* Copyright (C) 2017 Centro de Computacao Cientifica e Software Livre
* Departamento de Informatica - Universidade Federal do Parana
*
* This file is part of blendb.
*
* blendb is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* blendb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with blendb. If not, see <http://www.gnu.org/licenses/>.
*/
import * as express from "express";
export function ErrorMw(config: string): express.ErrorRequestHandler {
const handlers: { [key: string]: express.ErrorRequestHandler } = {
"v1": function(err, req, res, next) {
if (err.ramlNotFound) {
const splited = err.toString().split(" ");
const method = splited[1];
const route = "/v1" + splited[2];
res.status(404).json({ error: "Cannot " + method + " " + route });
}
else if (err.ramlValidation) {
res.status(400).json({
error: "Bad request: Some problems with this request were found"
, requestErrors: err.requestErrors
});
}
else if (err.ramlAuthorization) {
res.status(401).json({
error: "Unauthorized: Some problems with your authentication were found, try to authenticate again"
, requestErrors: err.authorizationErrors
});
}
else {
res.status(400).json({error: err.toString()});
}
return;
},
"default": function(err, req, res, next) {
next(err);
return;
}
};
if (handlers[config]) {
return handlers[config];
}
else {
return handlers.default;
}
};
......@@ -39,6 +39,7 @@ const config = ConfigParser.parse(configPath);
// include middlewares
import { EngineMw } from "./api/middlewares/engine";
import { PostgresMw } from "./api/middlewares/adapter";
import { ErrorMw } from "./api/middlewares/error";
app.use(EngineMw(config));
app.use(PostgresMw(config.connection));
......@@ -51,6 +52,8 @@ ramlParser.loadFile("specs/blendb-api-v1.raml")
osprey.server(raml),
router);
app.use("/v1", ErrorMw("v1"));
let port = process.env.PORT || 3000;
app.listen(port);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment