Thank you for sharing your workaroud (I thought not a lot of people have posted about the REST API) ^_^
Indeed your workaround is the only solution for this case because REST component methods (i.e CFC with restPath and httpMethod attributes) are executed after onRequestStart() and their exception are NOT implicitly dispatched to onError().
Notice that ONLY uncatched exception thrown in REST components initialization block (i.e outside its functions) are implicitly dispatched to onError().
Of course this behaviour requires restSetting.skipCFCWithError set to FALSE and these exceptions are thrown after onRequestStart().
In my situation I updated custom keys in the REQUEST scope (something like REQUEST.errorOccurred and REQUEST.thrownException). Also to avoid checking for all successful 2xx HTTP status values.
I also have to handle exception occuring before onRequestStart and REST components (e.g In Application.cfc initialization block) and I use the same REQUEST scope solution.
But I will try to mix with your workaround too because I'm particularly interested by the PageContext.getResponse().isCommitted() value.
One of my problem is that the solution implies a design convention between developpers because they have to be aware that onRequestEnd() will check for the processing status and may send a server response then.
This means that they should rethrow the exception if they've handled it (in order to have an 5xx HTTP status code). Also they may have to :
- Provide an access to the exception for onRequestEnd() as PageContext.getException() and PageContext.getErrorData() will unlikely reflect the CF exception.
- NOT send an error response from REST components because it may be ignored by the cfcontent(reset=true){} in onRequestEnd().
- In my case, custom key / value in the REQUEST scope must be updated on errors...
Even if there's no perfect solution for this issue, it SHOULD BE taken in consideration because it is a valuable feature (especially in enterprise). Of course there are frameworks...but...
And again, thanks for sharing such experience, really appreciated
PS: I'm using Lucee 4.5.x / Tomcat 8.x (didn't tried 5.x yet...).