0

in the process of creating a custom module to learn the magento structure but I am stuck in how to handle errors. Well maybe the way I created my module is already off but it works for now. Only want to add some error feedback. So let me explain.

With this tutorial I created my module.

https://www.atwix.com/magento/add-button-to-system-configuration/

So in my adminhtml I have an ajax button calling my controller function check

<script type="text/javascript">
    //<![CDATA[
    function check() {
        new Ajax.Request('<?php echo $this->getAjaxCheckUrl() ?>', {
            method:     'get',
            onSuccess: function(transport){

            if (transport.responseText){
                alert('Checked')
            }
            }
        });
    }
    //]]>
</script>
<?php echo $this->getButtonHtml() ?>

In my controller file I am calling multiple helper files

public function checkAction()
{
Mage::helper('Module/Copy')->copy();
Mage::helper('Module/Changexml')->changexml();

Example helper file

class Module_module_Helper_Copy extends Mage_Core_Helper_Abstract
{

    public function copy()
    {

    $source = Mage::getBaseDir();
    $dest = Mage::getStoreConfig('Module/module/folder');   
    shell_exec("cp -r $source $dest");

    }

What I now want to add is if something goes wrong in my helper function I actually show that message in the admin through the button.phtml file (first code). The code I added is just an example and in the original tutorial the function was like this

public function checkAction()
{
    $result = 1;
    Mage::app()->getResponse()->setBody($result);

is it even ok to call multiple helper file after each other from an observer file? Sorry for the questions, eager to learn how this should be done.!

0

Usually, for feedback messages you would use the Magento built-in messaging system, for example: Mage::getSingleton('adminhtml/session')->addError(""), but this will be shown on the next page load.

Since you are doing an AJAX, you're on your own. You should take care of the response and provide proper feedback. I see two ways you can go:

  1. Simplest way, gather all feedback into your response (make your helper methods return something and gather responses in your controller action response output), and update the <ul class="messages"> element from the page when the ajax completes, like done here: https://stackoverflow.com/a/6055324/1470684

  2. Use the Magento built-in messaging, so you can directly add multiple feedback messages from specific points in your code (your controller, helper, etc), and then take care of reloading the layout, thus also getting your new messages. You can find a solution for this here: https://stackoverflow.com/a/8277804/1470684

0

You might want to consider sending back an error code with something like:

$this->getResponse()->setHeader('HTTP/1.0','400',true);

inside you controller should it meet some kind of fail of validation. Then handle this inside your error section of the Ajax request.

error: function(jqXHR, textStatus, errorThrown) {
       console.log(textStatus, errorThrown);
}

Or something along those lines?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.