Skip to content

Instantly share code, notes, and snippets.

@anjanesh
Last active April 22, 2023 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anjanesh/fda4de82fe22f82ba0db5caf08308968 to your computer and use it in GitHub Desktop.
Save anjanesh/fda4de82fe22f82ba0db5caf08308968 to your computer and use it in GitHub Desktop.
Error Messages in Chrome vs FireFox
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Error Messages in Chrome vs FireFox</title>
</head>
<body>
<script type="text/javascript">
const DEBUG = true;
// Sample data hardcoded
let g_applications =
[
{
"name": "php",
"version": "8.2",
"is_outdated": false
},
{
"name": "php",
"version": "8.1",
"is_outdated": false
},
{
"name": "php",
"version": "8.0",
"is_outdated": false
},
{
"name": "php",
"version": "7.4",
"is_outdated": false
},
{
"name": "php",
"version": "7.2",
"is_outdated": false
},
{
"name": "wordpress",
"version": "6.2",
"is_outdated": false
},
{
"name": "wordpress",
"version": "6.1",
"is_outdated": false
},
{
"name": "wordpress",
"version": "5",
"is_outdated": false
},
{
"name": "Apache",
"version": "2.4.57",
"is_outdated": false
},
{
"name": "Apache",
"version": "2.3.9",
"is_outdated": false
},
{
"name": "Apache",
"version": "2.2",
"is_outdated": false
}
];
let applications = {}; // This is going to be an object where each key would have an array
// Ideally this would come from response = fetch("api/applications");
// But here we have hardcoded from pre-defining g_applications
let response = g_applications;
for (let i = 0; i < response.length; i++)
{
// property is the key we are going to use for the applications object
let property = response[i]['name'].toLowerCase();
try
{
if (DEBUG) console.log("property = ", property);
if (DEBUG) console.log("version = ", response[i]['version']);
// Add the value (version) to the object of applications array
// Like for example : applications['php'].push('8.0');
applications[property].push(response[i]['version']);
}
catch(e)
{
// if applications['php'] doesn't exist, it'll get 'caught' in the try-catch block and it'll be assigned below
if (
e.message.includes("Cannot read properties of undefined") || // Chrome
e.message.includes("applications[property] is undefined") || // FireFox
e.message.includes("undefined is not an object (evaluating 'applications[property].push'") // Safari
)
{
applications[property] = [response[i]['version']];
}
else
{
console.error("error = ", e.message);
}
}
}
if (DEBUG) console.log("applications = ", applications);
/*
applications =
Object { php: (5) […], wordpress: (3) […], apache: (3) […] }
*/
if (DEBUG) console.log("applications as JSON string = ", JSON.stringify(applications));
// {"php":["8.2","8.1","8.0","7.4","7.2"],"wordpress":["6.2","6.1","5"],"apache":["2.4.57","2.3.9","2.2"]}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment