CodeCharge Studio All ASP PHP Perl ColdFusion Java C#.NET VB.NET

Display Custom Error Messages

This example shows how to process critical errors. In this case, the error condition is based on the availability of an input parameter called task_id which is received over the URL.

Method 1

In the first instance, we want to prevent the page from displaying if the input parameter is not received. We place code in the After Initialize event of the page to check for the input parameter and display an error message if no value is received for it.

  1. Add the After Initialize event to the page.
  2. Within the event, add the code below:

ASP

Function Page_AfterInitialize(Sender)

  If IsEmpty(CCGetParam("task_id", Empty)) Then
    UnloadPage
    response.write("Error: ""task_id"" parameter is not specified.")
    response.end
  End If

End Function

PHP

function Page_AfterInitialize(& $sender) { 

  if (CCGetFromGet("task_id","") == "") { 
    print ("Error: \"task_id\" parameter is not specified.");
    exit;
  }

}

Perl

sub Page_AfterInitialize() { 

  if (CCGetFromGet("task_id","") eq "") { 
    if (!$isHTTPHeader) {
      print $page_header;
      print "Content-type: text/html\n\n";
    }
    print "Error: \"task_id\" parameter is not specified.\n";
    exit 0;
  }

}

ColdFusion

<!---Page_AfterInitialize --->
  <CFMODULE Template="CCGetParam.cfm" strName="task_id">
  <CFIF CCGetParam EQ ""> 
    <CFOUTPUT>Error: "task_id" parameter is not specified.</CFOUTPUT>
    <CFEXIT>
  </CFIF>

VB.Net

'Page_AfterInitialize 

  If IsNothing(Request.QueryString("task_id")) OR Request.QueryString("task_id") = "" Then  
    Response.Write("Error: ""task_id"" parameter is not specified.")
    Response.[End]
  End If

C#

//Page_AfterInitialize 

  if (Request.QueryString["task_id"] == null || Request.QueryString["task_id"].Length == 0) {
    Response.Write("Error: 'task_id' parameter is not specified.");
    Response.End();
  }

Java

//Page_AfterInitialize 

  if (StringUtils.isEmpty(e.getPage().getHttpGetParams().getParameter("task_id"))) { 
    Utils.printFatalError("Error: \"task_id\" parameter is not specified.", e.getPage().getResponse());
  }

Method 2

In the second instance, we want to prevent a form called Tasks from displaying if the input parameter task_id is not received.

  1. Add a Label control on the page next to the Tasks record form.
  2. Rename the Label to Tasks_Error.
  3. Add the After Initialize event to the page.
  4. Within the event, add the code below:

ASP

Function Page_AfterInitialize()

  If IsEmpty(CCGetParam("task_id", Empty)) Then
    Tasks.Visible=False
    Tasks_Error.Value="Error: ""task_id"" parameter is not specified."
  End If

End Function

PHP

function Page_AfterInitialize() {
global $Tasks;
global $Tasks_Error;

  if (CCGetFromGet("task_id","") == "") { 
    $Tasks->Visible = False;
    $Tasks_Error->SetValue("Error: \"task_id\" parameter is not specified.");
  }

}

Perl

sub Page_AfterInitialize() {

  if (CCGetFromGet("task_id","") eq "") { 
    $Tasks->{Visible} = 0;
    $Tasks_Error->SetValue("Error: \"task_id\" parameter is not specified.");
  }

}

ColdFusion

Set the Default Value property of the 'Task_Error' label to "Error: 'task_id' parameter is not specified."

<!---Page_AfterInitialize --->
  <CFMODULE Template="CCGetParam.cfm" strName="task_id">
  <CFIF CCGetParam EQ ""> 
    <CFSET hideTasks=True>
  <CFELSE>
    <CFSET hideTasks_Error=True>
  </CFIF>
 

ColdFusion with Templates

  1. Set the Default Value property of the Task_Error label to "Error: 'task_id' parameter is not specified."
  2. Add "<!-- BEGIN Label Tasks_Error -->" at the beginning and "<!-- END Label Tasks_Error -->" at the end of the Label control:

  3. <!-- BEGIN Label Tasks_Error -->
    {Tasks_Error}
    <!-- END Label Tasks_Error -->
  4. Add the code below:
<!---Page_AfterInitialize --->
  <CFMODULE Template="CCGetParam.cfm" strName="task_id">
  <CFIF CCGetParam EQ ""> 
    <CFSET hideTasks=True>
  <CFELSE>
    <CFSET hideTasks_Error=True>
  </CFIF>
 

Java

//Page_AfterInitialize 

  if (StringUtils.isEmpty(e.getPage().getHttpGetParams().getParameter("task_id"))) { 
    e.getPage().getRecord("Tasks").setVisible(false);
    e.getPage().getControl("Tasks_Error").setValue("Error: \"task_id\" parameter is not specified.");
 }

VB.Net

  1. Set the Default Value property of the Task_Error Label to "Error: 'task_id' parameter is not specified."
  2. Add the code below:
'Page_AfterInitialize 

  If IsNothing(Request.QueryString("task_id")) OR Request.QueryString("task_id") = "" Then  
    TasksHolder.Visible=False
  Else
    Tasks_Error.Visible=False
  End If

C#

  1. Set the Default Value property of the Task_Error Label to "Error: 'task_id' parameter is not specified."
  2. Add the code below:
//Page_AfterInitialize 

  if (Request.QueryString["task_id"] == null || Request.QueryString["task_id"].Length == 0) {
    TasksHolder.Visible = false;
  } else {
    Tasks_Error.Visible = false;
  }


On-line, printable versions and updates