Skip to content

Commit

Permalink
LDEV-3739 pass error object to error templates
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitzer committed Jun 10, 2024
1 parent e77f5ec commit 06909b7
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/src/main/java/lucee/runtime/PageContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2285,6 +2285,7 @@ public void handlePageException(PageException pe, boolean setHeader) {
Struct sct = pe.getErrorBlock(this, ep);
variablesScope().setEL(KeyConstants._error, sct);
variablesScope().setEL(KeyConstants._cferror, sct);
variablesScope().setEL(KeyConstants._error, pe.getErrorBlock(this, new ErrorPageImpl()));

doInclude(new PageSource[] { ep.getTemplate() }, false);
return;
Expand Down
102 changes: 102 additions & 0 deletions test/tickets/LDEV3739.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
component extends="org.lucee.cfml.test.LuceeTestCase" labels="error" {

function beforeAll(){

variables.uri = createURI("LDEV3739");
// stash default config
admin
action="getError"
type="server"
password="#request.SERVERADMINPASSWORD#"
returnVariable="variables.errorConfig";
systemOutput(variables.errorConfig, true);

// setup test config
admin
action="updateError"
type="server"
password="#request.SERVERADMINPASSWORD#"
template500="#uri#/500.cfm"
template404="#uri#/404.cfm"
statuscode="true";

admin
action="getError"
type="server"
password="#request.SERVERADMINPASSWORD#"
returnVariable="variables.errorConfig2";
systemOutput(variables.errorConfig2, true);

}

function afterAll(){
// restore default config
admin
action="updateError"
type="server"
password="#request.SERVERADMINPASSWORD#"
template500=variables.errorConfig.str.500
template404=variables.errorConfig.str.404
statuscode= variables.errorConfig.doStatusCode;
}

function run( testResults , testBox ) {
describe( title='LDEV-3739' , body=function(){
it( title='test that the test error templates are configured' , body=function() {
expect( fileExists( variables.errorConfig.templates.404 ) ).toBeTrue();
expect( fileExists( variables.errorConfig.templates.500 ) ).toBeTrue();
});

it( title='test 500 error page has error variable' , body=function() {
var req = _InternalRequest(
template: "#uri#/throw.cfm", // throw an error and trigger 500.cfm
throwonerror: false
);
//fileWrite( uri & "/500_req.json", req.toJson() );
//fileWrite( uri & "/500.json", req.filecontent );
// systemOutput( result.filecontent, true );
// systemOutput( result, true );
expect( isJson( req.filecontent ) ).toBeTrue();
var result = deserializeJSON( req.filecontent );
expect( req.status_code ).toBe( 500 );

expect( result ).toHaveKey( "error" );
expect( result ).toHaveKey( "catch" );
expect( result ).toHaveKey( "cfcatch" );

loop list="browser,datetime,diagnostics,GeneratedContent,HTTPReferer,mailto,QueryString,RemoteAddress,RootCause,Template" item="k" {
expect( result.error ).toHaveKey( k );
}
});

xit( title='test 404 error page has error variable', body=function() {
var req = _InternalRequest(
template : "#uri#/missing.cfm", // trigger 404.cfm
throwonerror: false
);
return; // testing this with internalRequest is difficult

//fileWrite( uri & "/404_req.json", req.toJson() );
//fileWrite( uri & "/404.json", req.filecontent );
expect( req.status_code ).toBe( 404 );
//systemOutput( result.filecontent, true );
//systemOutput( result, true );
expect( isJson( req.filecontent ) ).toBeTrue();
var result = deserializeJSON( req.filecontent );


expect( result ).toHaveKey( "error" );
expect( result ).toHaveKey( "catch" );
expect( result ).toHaveKey( "cfcatch" );

});

});
}

private string function createURI(string calledName){
var baseURI="/test/#listLast(getDirectoryFromPath(getCurrentTemplatePath()),"\/")#/";
return baseURI&""&calledName;
}

}
4 changes: 4 additions & 0 deletions test/tickets/LDEV3739/404.cfm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<cfscript>
systemOutput("I'm a 404 error template", true);
echo(variables.toJson());
</cfscript>
4 changes: 4 additions & 0 deletions test/tickets/LDEV3739/500.cfm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<cfscript>
systemOutput("I'm a 500 error template", true);
echo(variables.toJson());
</cfscript>
3 changes: 3 additions & 0 deletions test/tickets/LDEV3739/Application.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
component {
this.name="LDEV3739";
}
3 changes: 3 additions & 0 deletions test/tickets/LDEV3739/throw.cfm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<cfscript>
throw "I'm an error";
</cfscript>

0 comments on commit 06909b7

Please sign in to comment.