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

2 Answers 2

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.

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

Please go through below link it might help:

Common Error HTTP Codes in Magento Rest API

3
  • 1
    this link is not opening Sep 20, 2018 at 10:17
  • @bhargavshastri i think link is working you should check before down vote the answer thanks. Sep 20, 2018 at 10:27
  • The link is not working
    – Seb
    May 13, 2021 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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