diff --git a/apiErrorHandler.js b/apiErrorHandler.js new file mode 100644 index 0000000..c6d34f4 --- /dev/null +++ b/apiErrorHandler.js @@ -0,0 +1,18 @@ +export const APIERRORCODES = { + STATUS200:{ + STATUS: "OK", + CODE: 200 + }, + STATUS402:{ + STATUS: "no data has been found, check if kbo numer is correct or organisation has OVO number. Organisation without OVO number are not in wegwijs", + CODE: 402 + }, + STATUS403:{ + STATUS: "no kbo number found in OP", + CODE: 403 + }, + STATUS500:{ + STATUS: '', + CODE: 500 + } +} \ No newline at end of file diff --git a/app.js b/app.js index c239feb..64763e5 100644 --- a/app.js +++ b/app.js @@ -17,6 +17,7 @@ import { WEGWIJSAPI, WEGWIJSAPIFIELDS, } from "./config"; +import { APIERRORCODES } from "./apiErrorHandler"; app.post("/sync-kbo-data/:kboStructuredIdUuid", async function (req, res) { try { @@ -24,9 +25,9 @@ app.post("/sync-kbo-data/:kboStructuredIdUuid", async function (req, res) { const identifiers = await getIdentifiers(kboStructuredIdUuid); console.log("create kbo? " + createKbo); if (!identifiers?.kbo) { - return throwServer500Error( - res, - "no kbo number was found: " + identifiers + return throwServerError( + APIERRORCODES.STATUS403, + res ); } const wegwijsUrl = `${WEGWIJSAPI}?q=kboNumber:${identifiers.kbo}&fields=${WEGWIJSAPIFIELDS}`; @@ -37,10 +38,7 @@ app.post("/sync-kbo-data/:kboStructuredIdUuid", async function (req, res) { let wegwijsOvo = null; let kboObject = null; if (!data.length) { - return throwServer500Error( - res, - "no data has been found, check if KBO number is correct" - ); + return throwServerError(APIERRORCODES.STATUS402, res); } // We got a match on the KBO, getting the associated OVO back const wegwijsInfo = data[0]; // Wegwijs should only have only one entry per KBO @@ -75,9 +73,9 @@ app.post("/sync-kbo-data/:kboStructuredIdUuid", async function (req, res) { await updateOvoNumberAndUri(ovoStructuredIdUri, wegwijsOvo); } - return res.status(200).send(); // since we await, it should be 200 + return throwServerError(APIERRORCODES.STATUS200, res); // since we await, it should be 200 } catch (e) { - return throwServer500Error(res, e); + return throwServerError(APIERRORCODES.STATUS500, res, e); } }); @@ -277,9 +275,11 @@ async function createKbo(wegwijsKboOrg, kboId, abbOrg) { await linkAbbOrgToKboOrg(abbOrg, newKboOrgUri); } -function throwServer500Error(res, message) { - console.log("Something went wrong while calling /sync-from-kbo", message); - return res.status(500).send(); +function throwServerError(statusCode, res, message) { + if(statusCode.CODE === 500){ + console.log("Something went wrong while calling /sync-from-kbo", message); + } + return res.status(statusCode.CODE).send(statusCode.STATUS); } app.use(errorHandler);