Skip to content

Commit c07395e

Browse files
authored
Merge pull request #131 from coldbox-modules/patch/improve-error-handling-no-reason
👌 IMPROVE: Add support for missing 'reason' key in error handlers
2 parents 3e954ae + 823f415 commit c07395e

File tree

4 files changed

+52
-39
lines changed

4 files changed

+52
-39
lines changed

models/SearchBuilder.cfc

+12-8
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,12 @@ component accessors="true" {
177177
* @fields Array or list of fields to pull term vectors on
178178
* @options Any custom parameters to send with the request.
179179
*/
180-
struct function getTermVectors( string id = "", any fields = "", struct options = {} ){
181-
var args = arguments;
180+
struct function getTermVectors(
181+
string id = "",
182+
any fields = "",
183+
struct options = {}
184+
){
185+
var args = arguments;
182186
args.indexName = variables.index;
183187

184188
return getClient().getTermVectors( argumentCollection = args );
@@ -196,13 +200,13 @@ component accessors="true" {
196200
return this;
197201
}
198202

199-
/**
203+
/**
200204
* Backwards compatible getter for max result size
201-
*
205+
*
202206
* @deprecated
203207
*/
204208
any function getMaxRows(){
205-
return getSize();
209+
return getSize();
206210
}
207211

208212
/**
@@ -217,13 +221,13 @@ component accessors="true" {
217221
return this;
218222
}
219223

220-
/**
224+
/**
221225
* Backwards compatible getter for start row
222-
*
226+
*
223227
* @deprecated
224228
*/
225229
any function getStartRow(){
226-
return getFrom();
230+
return getFrom();
227231
}
228232

229233
/**

models/io/HyperClient.cfc

+15-8
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ component accessors="true" threadSafe singleton {
453453
} else if ( reindexResult.keyExists( "error" ) ) {
454454
throw(
455455
type = "cbElasticsearch.HyperClient.ReindexFailedException",
456-
message = "The reindex action failed with response code [#reindexResult.status#]. The cause of this exception was #reindexResult.error.reason#",
456+
message = "The reindex action failed with response code [#reindexResult.status#]. The cause of this exception was #reindexResult.error.reason ?: "None"#",
457457
extendedInfo = getUtil().toJSON( reindexResult )
458458
);
459459
}
@@ -538,13 +538,18 @@ component accessors="true" threadSafe singleton {
538538
* @params struct Struct of query parameters to influence the request. For example: `"offsets": false }`
539539
* @options struct Body payload to send. For example: `{ "filter": { "max_num_terms": 3 } }`
540540
*/
541-
struct function getTermVectors( required string indexName, string id = "", any fields = [], struct options = {} ){
541+
struct function getTermVectors(
542+
required string indexName,
543+
string id = "",
544+
any fields = [],
545+
struct options = {}
546+
){
542547
arguments.options[ "fields" ] = arguments.fields;
543-
if ( !isArray( arguments.options["fields"] ) ) {
544-
arguments.options["fields"] = listToArray( arguments.options["fields"] );
548+
if ( !isArray( arguments.options[ "fields" ] ) ) {
549+
arguments.options[ "fields" ] = listToArray( arguments.options[ "fields" ] );
545550
}
546551

547-
var endpoint = [arguments.indexName, "_termvectors" ];
552+
var endpoint = [ arguments.indexName, "_termvectors" ];
548553
if ( arguments.id != "" ) {
549554
endpoint.append( arguments.id );
550555
}
@@ -1031,7 +1036,7 @@ component accessors="true" threadSafe singleton {
10311036
deleteRequest.setQueryParam( param.name, param.value );
10321037
} );
10331038

1034-
var response = deleteRequest.send();
1039+
var response = deleteRequest.send();
10351040
var deleteResult = response.json();
10361041

10371042
if ( arguments.throwOnError && structKeyExists( deleteResult, "error" ) ) {
@@ -1206,10 +1211,12 @@ component accessors="true" threadSafe singleton {
12061211
item.update.keyExists( "error" )
12071212
&& item.update.error.keyExists( "root_cause" )
12081213
)
1209-
? " Reason: #isArray( item.update.error.root_cause ) ? item.update.error.root_cause[ 1 ].reason : item.update.error.root_cause.reason#"
1214+
? " Reason: #isArray( item.update.error.root_cause ) ? ( item.update.error.root_cause[ 1 ].reason ?: "None" ) : (
1215+
item.update.error.root_cause.reason ?: "None"
1216+
)#"
12101217
: (
12111218
structKeyExists( item.update, "error" )
1212-
? " Reason: #item.update.error.reason#"
1219+
? " Reason: #item.update.error.reason ?: "None"#"
12131220
: ""
12141221
);
12151222
throw(

models/logging/LogstashAppender.cfc

+21-21
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ component
123123
* Runs on registration
124124
*/
125125
public LogstashAppender function onRegistration() output=false{
126-
try{
126+
try {
127127
ensureDataStream();
128128
variables._dataStreamAssured = true;
129-
} catch( any e ){
129+
} catch ( any e ) {
130130
createObject( "java", "java.lang.System" ).err.println(
131131
"Unable to create data stream. The attempt to communicate with the Elasticsearch server returned: #e.message# - #e.detail#. Your ability to log messages with this appender may be compromised."
132132
);
@@ -138,11 +138,10 @@ component
138138
* Write an entry into the appender.
139139
*/
140140
public void function logMessage( required any logEvent ) output=false{
141-
142-
if( !variables._dataStreamAssured ){
141+
if ( !variables._dataStreamAssured ) {
143142
this.onRegistration();
144143
// skip out if there was a communication failure
145-
if( !variables._dataStreamAssured ){
144+
if ( !variables._dataStreamAssured ) {
146145
return;
147146
}
148147
}
@@ -151,8 +150,11 @@ component
151150

152151
try {
153152
var document = newDocument().new( index = getProperty( "dataStream" ), properties = logObj );
154-
if( getProperty( "async" ) ){
155-
variables.asyncManager.newFuture().withTimeout( getProperty( "asyncTimeout" ) ).run( () => document.create() );
153+
if ( getProperty( "async" ) ) {
154+
variables.asyncManager
155+
.newFuture()
156+
.withTimeout( getProperty( "asyncTimeout" ) )
157+
.run( () => document.create() );
156158
} else {
157159
document.create();
158160
}
@@ -166,12 +168,12 @@ component
166168
extraInfo = { "logData" : logObj, "exception" : e },
167169
category = e.type
168170
);
169-
var appendersMap = application.wirebox.getLogbox().getAppenderRegistry();
171+
var appendersMap = application.wirebox.getLogbox().getAppenderRegistry();
170172
// Log errors out to other appenders besides this one
171173
appendersMap
172174
.keyArray()
173175
.filter( function( key ){
174-
return lcase( key ) != lcase( getName() );
176+
return lCase( key ) != lCase( getName() );
175177
} )
176178
.each( function( appenderName ){
177179
appendersMap[ appenderName ].logMessage( eLogEvent );
@@ -366,7 +368,9 @@ component
366368
if ( propertyExists( "lifecyclePolicy" ) ) {
367369
policyBuilder.setPhases( getProperty( "lifecyclePolicy" ) );
368370
} else {
369-
policyBuilder.hotPhase( rollover=getProperty( "rolloverSize" ) ).withDeletion( age = getProperty( "retentionDays" ) );
371+
policyBuilder
372+
.hotPhase( rollover = getProperty( "rolloverSize" ) )
373+
.withDeletion( age = getProperty( "retentionDays" ) );
370374
}
371375

372376
policyBuilder.save();
@@ -407,7 +411,9 @@ component
407411
],
408412
"data_stream" : {},
409413
"priority" : getProperty( "indexTemplatePriority" ),
410-
"_meta" : { "description" : "Index Template for cbElasticsearch Logs ( DataStream: #dataStreamName# )" }
414+
"_meta" : {
415+
"description" : "Index Template for cbElasticsearch Logs ( DataStream: #dataStreamName# )"
416+
}
411417
}
412418
);
413419

@@ -645,7 +651,7 @@ component
645651
"dynamic_templates" : [
646652
{
647653
"user_info_fields" : {
648-
"path_match" : "user.info.*",
654+
"path_match" : "user.info.*",
649655
"match_mapping_type" : "string",
650656
"mapping" : {
651657
"type" : "text",
@@ -682,17 +688,11 @@ component
682688
},
683689
"error" : {
684690
"type" : "object",
685-
"properties" : {
686-
"extrainfo" : { "type" : "text" }
687-
}
691+
"properties" : { "extrainfo" : { "type" : "text" } }
688692
},
689693
"message" : {
690694
"type" : "text",
691-
"fields" : {
692-
"keyword" : {
693-
"type" : "keyword", "ignore_above" : 512
694-
}
695-
}
695+
"fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 512 } }
696696
},
697697
"event" : {
698698
"type" : "object",
@@ -709,7 +709,7 @@ component
709709
"properties" : {
710710
"signature" : { "type" : "keyword" },
711711
"isSuppressed" : { "type" : "boolean" },
712-
"assignment" : { "type" : "keyword" }
712+
"assignment" : { "type" : "keyword" }
713713
}
714714
}
715715
}

models/util/Util.cfc

+4-2
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,15 @@ component accessors="true" singleton {
9595
&& !isSimpleValue( errorPayload.error )
9696
&& errorPayload.error.keyExists( "root_cause" )
9797
)
98-
? " Reason: #isArray( errorPayload.error.root_cause ) ? errorPayload.error.root_cause[ 1 ].reason : errorPayload.error.root_cause.reason#"
98+
? " Reason: #isArray( errorPayload.error.root_cause ) ? ( errorPayload.error.root_cause[ 1 ].reason ?: "None" ) : (
99+
errorPayload.error.root_cause.reason ?: "None"
100+
)#"
99101
: (
100102
structKeyExists( errorPayload, "error" )
101103
? (
102104
isSimpleValue( errorPayload.error )
103105
? " Reason: #errorPayload.error# "
104-
: " Reason: #errorPayload.error.reason#"
106+
: " Reason: #errorPayload.error.reason ?: "None"#"
105107
)
106108
: ""
107109
);

0 commit comments

Comments
 (0)