Skip to content

Instantly share code, notes, and snippets.

@kevindb
Created June 24, 2014 18:28
Show Gist options
  • Save kevindb/101ffdc0198ee710b2c7 to your computer and use it in GitHub Desktop.
Save kevindb/101ffdc0198ee710b2c7 to your computer and use it in GitHub Desktop.
/* --------------------------------------------------------------------------------------- ----
Blog Entry:
Handling Remote API Errors With Application.cfc's OnError() Event Method
Author:
Ben Nadel / Kinky Solutions
Link:
http://www.bennadel.com/index.cfm?event=blog.view&id=1567
Date Posted:
Apr 17, 2009 at 6:47 PM
---- --------------------------------------------------------------------------------------- */
component displayname="remoteProxy" output=false {
/**
* @hint Returns empty API response object
*/
public struct function newAPIResponse() {
local.response = {
success = true,
errors = [],
data = {}
};
return local.response;
}
/**
* @hint Returns reasonable array of objects from a cfquery
*/
public function queryToArray(
required query query,
string excludeColumns = ""
) {
local.columns = arguments.query.getMetaData().getColumnLabels();
if (arguments.query.recordCount == 1){
for (local.columnIndex=1; local.columnIndex<=arrayLen(local.columns); local.columnIndex++){
local.columnName = local.columns[local.columnIndex];
if(arguments.excludeColumns == "" || !listFindNoCase(arguments.excludeColumns, local.columnName))
local.response[local.columnName] = arguments.query[local.columnName][1];
}
} else {
local.response = arrayNew(1);
for (local.rowIndex=1; local.rowIndex<=arguments.query.recordCount; local.rowIndex++){
local.response[local.rowIndex] = structNew();
for (local.columnIndex=1; local.columnIndex<=arrayLen(local.columns); local.columnIndex++){
local.columnName = local.columns[local.columnIndex];
if(local.columnName != "" && (arguments.excludeColumns == "" || !listFindNoCase(arguments.excludeColumns, local.columnName)))
local.response[local.rowIndex][local.columnName] = arguments.query[local.columnName][local.rowIndex];
}
}
}
return local.response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment