3

Can anyone let me know how to throw 500 internal error in api response with REST API? i have used below right now:

throw new NoSuchEntityException(
__(
    'Field : '.$fields.' required.'
));

but it throws error code 400, i need 500 error code exactly default magento throws error if key is wrong in parameter.

I have used below code: if (!isset($postData[$fields])) { throw new \Magento\Framework\Webapi\Exception( __( 'Field : '.$fields.' has wrong value.' ), 0, \Magento\Framework\Webapi\Exception::HTTP_INTERNAL_ERROR); }

Please find attached screenshot of 400 error: 400 error

1

Try below code, I think it will work for you.

throw new \Magento\Framework\Webapi\Exception(
 __(
   'Field : '.$fields.' required.'
 ), 0, \Magento\Framework\Webapi\Exception::HTTP_INTERNAL_ERROR);

Let me know if you face any problem.

  • I have tried but it throws 400 Bad Request – bhargav shastri Sep 20 '18 at 10:19
  • 1
    Ok let me check @bhargavshastri – Ramkishan Suthar Sep 20 '18 at 10:20
  • 1
    It throws 500, Can you please update your question with whole function. – Ramkishan Suthar Sep 20 '18 at 10:25
  • edited my question, please check – bhargav shastri Sep 20 '18 at 10:31
  • 1
    API's endpoint is magento's default one or you are trying this with custom API? – Ramkishan Suthar Sep 20 '18 at 10:33
-1

Please go through below link it might help:

Common Error HTTP Codes in Magento Rest API

  • this link is not opening – bhargav shastri Sep 20 '18 at 10:17
  • @bhargavshastri i think link is working you should check before down vote the answer thanks. – Shubham Khandelwal Sep 20 '18 at 10:27
  • I have not done down vote Shubham – bhargav shastri Sep 20 '18 at 10:31

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.