如何在MuleSoft中捕获POST请求的JSON错误响应并日志记录?
问题:MuleSoft中捕获并记录外部API的400验证错误详情
我正在开发一个MuleSoft应用,流程为:从某API端点GET数据,转换后POST至另一端点。当POST请求触发目标API的验证规则时,会返回400错误。在Postman中能看到具体的验证错误信息,修正数据后即可成功POST(返回含{ID:VALUE}的JSON响应)。
需求是构建一个流程,先验证并捕获/记录验证错误,再执行所有成功的POST请求。当前已在For Each作用域中配置错误处理器,能记录失败并继续处理后续请求,但无法在Logger中记录外部API返回的验证错误详情——尝试查看error对象的各部分都没拿到具体错误内容,只能得到通用的400 Bad Request错误,仅能从error实体中获取响应头。
Postman验证请求示例
POST /v1/employees?clientNamespace=myNameSpace&isValidateOnly=true
失败POST响应示例(Postman)
{ "requestId": "01c1c37b-ca31-4cba-b954-6ea0c4139d33", "processResults": [ { "code": "HR_Employee_EmployeeDataSaveFailure", "context": "Employee", "level": "ERROR", "message": "Failed to Save Employee Data" }, { "code": "HR_Employee_BirthCountryAndMaidenNameRequired", "level": "WARN", "message": "Birth Country and Birth Name - Values are required if the Social Insurance Number is missing for automatically registering the employee to the Social Insurance companies." }, { "code": "HR_Employee_BirthCountryMissing", "level": "WARN", "message": "Birth Country: Value is missing" }, { "code": "HR_Employee_BirthCityMissing", "level": "WARN", "message": "Birth City: Value is missing" }, { "code": "HR_Address_InvalidZipOrPostalCodeForCountry", "context": "Employee.Addresses[0].PostalCode", "level": "ERROR", "message": "The zip or postal code 252500-99 is not valid for the country: DEU" } ] }
成功POST响应示例(含警告,Postman)
{ "Data": { "XRefCode": "100022" }, "ProcessResults": [ { "Code": "HR_EmployeeWorkAssignmentManager_HasBeenEndDated", "Context": "ManagerName", "Level": "WARN", "Message": "Direct Manager - Value will be assigned an end date of 4/28/2025 due to the effective dates of the employee’s work assignments." } ] }
当前错误日志输出
[ com.mulesoft.mule.runtime.core.internal.processor.TransformMessageProcessor@225b0b38, post-employee-detailsFlow/processors/2/processors/0/processors/1, org.mule.runtime.core.internal.processor.LoggerMessageProcessor@3e2de4a7, org.mule.runtime.core.internal.processor.LoggerMessageProcessor@96bc62c ].switchOnErrorScheduler @5ccd130d] [processor: post-employee-detailsFlow/processors/2/processors/0/errorHandler/0/processors/0; event: 23acac40-0cb8-11f0-a7e1-5ce91eb7799f] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Error Body: org.mule.runtime.core.internal.message.ErrorBuilder$DeserializableErrorImplementation { description=HTTP POST on resource 'http://staginghostname.com:80/api/myClientNamespace/v1/employees' failed: bad request (400). detailedDescription=HTTP POST on resource 'http://staginghostname.com:80/api/myClientNamespace/v1/employees' failed: bad request (400). errorType=HTTP:BAD_REQUEST cause=org.mule.extension.http.api.request.validator.ResponseValidatorTypedException errorMessage= org.mule.runtime.core.internal.message.DefaultMessageBuilder$MessageImplementation { payload=org.mule.runtime.core.internal.streaming.bytes.ManagedCursorStreamProvider@27a33622 mediaType=application/json; charset=UTF-8 attributes=org.mule.extension.http.api.HttpResponseAttributes { Status Code=400 Reason Phrase=Bad Request Headers=[ date=Sat, 29 Mar 2025 16:17:39 GMT content-type=application/json content-length=620 connection=keep-alive access-control-allow-credentials=true strict-transport-security=max-age=31536000; includeSubDomains cf-cache-status=DYNAMIC server=cloudflare cf-ray=9280adbb5a7ea21f-MSP ] } attributesMediaType=*/* } suppressedErrors=[] childErrors=[] }
补充说明:无错误时POST执行正常,最终计划将应用部署至CloudHub,需配置基础日志,当前处于本地开发阶段。
内容的提问来源于stack exchange,提问作者elegantcomplexity
相关产品推荐
相关产品推荐

