Skip to content

Instantly share code, notes, and snippets.

@wellercs
Created April 6, 2013 20:43
Show Gist options
  • Save wellercs/5327556 to your computer and use it in GitHub Desktop.
Save wellercs/5327556 to your computer and use it in GitHub Desktop.
Example of working with Response.cfc
<cffunction name="validateDemo" access="public" returntype="any" output="false">
<cfargument name="fname" type="string" required="true">
<cfargument name="lname" type="string" required="true">
<cfargument name="email" type="string" required="true">
<cfargument name="throwerror" type="boolean" required="false" default="false">
<cfset var local = {}>
<cftry>
<cfset local.responseobject = new Response()>
<cfset local.responseobject.setData(arguments)>
<cfif arguments.throwerror>
<cfthrow type="customerror" message="test error message" detail="test error detail">
</cfif>
<cfif NOT len(trim(arguments.fname))>
<cfset local.responseobject.appendError("First name is required.")>
</cfif>
<cfif NOT len(trim(arguments.lname))>
<cfset local.responseobject.appendError("Last name is required.")>
</cfif>
<cfif NOT IsValid("email",arguments.email)>
<cfset local.responseobject.appendError("Email address must be valid.")>
</cfif>
<cfif ArrayLen(local.responseobject.getErrors())>
<cfset local.responseobject.setErrorCode(101)>
<cfset local.responseobject.setErrorMessage("Please correct the following:")>
<cfset local.responseobject.setSuccess(false)>
</cfif>
<cfcatch type="any">
<cfset local.responseobject.setErrorCode(100)>
<cfset local.responseobject.setErrorDetail("Message: #cfcatch.message# Detail: #cfcatch.detail#")>
<cfset local.responseobject.setErrorMessage("An error occurred processing your request.")>
<cfset local.responseobject.setSuccess(false)>
</cfcatch>
</cftry>
<cfreturn local.responseobject.getResponse()>
</cffunction>
<h3>Default Response</h3>
<cfset variables.demoresponse = validateDemo(fname="John",lname="Doe",email="[email protected]")>
<cfdump var="#variables.demoresponse#">
<cfdump var="#DeserializeJSON(variables.demoresponse)#">
<hr>
<h3>Validation Error Response</h3>
<cfset variables.demoresponse = validateDemo(fname="John",lname="Doe",email="bademail")>
<cfdump var="#variables.demoresponse#">
<cfdump var="#DeserializeJSON(variables.demoresponse)#">
<hr>
<h3>Unexpected Error Response</h3>
<cfset variables.demoresponse = validateDemo(fname="John",lname="Doe",email="[email protected]",throwerror=true)>
<cfdump var="#variables.demoresponse#">
<cfdump var="#DeserializeJSON(variables.demoresponse)#">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment