Added basic error reporting
This commit is contained in:
parent
e63187165b
commit
ff66188f5f
@ -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,
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user