In Magento2, while returning an error from a rest endpoint, i can throw an exception like this
throw new \Magento\Framework\Webapi\Exception(__('No centers matching search criteria'))
It will throw an error like
My requirement, is to add an error code along with response i.e.
{
"message": "No centers matching search criteria.",
"error_code":101
"trace": null
}
Any ideas how this is possible?
asked May 26, 2018 at 7:09
huzefam
2,6464 gold badges20 silver badges33 bronze badges
1 Answer 1
You have to pass error code as a 2nd parameter as given below:
throw new \Magento\Framework\Webapi\Exception(__('No centers matching search criteria'),101)
Then the response should be looks like as:
{
"message": "No centers matching search criteria.",
"code": 101,
"trace": null
}
answered May 26, 2018 at 10:50
Prasanta Hatui
1,8311 gold badge11 silver badges16 bronze badges
-
this will return 200 ok with error message, i want to handle using magento's exceptionshuzefam– huzefam2018年05月27日 11:27:44 +00:00Commented May 27, 2018 at 11:27
-
ooh, I did not recognized that. I have modified the answer. Please check this may works for you.Prasanta Hatui– Prasanta Hatui2018年05月27日 13:46:47 +00:00Commented May 27, 2018 at 13:46
-
oh thats interesting, will try and get back! thankshuzefam– huzefam2018年05月28日 09:25:47 +00:00Commented May 28, 2018 at 9:25
-
can the message in array format ?jojo– jojo2020年04月14日 01:56:54 +00:00Commented Apr 14, 2020 at 1:56
default