18
Views
1
Comments
Global exception handler for exposed API
Application Type
Mobile, Reactive, Service
Service Studio Version
11.55.39 (Build 64335)
Platform Version
11.38.0 (Build 45971)

Hi,

I'd like to create a global exception handler for my exposed api.

Since I want to return a custom response with a error code (not http error code but internal error code 001,002...) based on the kind of exception, I have a handler for each user exception:


The problem is that I have to repeat these exception handlers in every exposed API function and this is not a good practice.

I'd like to have something like the OnException event in UI Flow


The OnResponse event doesn't work because this happens after the exception is thrown, so I cannot handle exception there.

Any idea?




2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Alessandro,

What you want isn't possible, there's no global exception handler for REST APIs. In practice this means you indeed need to catch every possible exception in every method if you want to send a custom error message.

That said, you could have a single User Exception instead of the 7 you have, and have an exception message that actually contains either the actual result code to send or some identification you can use to map it to a result code. Thus you only need a single handler in every method (besides perhaps a catch-all for unexpected errors, but even that can be done via inspection of the error).

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.