Home > OS >  how to extract only certain sentences
how to extract only certain sentences

Time:08-18

Exception: Request failed for https://test123.my.salesforce.com returned code 400. Truncated server response: [{"message":"Use one of these records?","errorCode":"UNKNOWN_EXCEPTION","fields":[]}] (use muteHttpExceptions option to examine full response)


Exception: Request failed for https://test123.my.salesforce.com returned code 400. Truncated server response: [{"message”:”myNum: value outside of valid range on numeric field: 100.0","errorCode":"NUMBER_OUTSIDE_VALID_RANGE","fields”:[“myNum”__c]}] (use muteHttpExceptions option to examine full response)

I want to extract only the message part from the following sentence.

"Use one of these records?"

”myNum: value outside of valid range on numeric field: 100.0"

I tried a few ways, but it didn't work out.

I tried using split and slice, The format of the error message has changed and it has not come out properly.

var response_code = error.toString();
var slice = response_code.split(':')[4]
var errormessage = (slice||'').split(',')[0]

There are results that come out as I want,

"Use one of these records?"

Some of the results were cut off.

"myNum

What should I do?

CodePudding user response:

Assuming that the JSON key "message" would be relatively unique in your log statements, you could use match() as follows:

var messages = ["Exception: Request failed for https://test123.my.salesforce.com returned code 400. Truncated server response: [{\"message\":\"Use one of these records?\",\"errorCode\":\"UNKNOWN_EXCEPTION\",\"fields\":[]}] (use muteHttpExceptions option to examine full response)",
"Exception: Request failed for https://test123.my.salesforce.com returned code 400. Truncated server response: [{\"message\":\"myNum: value outside of valid range on numeric field: 100.0\",\"errorCode\":\"NUMBER_OUTSIDE_VALID_RANGE\",\"fields\":[\"myNum\"__c]}] (use muteHttpExceptions option to examine full response)"];
var outputs = messages.map(x => x.match(/"message":"(.*?)"/)[1]);
console.log(outputs);

  • Related