Reputation: 4977
I have this existing function:
const inferProcessingError = R.ifElse(
R.propEq('conversionJobStatus', 3),
R.always('Last Process failed with error; please contact DevOps'),
R.always(null)
);
which is called like this:
const msg = inferProcessingError(jobStruct || {});
with this jobStruct:
{"id":9,"mediaGroupId":1000000,"conversionJobStatus":3,
"errorDetails": {
"Cause": {
"errorMessage": "MediaConvert Job Failed with ERROR status: ERROR Video codec [indeo4] is not a supported input video codec",
},
"Error": "Error",
}
}
and I need to create an error message string which includes the data from the Cause.errorMessage
element.
This would be dead simple with a native JavaScript function, but I'm learning Ramda and want to just modify the existing code to include in the error message.
An R.prop('Cause')['errorMessage']
could work except that I can't figure out how to reference the jobStruct that was passed in to the inferProcessingError
statement.
I can see that the R.ifElse
and subsequent Ramda functions are able to get that reference, but when I embed an R.prop('Cause')
in the error message string, it resolves to a function and not the value of the Cause
element because it seems to be waiting for the data structure.
So...how do I gain access to the jobStruct reference? (arguments
is not defined here).
UPDATE:
I can get this to work by referencing the original jobStruct as in R.Prop('ErrorDetails', jobStruct)['Cause']['errorMessage']
but that seems rather kludgy to me...
BUT if the call to inferProcessingError
is actually inside a map
statement and references an element in a larger structure, then the map index is not available to reference the data structure for the R.prop
.
Upvotes: 1
Views: 279
Reputation: 30400
Perhaps you could use the pipe
and path
methods to achieve this "the ramda way".
Begin by using ramda's path()
function to extract the nested errorMessage
value from the input jobStruct
object. Next, enclose that in a pipe()
that transforms the extracted message into a string formatted with a custom error prefix:
const incCount = R.ifElse(
R.propEq('conversionJobStatus', 3),
/* Evaluate this pipe if the error case is satisfied */
R.pipe(
/* Path to extract message from input object */
R.path(["errorDetails", "Cause", "errorMessage"]),
/* Prefix string to extracted error message */
R.concat('Custom error prefix:')),
R.always('')
);
incCount({"id":9,"mediaGroupId":1000000,"conversionJobStatus":3,
"errorDetails": {
"Cause": {
"errorMessage": "MediaConvert Job Failed with ERROR etc etc",
},
"Error": "Error",
}
});
Here's a working example - hope that helps!
Thanks to @customcommander for the suggestion to use concat
for the string prefix, as well as returning an empty string value for the second branch
Upvotes: 2