RFC 7807/9457: HTTP status code 400 with data

I have to answer in this standard: RFC 7807, RFC 9457

How can I set the "response.statuscode" to 400 in a service function so that I can respond with a standardized, detailed error message? Is there already something built in and automatic?

The detour via "OnModuleException" is quite cumbersome.

The easiest and "official" way to respond error in XData is to raise exceptions. If you want to define a specific status code, you can use EXDataHttpException:

raise EXDataHttpException.Create(400, 'Some error message');

If you want to add more detailed information from the exception, you can simply create your own exception class and inherit from EXDataHttpException, and then add more data in the Create constructor.

If you want the JSON response to be more specific, with your own format and additional properties, OnModuleException is the best way to go, I don't see it as cumbersome.

Just check if the exception raise is your type (e.g., EHttpProblemException) and serialize the exception properties accordingly.

Okay, I have my own exception type and can call a raise.

What do I need to do in Exception.Create (passing multiple parameters) and in the OnModuleException body to generate my JSON response? JSON data (string/object) is available. Unfortunately, I can't find an example.

Or am I just thinking too complicated?