1. /**
  2. * Creates a nested structure for experiment headers and metadata with error logging.
  3. * @param {object} config - Configuration object containing experiment details.
  4. * @returns {object} - A nested object representing the experiment headers and metadata.
  5. * @throws {Error} - If the input config is invalid.
  6. */
  7. function createExperimentStructure(config) {
  8. if (!config || typeof config !== 'object') {
  9. throw new Error("Invalid input: config must be an object.");
  10. }
  11. const structure = {};
  12. try {
  13. structure.headers = {};
  14. structure.metadata = {};
  15. if (config.headers) {
  16. if (typeof config.headers !== 'object') {
  17. throw new Error("Invalid input: headers must be an object.");
  18. }
  19. for (const headerName in config.headers) {
  20. if (config.headers.hasOwnProperty(headerName)) {
  21. structure.headers[headerName] = config.headers[headerName];
  22. }
  23. }
  24. }
  25. if (config.metadata) {
  26. if (typeof config.metadata !== 'object') {
  27. throw new Error("Invalid input: metadata must be an object.");
  28. }
  29. for (const metadataKey in config.metadata) {
  30. if (config.metadata.hasOwnProperty(metadataKey)) {
  31. structure.metadata[metadataKey] = config.metadata[metadataKey];
  32. }
  33. }
  34. }
  35. return structure;
  36. } catch (error) {
  37. console.error("Error creating experiment structure:", error.message);
  38. return null; // Or throw the error, depending on desired error handling.
  39. }
  40. }
  41. // Example usage (can be removed for production)
  42. // const experimentConfig = {
  43. // headers: {
  44. // 'X-Custom-Header': 'exampleValue',
  45. // 'Content-Type': 'application/json'
  46. // },
  47. // metadata: {
  48. // version: '1.0',
  49. // author: 'John Doe',
  50. // timestamp: new Date().toISOString()
  51. // }
  52. // };
  53. // const experimentStructure = createExperimentStructure(experimentConfig);
  54. // if (experimentStructure) {
  55. // console.log(JSON.stringify(experimentStructure, null, 2));
  56. // }

Add your comment