Add input validation and HTTP status error handling
This commit is contained in:
+29
-4
@@ -15,6 +15,9 @@ async function DoImport(msg, url, node, maxRetries, baseBackoffMs) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(url);
|
const response = await fetch(url);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP ${response.status} ${response.statusText}`)
|
||||||
|
}
|
||||||
datastr = await response.text();
|
datastr = await response.text();
|
||||||
|
|
||||||
if (datastr.toLowerCase().includes("no data found")) {
|
if (datastr.toLowerCase().includes("no data found")) {
|
||||||
@@ -70,15 +73,37 @@ module.exports = function(RED) {
|
|||||||
var node = this;
|
var node = this;
|
||||||
node.status({ text: `` })
|
node.status({ text: `` })
|
||||||
node.on('input', async function(msg) {
|
node.on('input', async function(msg) {
|
||||||
const page = parseInt(encodeURIComponent(msg.page || 0));
|
const pageRaw = msg.page === undefined || msg.page === null || msg.page === "" ? 0 : msg.page
|
||||||
const apikey = encodeURIComponent(msg.apikey || config.apikey);
|
const page = parseInt(pageRaw, 10)
|
||||||
const what = encodeURIComponent(config.what)
|
const apikeyRaw = msg.apikey || config.apikey
|
||||||
|
const whatRaw = config.what
|
||||||
const orderby = encodeURIComponent(msg.orderby || config.orderby);
|
const orderby = encodeURIComponent(msg.orderby || config.orderby);
|
||||||
|
|
||||||
|
if (!whatRaw || String(whatRaw).trim().length === 0) {
|
||||||
|
node.status({ fill: "red", shape: "ring", text: "Invalid config: 'what' is required" })
|
||||||
|
node.error("Invalid config: 'what' is required", msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!apikeyRaw || String(apikeyRaw).trim().length === 0) {
|
||||||
|
node.status({ fill: "red", shape: "ring", text: "Missing API key" })
|
||||||
|
node.error("Missing API key: set config.apikey or msg.apikey", msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Number.isNaN(page) || page < 0) {
|
||||||
|
node.status({ fill: "red", shape: "ring", text: "Invalid page: must be >= 0" })
|
||||||
|
node.error(`Invalid page '${pageRaw}': must be a non-negative integer`, msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const apikey = encodeURIComponent(apikeyRaw);
|
||||||
|
const what = encodeURIComponent(whatRaw)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
msg.page = page
|
msg.page = page
|
||||||
msg.what = what
|
msg.what = whatRaw
|
||||||
const maxRetries = Number.isInteger(parseInt(config.maxRetries, 10))
|
const maxRetries = Number.isInteger(parseInt(config.maxRetries, 10))
|
||||||
? Math.max(0, parseInt(config.maxRetries, 10))
|
? Math.max(0, parseInt(config.maxRetries, 10))
|
||||||
: 3
|
: 3
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
"name": "@accede/node-red-contrib-odbcwritenow",
|
"name": "@accede/node-red-contrib-odbcwritenow",
|
||||||
"version": "1.0.4",
|
"version": "1.0.4",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "odbcwritenow.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user