binding icon indicating copy to clipboard operation
binding copied to clipboard

REQUEST: downgrade 'No Aurelia APIs are defined for the element: "xxx".' from thrown error to warning message

Open Anthony-Mckale opened this issue 6 years ago • 6 comments

I'm submitting a feature request

currently aurelia enhance is falling over when it gets elements with element/controller/view-model/view which are not defined

and it breaks the bootstraping of aurelia

caused by this code

function getAU(element) {
  let au = element.au;

  if (au === undefined) {
    throw new Error(`No Aurelia APIs are defined for the element: "${element.tagName}".`);
  }

  return au;
}
node_modules\aurelia-binding\dist\aurelia-binding.js

is there any chance this could be sent as a warning or try/catched into a warning

currently monkey patched with this in dev which not ideal, to stopping html inserted ahead of js components being deployed breaking builds

// Monkey Patch : turn thrown error into error message
import {NameExpression} from 'aurelia-binding';  
  function getAU(element) {
    var au = element.au;

    if (au === undefined) {
      // assumption that aurelia instance available
      aurelia.logger.error('No Aurelia APIs are defined for the element: "' + element.tagName + '".');
      // throw new Error('No Aurelia APIs are defined for the element: "' + element.tagName + '".');
    }
    return au;
  }
  NameExpression.locateAPI = function locateAPI(element, apiName) {
    switch (apiName) {
    case 'element':
      return element;
    case 'controller':
      return getAU(element).controller;
    case 'view-model':
      return getAU(element).controller.viewModel;
    case 'view':
      return getAU(element).controller.view;
    default:
      var target = getAU(element)[apiName];

      if (target === undefined) {
        throw new Error('Attempted to reference "' + apiName + '", but it was not found amongst the target\'s API.');
      }

      return target.viewModel;
    }
  };
  • Library Version: ALL

Please tell us about your environment:

  • Operating System: NA

  • Node Version: NA

  • JSPM OR Webpack AND Version NA

  • Browser: NA

  • Language: NA

Current behavior:

Expected/desired behavior:

  • What is the expected behavior? Aurelia should be robust and not fall over / aka cascade exception, when finding dom without Aurelia API

  • What is the motivation / use case for changing the behavior? Aurelia should be tough and handle not ideal html gracefully (but still call out error and the situations)

Anthony-Mckale avatar Mar 29 '18 15:03 Anthony-Mckale

Can't you just add a try-catch around your own code calling enhance? I ask because it sounds like there's a bug in your code, where you're calling an Aurelia api when you shouldn't have?

CasiOo avatar Mar 29 '18 15:03 CasiOo

@Anthony-Mckale Please provide an minimal example code when it fails for you. How are you using it?

Alexander-Taran avatar Mar 29 '18 16:03 Alexander-Taran

this gist is configured with the latest version of aurelia: https://gist.run/?id=7542e061bc940cde506b

jdanyow avatar Mar 30 '18 04:03 jdanyow

The error comes from ref binding, which happens when names of reference cannot be found. I think:

  1. it's a must that this fails hard & fast
  2. this error should be documented.

Would be nice if @Anthony-Mckale or some community member could help with this 😃

bigopon avatar Jan 17 '19 13:01 bigopon

@Anthony-Mckale I use ref with enhance and it works if you register your components with .globalResources() .globalResources( [ PLATFORM.moduleName("globals/checkaccess") ,PLATFORM.moduleName("globals/message") ]);

magnusdanielson avatar Feb 08 '19 14:02 magnusdanielson

@magnusdanielson yeah i ended up doing that for the webpack port, i'm now on a new contract doing react work, looking forward to see how aurelia next goes though

Anthony-Mckale avatar Feb 08 '19 14:02 Anthony-Mckale