richard.herbert
09/21/2022, 3:48 PMcomponent restHandler {
...
}
wil-shiftinsert
09/21/2022, 3:54 PMsuper.onInvalidHttpMethod(event….) ?
wil-shiftinsert
09/21/2022, 3:57 PMrichard.herbert
09/21/2022, 4:02 PMfunction onInvalidHTTPMethod( event, rc, prc, faultAction, eventArguments ) {
super.onInvalidHTTPMethod( event, rc, prc, faultAction, eventArguments );
}
in my v1:BaseHandler
I've not knowingly protected it, there's no secured
in the definition. Is there another way I might have done that?
I don't understand where that error message is coming from either!richard.herbert
09/21/2022, 4:06 PMPage /layouts/Main.cfm ... not found
wil-shiftinsert
09/21/2022, 4:06 PMrichard.herbert
09/21/2022, 4:17 PMpost( '/signin', 'Authentication.signin' );
in my Route.cfc
which directs POST
events to the right place. Obviously that hander isn't secured. I was expecting a GET
to be caught by the onInvalidHTTPMethod()
which it is (thank you writedump()
) but if I try to call the framework onInvalidHTTPMethod()
it fails as shown.richard.herbert
09/21/2022, 4:17 PMcomponent restHandler {
is not the same as
component extends=coldbox.system.RestHandler {
With component extends=coldbox.system.RestHandler {
I can use super
to access the extended component.
A design "feature"?wil-shiftinsert
09/21/2022, 4:18 PMcomponent extends=coldbox.system.RestHandler
wil-shiftinsert
09/21/2022, 4:18 PMrichard.herbert
09/21/2022, 4:19 PMrichard.herbert
09/21/2022, 4:20 PMwil-shiftinsert
09/21/2022, 4:21 PMrichard.herbert
09/21/2022, 4:22 PMwil-shiftinsert
09/21/2022, 4:22 PMrichard.herbert
09/21/2022, 4:31 PMfunction onInvalidHTTPMethod( event, rc, prc, faultAction, eventArguments ) {
event.getResponse()
.setError( true )
.setErrorMessage( 'Method Not Allowed' )
.setStatus( 405 );
}
in my BaseHandler
without it complaining that it can't find the view 🤔wil-shiftinsert
09/21/2022, 5:50 PMarguments.event
.getResponse()
.setError( true )
.addMessage(
"InvalidHTTPMethod Execution of (#arguments.faultAction#): #arguments.event.getHTTPMethod()#"
)
.setStatusCode( arguments.event.STATUS.NOT_ALLOWED )
.setStatusText( "Invalid HTTP Method" );
// Render Error Out
arguments.event.renderData(
type = arguments.prc.response.getFormat(),
data = arguments.prc.response.getDataPacket( reset = this.resetDataOnError ),
contentType = arguments.prc.response.getContentType(),
statusCode = arguments.prc.response.getStatusCode(),
statusText = arguments.prc.response.getStatusText(),
location = arguments.prc.response.getLocation(),
isBinary = arguments.prc.response.getBinary()
);
wil-shiftinsert
09/21/2022, 5:51 PMrichard.herbert
09/22/2022, 6:01 AMunless you want to augment the parent handler with some extra functionalityYes, that's what I want to do. Ideally I'd prefer not to copy the framework version of the method into my
BaseHandler.cfc
and adapt it, better to capture the output and alter the contents, like the message, before returning it to the caller.
The resthandler annotation ... is not extending the coldbox.system.RestHandler ... just adding methods from the RestHandler which were not thereAn interesting but important distinction that I hadn't realised, thank you - I'll look to add something to the docs.
What happens if you remove the onInvalidHTTPMethod from your basehandler?• With
component _restHandler_ {
in my BaseHandler.cfc
and no local onInvalidHTTPMethod()
it all works as advertised
• With component _extends_=coldbox.system.RestHandler {
the same, as you would expect
• With component _restHandler_ {
and a copy of the framework onInvalidHTTPMethod()
in my BaseHandler.cfc
the local version of the method is called as expected
• With component _extends_=coldbox.system.RestHandler {
the same, as you would expect
• Now with component _restHandler_ {
and my own simple local onInvalidHTTPMethod()
as shown above, I get the view or layout not found error
• And with component _extends_=coldbox.system.RestHandler {
I get the same error
• Going back to component _restHandler_ {
with this locally I get the same not found error
function onInvalidHTTPMethod( event, rc, prc, faultAction, eventArguments ) {
super.onInvalidHTTPMethod( event, rc, prc, faultAction, eventArguments );
}
• But with component _extends_=coldbox.system.RestHandler {
and the same super
call it works as you would expect
The lesson learnt, as you said, component _restHandler_ {
is not the same as component _extends_=coldbox.system.RestHandler {
So, ideally, I'd like to capture the product of the super
call, change a part of the contents like the message, and return the packet back to the caller.
Do you think this possible or am I trying to bend the space-time continuum?wil-shiftinsert
09/22/2022, 9:30 AMsuper
call instead of creating your own. There’s only two things happening in this call
1. set some properties of the response object
2. render the response with event.renderData
You want to modify nr 1, so different message probably. And you have to do step 2 anyway.wil-shiftinsert
09/22/2022, 9:36 AMevent.getResponse()
.setError( true )
.setErrorMessage( 'Method Not Allowed' )
.setStatus( 405 );
is rendering a response, because that’s what happening with all your normal responses.
The point is: all normal responses are wrapped by the aroundHandler which does a event.renderdata on your prc.response (or event.getResponse() which is the same object). Your own onInvalidHTTPMethod will not be wrapped by this aroundhandler so you have to do the rendering yourself. If you take a look at the box Resthandler you will see that most exception handling ends with a renderdata()
call.
Considering all this I think it is pretty useless to try to capture the onInvalidHTTPMethod of super. Just create your own method which renders correctly and everything will be fine. If you don’t want to recreate this onInvalidHttpMethod in every handler, I would suggest you create your own BaseHandler by extending the coldbox rest handler and extend all other handlers from your own Basehandlers. That’s how we do it here. We also wanted some modifications in the around method and some other exception handling (like the onValidationException). This gives us maximum flexibility.
We also wanted to modify the response format, so we created our own Response object by extending the coldbox version. I described this proces here in my blog https://shiftinsert.nl/problem-details-for-http-apis-modifying-your-coldbox-rest-handler-response/ . Actually it is quite simple and described in the coldbox manual here https://coldbox.ortusbooks.com/digging-deeper/rest-handler#extending-the-response-objectrichard.herbert
09/22/2022, 4:49 PMsuper
I always see it as the solution irrespective of the size of the problem.
I'll have a sit and think about your other points. I'd certainly like to have control of my own response object.
Once again, thank you for your thoughts and advice.