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

0
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." } }  
asked
3 answers
1

- 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

answered
0

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:

https://docs.mendix.com/howto/logic-business-rules/set-up-error-handling 

https://docs.mendix.com/refguide/log-message 

answered
0

Thanks。

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.

eg.

Common return:

{

rfid:000232,

matcode:”sdf”,

weighting:99

}

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?

 

 

answered