[ContentBox 4.3.0-snapshot][ColdBox 5.6.2+1021]RenderData-statusCode overwritten when using html format

Skip to first unread message


Jan 19, 2021, 8:09:44 AM1/19/21
to ColdBox Platform
I am extending my contentbox site with a small api. For this reason I created a function to return responses when something went wrong which all events can call to return something other then a valid response.
In this function I am using renderData to return the errorMessage/ statusCode etc.
I noticed some stange behaviour:
When i set my request-Accept header to json/XML everything is working as intended and i recieve the expected status codes.
However when I try to get a html-response (accept header: */*; text/plain or text/html) I got an error message that the view of the event could not be found.
As I added the "formatsView" argument to point to a valid cfm view, or added a event.setView("..."); call I did not get the correct statusCode annymore as I only got a 200 OK.
Is there any possibility to create a universal "httpErrorResponse" function which will also work with html/text responses without overwriting the statusCode?
Do I have to set the statusCode and message in my view again? I am not sure if I can access these in the view.
Thanks for any help in advance!

private function httpResponse( required event, required numeric statusCode, required boolean error, required string message, required string logText ) {
     // Log Locally
     log.warn( arguments.logText, getHTTPRequestData( ) );
     // Setup Response
     local.response = getModel( "Response@cb" )
         .setError( arguments.error )
         .addMessage( arguments.message )
         .setStatusCode( arguments.statusCode )
         .setStatusText( arguments.message );
     // Render Error Out
         data = local.response.getDataPacket( ),
         formats = "json,xml,html,pdf",
         formatsView = "main/error",
         contentType = local.response.getContentType( ),
         statusCode = local.response.getStatusCode( ),
         statusText = local.response.getStatusText( ),
         location = local.response.getLocation( ),
         isBinary = local.response.getBinary( )

Reply all
Reply to author
0 new messages