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.

  1. when model validation wrong.
  2. 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

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -