boxalino / plugin-magento2

boxalino plugin for Magento 2
1 stars 6 forks source link

Add Error Handling #45

Closed heldchen closed 8 years ago

heldchen commented 8 years ago

during our site development in the last few weeks, there were several moments where there were exceptions thrown inside the boxalino intelligence plugin: bad configurations, unknown bx widget name, network problems, timeouts, webservice errors due to new attributes, webservice errors due to missing attribute values etc. in all these cases, the website was at best severely cripled or not working at all due to missing proper error handling.

the plugin should:

here's a quick example for the Crossell class:

/**
 * @param bool $execute
 * @return $this|array|null
 */
public function getItems($execute = true){

    if($this->bxHelperData->isCrosssellEnabled()){
        try {
            $config = $this->_scopeConfig->getValue('bxRecommendations/cart', $this->scopeStore);

            $products = array();
            foreach ($this->getQuote()->getAllItems() as $item) {
                $product = $item->getProduct();
                if ($product) {
                    $products[] = $product;
                }
            }

            $choiceId = (isset($config['widget']) && $config['widget'] != "") ? $config['widget'] : 'basket';
            $entity_ids = $this->p13nHelper->getRecommendation(
                $choiceId,
                'basket',
                $config['min'],
                $config['max'],
                $products,
                $execute
            );

            if(!$execute){
                return null;
            }

            if ((count($entity_ids) == 0)) {
                $entity_ids = array(0);
            }

            $items = $this->factory->create()
                ->addFieldToFilter('entity_id', $entity_ids)->addAttributeToSelect('*');
            $items->load();

            foreach ($items as $product) {
                $product->setDoNotUseCategoryId(true);
            }

            return $items;
        }
        catch (\Exception $ex)
        {
            // report
        }
    }
   return parent::getItems();
}
heldchen commented 8 years ago

implemented in latest commits, thanks