Yii2 REST: How to customize Error response? -
for first use solution - http://www.yiiframework.com/doc-2.0/guide-rest-error-handling.html but, want customize 2 types error.
- when model validation wrong.
- when wrong (exceptiin)
if model validation wrong response, that:
{ "success": false, "data": [ { "field": "country_id", "message": "country id cannot blank." }, { "field": "currency_id", "message": "currency id cannot blank." }, { "field": "originator_id", "message": "originator id cannot blank." } ] }
but want that:
{ "success": false, "data": [ "errors": [ { "field": "country_id", "message": "country id cannot blank." }, { "field": "currency_id", "message": "currency id cannot blank." }, { "field": "originator_id", "message": "originator id cannot blank." } ] ] }
second type error
{ "success": false, "data": { "name": "exception", "message": "invalid request arguments", "code": 0, "type": "yii\\base\\invalidparamexception", ] } }
but want:
{ "success": false, "data": { "errors" : 1, <---------------- "name": "exception", "message": "invalid request arguments", "code": 0, "type": "yii\\base\\invalidparamexception", ] } }
because in anyway user 200 response , don know error or mistake.
if using default yii/rest/controller model error send 422. use yii/rest/controller or yii/rest/activecontroller or extend it. or use own serializer
http://www.yiiframework.com/doc-2.0/yii-rest-serializer.html
Comments
Post a Comment