From ff66188f5ff8627c557944719e620c449b8e3484 Mon Sep 17 00:00:00 2001 From: Peter Morton Date: Wed, 19 Oct 2022 10:57:21 -0500 Subject: [PATCH] Added basic error reporting --- routes/unified-data-gateway.js | 56 +++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/routes/unified-data-gateway.js b/routes/unified-data-gateway.js index def4efd..3d2a08e 100644 --- a/routes/unified-data-gateway.js +++ b/routes/unified-data-gateway.js @@ -1,42 +1,40 @@ const express = require("express"); const router = express.Router(); const axios = require("axios"); -const qs = require("querystring"); +const url = require("url"); const util = require("util"); router.get("/", (req, res) => { // token in session -> get user data and send it back to the vue app if (req.session.token) { - query(req.query.threadId); + query(req.query.referenceId); } // no token -> send nothing else { + const params = new url.URLSearchParams({ + grant_type: "password", + username: process.env.EO_API_USERNAME, + password: process.env.EO_API_PASSWORD, + scope: process.env.EO_API_SCOPE, + client_id: process.env.EO_API_CLIENT_ID, + client_secret: process.env.EO_API_SECRET, + }); + axios - .post( - process.env.EO_API_ACCESS_TOKEN_URL, - qs.stringify({ - grant_type: "password", - username: process.env.EO_API_USERNAME, - password: process.env.EO_API_PASSWORD, - scope: process.env.EO_API_SCOPE, - client_id: process.env.EO_API_CLIENT_ID, - client_secret: process.env.EO_API_SECRET, - }), - { - headers: { - "Content-Type": "application/x-www-form-urlencoded", - }, - } - ) + .post(process.env.EO_API_ACCESS_TOKEN_URL, params.toString(), { + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }) .then((result) => { // save token to session req.session.token = result.data.access_token; console.log(result); - //redirect to Vue app + query(req.query.referenceId); }) .catch((err) => { - console.error(err); + sendError(err, res); }); } @@ -219,8 +217,24 @@ router.get("/", (req, res) => { } }) .catch((err) => { - console.log(err); + // bin the token on error + req.session.destroy(); + sendError(err, res); }); } }); module.exports = router; +function sendError(err, res) { + console.error(err); + const errStatus = 500; + if (err.response) errStatus = err.response.status; + res.status(errStatus).send({ + errors: [ + { + status: errStatus, + title: err.code, + detail: err.message, + }, + ], + }); +}