How to return a custom error response body in published rest api.

When publish a rest api in mendix. When call api from 3rd app,  If there is some validation error, or other logic checking errors in micoflow,  I want to return detail message to client. How to do it?   eg.  Post json from 3rd app: {   "spec_id_4_api": 2323,   "WeightGross": 46445,   "EPC_Tag": "" } By business logic, if EPC_Tag is empty or is invalid value, or spec_id_4_api is not a exists value.  …    I want to return a detail message from api response. Now, I use throw webservice exception action when has error, but the response body is too common. Response body Download   { "error": { "code": 500, "message": "An unexpected error occurred." } }  
3 answers


I read the document.  But, it doesn’t solve my confustion.

Yes, if i want to do some error handling or log error messges or display error messge on web or native page,  It is fine.

But,   I am using a Java Native Android app to call the rest api  published by mendix app.   

When use on app input some values , I will post the data to mendix api.  If everything is ok,  the rest will return the new create entity data.  If some logic error( this by logic,not only data validation) ,  Client app expect to recieve a error code and  messge, the messge will show on 3re Android app to user.

The problem is:  The common return format is different with error message.


Common return:






But if error happen

it should return:

{ "error": { "code": 500, "message": "Operation fail, the reason is:XXXXXXXXXX" } }


If any place where i can change the message content before return response to client?




- When you create the REST service and attach a Microflow, the Response type for the service will be String, so you should send only string

- But normally what I follow is, I make the Microflow return HTTP response instead of String, so in the service the Response type will be changed to HTTPResponse. See the Green box in the image below.

- This also gave me the flexibility of changing the Error code. So for specific errors, I returned Specific messages along with appropriate error code (attached the Microflow as well)

- In the below Sub microflow, I created HTTP response and attached it to the HTTP header object.

- The reason phrase will be like the title and content will be actual JSON

- Errorcode can be set on the status code in HTTP response


You can set custom error handling on your integration activities, file a custom log message and send a custom message to the user

some background: