How to deserialize the stacktrace

0
A Json post results in an error. The stacktrace contains valuable information in Json about what is wrong with the post. The stacktrace contains twice the same Json structure. I am thinking to combine several string operation the get the Json part from the stacktrace but I am aware that this will be a wishy-washy solution. What is the best way to get the Json from the stacktrace? Does the provider of the REST service needs to return the error on another way? com.mendix.core.CoreRuntimeException: com.mendix.systemwideinterfaces.MendixRuntimeException: restservices.consume.RestConsumeException: 400. Rest request failed: [HTTP Request: POST 'https://staging.synerg-e.com/api/rest/resource/candidate/create' --> Response status: 400 Bad Request, ETag: null, body: '{"response":{"result":false,"error":true,"request":{"method":"POST","resource":"candidate","operation":"create","path":"\/candidate\/create\/","params":null},"message":"Validation failed","report":{"sex":{"valid":true,"value":null},"initials":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"firstname":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"insertion":{"valid":true,"value":null},"lastname":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"address":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"zipcode":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"city":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"phone":{"valid":true,"value":null},"mobile":{"valid":true,"value":null},"email":{"valid":true,"value":"thijs.schoemaker@pinkelephant.nl"},"birthdate":{"valid":true,"value":null},"birthplace":{"valid":true,"value":null},"nationality":{"valid":true,"value":null},"travelMaximum":{"valid":true,"value":null},"workexperience":{"valid":true,"value":[]},"education":{"valid":true,"value":[]},"intermediaryId":{"valid":true,"value":null},"companyId":{"valid":true,"value":null},"entities":{"valid":true,"value":[]},"establishmentId":{"valid":true,"value":null},"linkedinAccount":{"valid":true,"value":null},"twitterAccount":{"valid":true,"value":null},"facebookAccount":{"valid":true,"value":null},"valid":false}}}'] at com.mendix.core.component.InternalCore.execute(InternalCore.java:394) Caused by: com.mendix.systemwideinterfaces.MendixRuntimeException: restservices.consume.RestConsumeException: 400. Rest request failed: [HTTP Request: POST 'https://staging.synerg-e.com/api/rest/resource/candidate/create' --> Response status: 400 Bad Request, ETag: null, body: '{"response":{"result":false,"error":true,"request":{"method":"POST","resource":"candidate","operation":"create","path":"\/candidate\/create\/","params":null},"message":"Validation failed","report":{"sex":{"valid":true,"value":null},"initials":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"firstname":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"insertion":{"valid":true,"value":null},"lastname":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"address":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"zipcode":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"city":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"phone":{"valid":true,"value":null},"mobile":{"valid":true,"value":null},"email":{"valid":true,"value":"thijs.schoemaker@pinkelephant.nl"},"birthdate":{"valid":true,"value":null},"birthplace":{"valid":true,"value":null},"nationality":{"valid":true,"value":null},"travelMaximum":{"valid":true,"value":null},"workexperience":{"valid":true,"value":[]},"education":{"valid":true,"value":[]},"intermediaryId":{"valid":true,"value":null},"companyId":{"valid":true,"value":null},"entities":{"valid":true,"value":[]},"establishmentId":{"valid":true,"value":null},"linkedinAccount":{"valid":true,"value":null},"twitterAccount":{"valid":true,"value":null},"facebookAccount":{"valid":true,"value":null},"valid":false}}}'] at com.mendix.core.actionmanagement.ActionManager.executeSync(ActionManager.java:198) Caused by: restservices.consume.RestConsumeException: 400. Rest request failed: [HTTP Request: POST 'https://staging.synerg-e.com/api/rest/resource/candidate/create' --> Response status: 400 Bad Request, ETag: null, body: '{"response":{"result":false,"error":true,"request":{"method":"POST","resource":"candidate","operation":"create","path":"\/candidate\/create\/","params":null},"message":"Validation failed","report":{"sex":{"valid":true,"value":null},"initials":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"firstname":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"insertion":{"valid":true,"value":null},"lastname":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"address":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"zipcode":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"city":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"phone":{"valid":true,"value":null},"mobile":{"valid":true,"value":null},"email":{"valid":true,"value":"thijs.schoemaker@pinkelephant.nl"},"birthdate":{"valid":true,"value":null},"birthplace":{"valid":true,"value":null},"nationality":{"valid":true,"value":null},"travelMaximum":{"valid":true,"value":null},"workexperience":{"valid":true,"value":[]},"education":{"valid":true,"value":[]},"intermediaryId":{"valid":true,"value":null},"companyId":{"valid":true,"value":null},"entities":{"valid":true,"value":[]},"establishmentId":{"valid":true,"value":null},"linkedinAccount":{"valid":true,"value":null},"twitterAccount":{"valid":true,"value":null},"facebookAccount":{"valid":true,"value":null},"valid":false}}}'] at com.mendix.util.classloading.Runner.doRunUsingClassLoaderOf(Runner.java:36) Caused by: 400. Rest request failed: [HTTP Request: POST 'https://staging.synerg-e.com/api/rest/resource/candidate/create' --> Response status: 400 Bad Request, ETag: null, body: '{"response":{"result":false,"error":true,"request":{"method":"POST","resource":"candidate","operation":"create","path":"\/candidate\/create\/","params":null},"message":"Validation failed","report":{"sex":{"valid":true,"value":null},"initials":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"firstname":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"insertion":{"valid":true,"value":null},"lastname":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"address":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"zipcode":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"city":{"errors":["Waarde mag niet leeg zijn"],"valid":false,"value":null},"phone":{"valid":true,"value":null},"mobile":{"valid":true,"value":null},"email":{"valid":true,"value":"thijs.schoemaker@pinkelephant.nl"},"birthdate":{"valid":true,"value":null},"birthplace":{"valid":true,"value":null},"nationality":{"valid":true,"value":null},"travelMaximum":{"valid":true,"value":null},"workexperience":{"valid":true,"value":[]},"education":{"valid":true,"value":[]},"intermediaryId":{"valid":true,"value":null},"companyId":{"valid":true,"value":null},"entities":{"valid":true,"value":[]},"establishmentId":{"valid":true,"value":null},"linkedinAccount":{"valid":true,"value":null},"twitterAccount":{"valid":true,"value":null},"facebookAccount":{"valid":true,"value":null},"valid":false}}}'] at restservices.consume.RestConsumer.request(RestConsumer.java:487) at restservices.consume.RestConsumer.postObject(RestConsumer.java:647) at restservices.actions.post.executeAction(post.java:38) at restservices.actions.post.executeAction(post.java:20) at com.mendix.systemwideinterfaces.core.UserAction.execute(UserAction.java:49) at com.mendix.core.actionmanagement.CoreAction.doCall(CoreAction.java:260) at com.mendix.core.actionmanagement.CoreAction.call(CoreAction.java:248) at com.mendix.core.actionmanagement.ActionManager$1.execute(ActionManager.java:188) at com.mendix.util.classloading.Runner.doRunUsingClassLoaderOf(Runner.java:32) at com.mendix.core.actionmanagement.ActionManager.executeSync(ActionManager.java:194) at com.mendix.core.component.InternalCore.execute(InternalCore.java:386) at com.mendix.modules.microflowengine.actions.actioncall.JavaAction.execute(JavaAction.scala:52) at com.mendix.modules.microflowengine.microflow.impl.MicroflowObject.execute(MicroflowObject.java:47) at com.mendix.modules.microflowengine.microflow.impl.MicroflowImpl.executeAfterBreakingIfNecessary(MicroflowImpl.java:192) at com.mendix.modules.microflowengine.microflow.impl.MicroflowImpl.executeAction(MicroflowImpl.java:149) at com.mendix.systemwideinterfaces.core.UserAction.execute(UserAction.java:49) at com.mendix.core.actionmanagement.CoreAction.doCall(CoreAction.java:260) at com.mendix.core.actionmanagement.CoreAction.call(CoreAction.java:248) at com.mendix.core.actionmanagement.ActionManager$1.execute(ActionManager.java:188) at com.mendix.util.classloading.Runner.doRunUsingClassLoaderOf(Runner.java:32) at com.mendix.core.actionmanagement.ActionManager.executeSync(ActionManager.java:194) at com.mendix.core.component.InternalCore.execute(InternalCore.java:386) at com.mendix.webui.actions.client.ExecuteAction.execute(ExecuteAction.java:135) at com.mendix.webui.requesthandling.ClientRequestHandler$$anonfun$handleRequest$1.apply$mcV$sp(ClientRequestHandler.scala:322) at com.mendix.webui.requesthandling.ClientRequestHandler$$anonfun$handleRequest$1.apply(ClientRequestHandler.scala:315) at com.mendix.webui.requesthandling.ClientRequestHandler$$anonfun$handleRequest$1.apply(ClientRequestHandler.scala:315) at com.mendix.core.session.Worker$$anonfun$receive$3$$anonfun$2$$anon$1.execute(ActionDispatching.scala:145) at com.mendix.util.classloading.Runner.doRunUsingClassLoaderOf(Runner.java:32) at com.mendix.core.session.Worker$$anonfun$receive$3$$anonfun$2.apply(ActionDispatching.scala:147) at scala.util.Try$.apply(Try.scala:191) at com.mendix.core.session.Worker$$anonfun$receive$3.applyOrElse(ActionDispatching.scala:141) at akka.actor.Actor$class.aroundReceive(Actor.scala:465) at com.mendix.core.session.Worker.aroundReceive(ActionDispatching.scala:137) at akka.actor.ActorCell.receiveMessage(ActorCell.scala:516) at akka.actor.ActorCell.invoke(ActorCell.scala:487) at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:238) at akka.dispatch.Mailbox.run(Mailbox.scala:220) at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:393) at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
asked
1 answers
0

Whatever approach you choose, you cannot rely on your solution working for future version of the Rest module as I assume the exceptions it throws are not part of it's documented contract.

What is the best way to get the Json from the stacktrace?

What you could do here is create you own Java action as a wrapper around the Post Java action. This would call the Post java action, and catch any exceptions. When handling the Exception, you try to retrieve the cause, and if the cause is a RestConsumeException, there is probably a way to specifically get the json you're looking for from there. Then you would still need to deserialize the json to something you can use in you Mendix logic. I could try to write the Java code here, but it's probably easier to do this while debugging as you get immediate feedback that way.

Does the provider of the REST service needs to return the error on another way?

I am no REST expert, but yes I think so. the 400 error should be used if the payload was malformed. It seems that in this case it was not necessarily malformed, but just did not pass the business validation rules. In this case I think it would have been better if the return object for this Rest operation would have specific fields to use for this type of feedback on failing validation.

answered