Skip to content

Commit

Permalink
build: ✨ create customized metakg loading error
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinxin90 committed Feb 19, 2021
1 parent 45a3926 commit caef894
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/middlewares/error.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const swaggerValidation = require('./validate');
const InvalidQueryGraphError = require("../utils/errors/invalid_query_graph_error");
const PredicatesLoadingError = require('../utils/errors/predicates_error');
const MetaKGLoadingError = require("../utils/errors/metakg_error");

class ErrorHandler {
setRoutes(app) {
Expand All @@ -23,6 +24,13 @@ class ErrorHandler {
more_info: error.message
})
}

if (error instanceof MetaKGLoadingError) {
return res.status(404).json({
error: "Unable to load metakg",
more_info: error.message
})
}
if (!error.statusCode) error.statusCode = 500;

if (error.statusCode === 301) {
Expand Down
6 changes: 3 additions & 3 deletions src/routes/metakg.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const utils = require("../utils/common");
const assoc = require("../controllers/association");
const MetaKGLoadingError = require("../utils/errors/metakg_error");

class RouteMetaKG {
setRoutes(app) {
app.get('/metakg', async (req, res) => {
app.get('/metakg', async (req, res, next) => {
try {
res.setHeader('Content-Type', 'application/json');
let api = undefined, source = undefined;
Expand All @@ -16,8 +17,7 @@ class RouteMetaKG {
let assocs = await assoc(req.query.subject, req.query.object, req.query.predicate, api, source);
res.end(JSON.stringify({ associations: assocs }));
} catch (error) {
console.log(error);
res.end();
next(new MetaKGLoadingError());
}
})
}
Expand Down
11 changes: 11 additions & 0 deletions src/utils/errors/metakg_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class MetaKGLoadingError extends Error {
constructor(message = "Failed to load metakg", ...params) {
super(...params);

this.name = 'MetaKGLoadingError';
this.message = message;
this.statusCode = 400;
}
}

module.exports = MetaKGLoadingError;

0 comments on commit caef894

Please sign in to comment.