4

Hi Guys for a project i try to extend the Magento REST API. I have some problems with my error handling. I don't know how to send the right Http Response Code. I tried it with something like this:

    $this->_errorMessage($e->getMessage(), Mage_Api2_Model_Server::HTTP_BAD_REQUEST, array(
         'item' => $data
    ));
    return;

I get the right message as a response but the status code is always 207.

4

You can configure the whole HTTP response with the response object from a controller:

$this->getResponse()
    ->clearHeaders()
    ->setHeader('HTTP/1.0', YOUR_STATUS_CODE, true)
    ->setHeader('Content-Type', 'text/html') // can be changed to json, xml...
    ->setBody(YOUR_RETURN_CONTENT);
2

I am using the following approach please suggest me If I am using the wrong approach?

Included following class in my custom namespace

use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;

Return 400(bad Request)

if(empty($data)){
        throw new InputException(__('Not a valid data'));
}

Return 404 (Not Found)

if(empty($data)){
        throw new NoSuchEntityException(__('Not a valid data'));
}
1
  • This will send a stack trace to user which is not what you want to do – user487772 Jun 17 at 12:35
1

$this->_errorMessage() is used by operations performing multi-create or multi-update to provide status of individual create/update operations. In this case HTTP status code is always set to multi-status (207).

If error is critical, use $this->_critical($message, $code). If execution can be continued, use $this->_error($message, $code).

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.