-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponse.cfc
More file actions
133 lines (124 loc) · 5.11 KB
/
Response.cfc
File metadata and controls
133 lines (124 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
component displayname="Response Object" accessors="true" {
property name="data" type="any" required="false" getter="true";
property name="errorCode" type="numeric" required="false" getter="true";
property name="errorDetail" type="any" required="false" getter="true";
property name="errorMessage" type="string" required="false" getter="true";
property name="errors" type="array" required="false" getter="true";
property name="requestDurationInMilliseconds" type="numeric" required="false" getter="true";
property name="requestID" type="numeric" required="false" getter="true";
property name="result" type="any" required="false" getter="true";
property name="stackTrace" type="string" required="false" getter="true";
property name="statusCode" type="numeric" required="false" getter="true";
property name="statusText" type="string" required="false" getter="true";
property name="subErrorCode" type="numeric" required="false" getter="true";
property name="success" type="boolean" required="false" getter="true";
property name="successMessage" type="string" required="false" getter="true";
this.setData("");
this.setErrorCode(0);
this.setErrorDetail("");
this.setErrorMessage("");
this.setErrors([]);
this.setRequestDurationInMilliseconds(0);
this.setRequestID(0);
this.setResult([]);
this.setStackTrace("");
this.setStatusCode(200);
this.setStatusText("OK");
this.setSubErrorCode(0);
this.setSuccess(true);
this.setSuccessMessage("");
/**
* @hint I initialize the component
*/
public Response function init() {
return this;
}
/**
* @hint I append data to the data property object
* @newData I am the data to append onto the data property object
*/
public void function appendData( required any newData ) {
if ( isValid("array", this.getData()) ) {
arrayappend(this.getData(), arguments.newData);
}
else if ( isValid("struct", this.getData()) ) {
structappend(this.getData(), arguments.newData);
}
}
/**
* @hint I append an error element to the errors array
* @theError I am the error message to append onto the errors array
*/
public void function appendError( required string theError ) {
if ( len(trim(arguments.theError)) ) {
arrayappend(this.getErrors(), arguments.theError);
}
}
/**
* @hint I append an error array to the errors array
* @theErrors I am the error array to append onto the errors array
*/
public void function appendErrors( required array theErrors ) {
if ( arrayLen(arguments.theErrors) ) {
arrayappend(this.getErrors(), arguments.theErrors);
}
}
/**
* @hint I return the current state of the bean
* @format I am the return format of the response
* @include_stacktrace I determine whether or not to include the stacktrace in the response.
* @log_error I determine whether or not to log the error (in BugLogHQ).
*/
public any function getResponse( string format = "json", include_stacktrace = false, log_error = true ) {
local.responseStruct = {};
local.responseStruct["data"] = this.getData();
local.responseStruct["errorCode"] = this.getErrorCode();
local.responseStruct["errorDetail"] = this.getErrorDetail();
local.responseStruct["errorMessage"] = this.getErrorMessage();
local.responseStruct["errors"] = this.getErrors();
local.responseStruct["requestDurationInMilliseconds"] = this.getRequestDurationInMilliseconds();
local.responseStruct["requestID"] = this.getRequestID();
local.responseStruct["result"] = this.getResult();
local.responseStruct["stackTrace"] = this.getStackTrace();
local.responseStruct["statusCode"] = this.getStatusCode();
local.responseStruct["statusText"] = this.getStatusText();
local.responseStruct["subErrorCode"] = this.getSubErrorCode();
local.responseStruct["success"] = this.getSuccess();
local.responseStruct["successMessage"] = this.getSuccessMessage();
if ( NOT arguments.include_stacktrace ) {
this.setStackTrace("");
}
if ( arguments.format is "json" ) {
return serializeJSON(this);
}
else if ( arguments.format is "jsonrpc" ) {
local.responseStruct = {};
local.responseStruct["id"] = javaCast("int", this.getRequestID());
local.responseStruct["jsonrpc"] = javaCast("string", "<deleteMe>2.0");
local.responseStruct["error"]["code"] = javaCast("int", this.getErrorCode());
local.responseStruct["error"]["message"] = this.getErrorMessage();
local.responseStruct["error"]["data"] = this.getErrorDetail();
local.responseStruct["result"] = this.getResult();
if ( this.getSuccess() ) {
structDelete(local.responseStruct, "error");
}
else {
structDelete(local.responseStruct, "result");
if ( NOT this.getErrorCode() ) {
local.responseStruct["error"]["code"] = 400;
}
}
return replaceNoCase(serializeJSON(local.responseStruct), "<deleteMe>", "", "all");
}
else if ( arguments.format is "struct" ) {
return local.responseStruct;
}
else {
return this;
}
}
/**
* @hint I catch it if someone passes in a bad method name
*/
public any function onMissingMethod( string missingMethodName, struct missingMethodArguments ) {}
}