orestbida / cookieconsent

:cookie: Simple cross-browser cookie-consent plugin written in vanilla js
https://playground.cookieconsent.orestbida.com/
MIT License
3.91k stars 406 forks source link

[Feat]: Better handling when translations are undefined #650

Open hrasekj opened 7 months ago

hrasekj commented 7 months ago

Description

Hello,

so i had this bug in my code, when my function didn't return object with traslations for ConsentModal. Which led to state._currentTranslation.consentModal be undefined and function createConsentModal did not create any html elements. This led to unrelated errors about missing dom elements. CookieConsent was trying add events and attributes even, when props like dom._cm where undefined.

Proposed solution

In createConsentModal should be atleast console.warn or throw?

const createConsentModal = (api, createMainContainer) => {
  // ...

  /**
   * @type {import("../global").ConsentModalOptions}
   */
  const consentModalData = state._currentTranslation && state._currentTranslation.consentModal;

  if (!consentModalData) {
    // console.warn('consentModalData is undefined');
    // throw new Error('consentModalData is undefined');
    return;
  }

  // ...

Additional details

No response