如何迭代 Mule 4 中的错误消息
How to iterate over the fault message in Mule 4
就我而言,我尝试将以下 Mule 表达式语言 (MEL) 脚本转换为 DataWeave 2.0。
Mule 3 上的 MEL 脚本:
import java.util.Iterator;
import org.dom4j.*;
import java.util.logging.Logger;
import java.util.*;
faultstring = xpath3('/Envelope/Body/Fault/faultstring',payload).replaceFirst("rating failed:","");
faults = faultstring.split("rating failed:");
appNode = domPayload.selectSingleNode("/DTOApplication[1]");
for (fault : faults) {
appNode.addElement('ValidationError')
.addAttribute("TypeCd","Validation")
.addAttribute("Name","Rate Service Error")
.addAttribute("Msg","Rating Service Error (" + flowVars.ratingModule + "). Message: " + fault.trim())
.addAttribute("SubTypeCd","ERROR");
}
逻辑是将故障消息负载附加到 dompayload(它只不过是一个输入请求)。
我能够为返回的故障消息附加单个子节点:
<?xml version="1.0" encoding="UTF-8"?>
<DTOApplication id="Application-1660258480-1493174910">
<ValidationError TypeCd="Validation" Name="Rate Service Error" Msg="Rate Service Error (TPCL)" Message=", Policy.Class='424490C' not in list , Policy.Industry='WHOL' not in list " SubTypeCd="ERROR"/>
</DTOApplication>
预期输出:
<?xml version="1.0" encoding="UTF-8"?>
<DTOApplication id="Application-1660258480-1493174910">
<ValidationError TypeCd="Validation" Name="Rate Service Error" Msg="Rating Service Error (TPCL). Message: Policy.Class='424490C' not in list" SubTypeCd="ERROR"/>
<ValidationError TypeCd="Validation" Name="Rate Service Error" Msg="Rating Service Error (TPCL). Message: Policy.Industry='WHOL' not in list" SubTypeCd="ERROR"/>
</DTOApplication>
我试过 DataWeave 2.0 :
%dw 2.0
var errormessage = (vars.fault replace "rating failed:" with ", ")
var ratingModule= vars.faultPayload.Envelope.Body.Fault.faultstring
output application/xml writeDeclaration=false
---
payload mapObject ((value, key, index) ->
(key): value ++ {
ValidationError @(TypeCd: "Validation", Name:"Rate Service Error" ,Msg: "Rate Service Error (" ++ ratingModule ++ ")" , Message: errormessage, SubTypeCd: "ERROR"): null
}
)
故障载荷:
{Envelope={Body={rateResponse=null, Fault={faultcode=FcgiSvc.16.rateHASH.service.exception, faultstring=rating failed:Policy.Class='424490C' not in list rating failed:Policy.Industry='WHOL' not in list }}}}
我想在 DataWeave 2.0 中做一些等同于 for (fault : faults) 的事情。
我假设输入有效负载如下所示
<?xml version="1.0" encoding="UTF-8"?>
<DTOApplication id="Application-1660258480-1493174910">
</DTOApplication>
假设上面的负载,我使用了 update
运算符。有关更多详细信息,请查看此 cookbook
%dw 2.0
output application/xml
var faultstring = (vars.faultPayload.Envelope.Body.Fault.faultstring replace "rating failed:" with ",") default ""
var faults = faultstring[1 to -1] splitBy ","
---
payload update {
case .DTOApplication -> ($ default {}) ++ {
(faults map ((item) -> {
ValidationError @(
TypeCd: "Validation",
Name:"Rate Service Error",
Msg: "Rate Service Error (" ++ vars.ratingModule ++ ")" ++ " Message: " ++ trim(item) replace ":" with "=",
SubTypeCd: "ERROR"
): null
}))
}
}
输出
<?xml version='1.0' encoding='UTF-8'?>
<DTOApplication id="Application-1660258480-1493174910">
<ValidationError TypeCd="Validation" Name="Rate Service Error" Msg="Rate Service Error (TPCL) Message= Policy.Class='424490C' not in list" SubTypeCd="ERROR"/>
<ValidationError TypeCd="Validation" Name="Rate Service Error" Msg="Rate Service Error (TPCL) Message= Policy.Industry='WHOL' not in list" SubTypeCd="ERROR"/>
</DTOApplication>
DataWeave 是一种函数式语言,没有 for
或 foreach
概念。您需要使用 map
、mapObject
、reduce
等函数转换任何输入以获得相同的结果。
在这种情况下,您想要遍历“payload.Envelope.Body.Fault.faultstring”,但它是一个字符串。不能直接迭代。首先,您需要用一些分隔符子字符串拆分字符串,以便我们得到一个元素数组。我们可以使用“rating failed:”作为分隔符拆分 faultstring,然后将每个字符串映射到将用作元素的 key-pair,然后缩减以将所有 key-pair 连接成一个对象。
%dw 2.0
output application/xml
var fault=payload.Envelope.Body.Fault
var errormessage = (fault.faultstring splitBy "rating failed:") filter sizeOf($) > 0
---
{
DTOApplication: (errormessage map {ValidationError @(TypeCd: "Validation", Name:"Rate Service Error" ,Msg: "Rate Service Error (TCPL).", Message: $, SubTypeCd: "ERROR"): null })
reduce ($$ ++ $)
}
输出:
<DTOApplication>
<ValidationError TypeCd="Validation" Name="Rate Service Error" Msg="Rate Service Error (TCPL)." Message="Policy.Class='424490C' not in list " SubTypeCd="ERROR"/>
<ValidationError TypeCd="Validation" Name="Rate Service Error" Msg="Rate Service Error (TCPL)." Message="Policy.Industry='WHOL' not in list" SubTypeCd="ERROR"/>
</DTOApplication>
就我而言,我尝试将以下 Mule 表达式语言 (MEL) 脚本转换为 DataWeave 2.0。
Mule 3 上的 MEL 脚本:
import java.util.Iterator;
import org.dom4j.*;
import java.util.logging.Logger;
import java.util.*;
faultstring = xpath3('/Envelope/Body/Fault/faultstring',payload).replaceFirst("rating failed:","");
faults = faultstring.split("rating failed:");
appNode = domPayload.selectSingleNode("/DTOApplication[1]");
for (fault : faults) {
appNode.addElement('ValidationError')
.addAttribute("TypeCd","Validation")
.addAttribute("Name","Rate Service Error")
.addAttribute("Msg","Rating Service Error (" + flowVars.ratingModule + "). Message: " + fault.trim())
.addAttribute("SubTypeCd","ERROR");
}
逻辑是将故障消息负载附加到 dompayload(它只不过是一个输入请求)。
我能够为返回的故障消息附加单个子节点:
<?xml version="1.0" encoding="UTF-8"?>
<DTOApplication id="Application-1660258480-1493174910">
<ValidationError TypeCd="Validation" Name="Rate Service Error" Msg="Rate Service Error (TPCL)" Message=", Policy.Class='424490C' not in list , Policy.Industry='WHOL' not in list " SubTypeCd="ERROR"/>
</DTOApplication>
预期输出:
<?xml version="1.0" encoding="UTF-8"?>
<DTOApplication id="Application-1660258480-1493174910">
<ValidationError TypeCd="Validation" Name="Rate Service Error" Msg="Rating Service Error (TPCL). Message: Policy.Class='424490C' not in list" SubTypeCd="ERROR"/>
<ValidationError TypeCd="Validation" Name="Rate Service Error" Msg="Rating Service Error (TPCL). Message: Policy.Industry='WHOL' not in list" SubTypeCd="ERROR"/>
</DTOApplication>
我试过 DataWeave 2.0 :
%dw 2.0
var errormessage = (vars.fault replace "rating failed:" with ", ")
var ratingModule= vars.faultPayload.Envelope.Body.Fault.faultstring
output application/xml writeDeclaration=false
---
payload mapObject ((value, key, index) ->
(key): value ++ {
ValidationError @(TypeCd: "Validation", Name:"Rate Service Error" ,Msg: "Rate Service Error (" ++ ratingModule ++ ")" , Message: errormessage, SubTypeCd: "ERROR"): null
}
)
故障载荷:
{Envelope={Body={rateResponse=null, Fault={faultcode=FcgiSvc.16.rateHASH.service.exception, faultstring=rating failed:Policy.Class='424490C' not in list rating failed:Policy.Industry='WHOL' not in list }}}}
我想在 DataWeave 2.0 中做一些等同于 for (fault : faults) 的事情。
我假设输入有效负载如下所示
<?xml version="1.0" encoding="UTF-8"?>
<DTOApplication id="Application-1660258480-1493174910">
</DTOApplication>
假设上面的负载,我使用了 update
运算符。有关更多详细信息,请查看此 cookbook
%dw 2.0
output application/xml
var faultstring = (vars.faultPayload.Envelope.Body.Fault.faultstring replace "rating failed:" with ",") default ""
var faults = faultstring[1 to -1] splitBy ","
---
payload update {
case .DTOApplication -> ($ default {}) ++ {
(faults map ((item) -> {
ValidationError @(
TypeCd: "Validation",
Name:"Rate Service Error",
Msg: "Rate Service Error (" ++ vars.ratingModule ++ ")" ++ " Message: " ++ trim(item) replace ":" with "=",
SubTypeCd: "ERROR"
): null
}))
}
}
输出
<?xml version='1.0' encoding='UTF-8'?>
<DTOApplication id="Application-1660258480-1493174910">
<ValidationError TypeCd="Validation" Name="Rate Service Error" Msg="Rate Service Error (TPCL) Message= Policy.Class='424490C' not in list" SubTypeCd="ERROR"/>
<ValidationError TypeCd="Validation" Name="Rate Service Error" Msg="Rate Service Error (TPCL) Message= Policy.Industry='WHOL' not in list" SubTypeCd="ERROR"/>
</DTOApplication>
DataWeave 是一种函数式语言,没有 for
或 foreach
概念。您需要使用 map
、mapObject
、reduce
等函数转换任何输入以获得相同的结果。
在这种情况下,您想要遍历“payload.Envelope.Body.Fault.faultstring”,但它是一个字符串。不能直接迭代。首先,您需要用一些分隔符子字符串拆分字符串,以便我们得到一个元素数组。我们可以使用“rating failed:”作为分隔符拆分 faultstring,然后将每个字符串映射到将用作元素的 key-pair,然后缩减以将所有 key-pair 连接成一个对象。
%dw 2.0
output application/xml
var fault=payload.Envelope.Body.Fault
var errormessage = (fault.faultstring splitBy "rating failed:") filter sizeOf($) > 0
---
{
DTOApplication: (errormessage map {ValidationError @(TypeCd: "Validation", Name:"Rate Service Error" ,Msg: "Rate Service Error (TCPL).", Message: $, SubTypeCd: "ERROR"): null })
reduce ($$ ++ $)
}
输出:
<DTOApplication>
<ValidationError TypeCd="Validation" Name="Rate Service Error" Msg="Rate Service Error (TCPL)." Message="Policy.Class='424490C' not in list " SubTypeCd="ERROR"/>
<ValidationError TypeCd="Validation" Name="Rate Service Error" Msg="Rate Service Error (TCPL)." Message="Policy.Industry='WHOL' not in list" SubTypeCd="ERROR"/>
</DTOApplication>