/**
* Creates a nested structure for experiment headers and metadata with error logging.
* @param {object} config - Configuration object containing experiment details.
* @returns {object} - A nested object representing the experiment headers and metadata.
* @throws {Error} - If the input config is invalid.
*/
function createExperimentStructure(config) {
if (!config || typeof config !== 'object') {
throw new Error("Invalid input: config must be an object.");
}
const structure = {};
try {
structure.headers = {};
structure.metadata = {};
if (config.headers) {
if (typeof config.headers !== 'object') {
throw new Error("Invalid input: headers must be an object.");
}
for (const headerName in config.headers) {
if (config.headers.hasOwnProperty(headerName)) {
structure.headers[headerName] = config.headers[headerName];
}
}
}
if (config.metadata) {
if (typeof config.metadata !== 'object') {
throw new Error("Invalid input: metadata must be an object.");
}
for (const metadataKey in config.metadata) {
if (config.metadata.hasOwnProperty(metadataKey)) {
structure.metadata[metadataKey] = config.metadata[metadataKey];
}
}
}
return structure;
} catch (error) {
console.error("Error creating experiment structure:", error.message);
return null; // Or throw the error, depending on desired error handling.
}
}
// Example usage (can be removed for production)
// const experimentConfig = {
// headers: {
// 'X-Custom-Header': 'exampleValue',
// 'Content-Type': 'application/json'
// },
// metadata: {
// version: '1.0',
// author: 'John Doe',
// timestamp: new Date().toISOString()
// }
// };
// const experimentStructure = createExperimentStructure(experimentConfig);
// if (experimentStructure) {
// console.log(JSON.stringify(experimentStructure, null, 2));
// }
Add your comment