diff --git a/backend/app/lib/waybillGet.js b/backend/app/lib/waybillGet.js index eecd95e..ec0a8c2 100644 --- a/backend/app/lib/waybillGet.js +++ b/backend/app/lib/waybillGet.js @@ -230,6 +230,39 @@ function fail(code, message) { return { delivery_order_code: code || '', success: false, message }; } +/** + * 把平台返回的原始错误拼成一句可读提示(回给 WMS,同时写进取号记录) + * + * 平台(店管家)失败时通常只回一个 `ErrorCode`(如 2),真实原因在 `Data.ErrorMsg` / `Data.BranchName` 里, + * WMS 侧只能看到我们给出的文案,所以这里把可读字段一并带上,便于直接定位 + * (不包含任何收件人信息)。 + * @param {Error|Object} err 平台异常(可带 rawResponse) + * @return {string} 失败原因 + */ +function describePlatformFailure(err) { + const message = String((err && err.message) || '取号失败'); + const raw = err && err.rawResponse; + if (!raw || typeof raw !== 'object') return message; + + const parts = []; + const push = (label, value) => { + const text = String(value == null ? '' : value).trim(); + if (!text || text === 'null' || text === 'undefined') return; + if (message.includes(text) || parts.some(item => item.endsWith(`:${text}`))) return; + parts.push(`${label}:${text}`); + }; + + push('平台错误码', raw.ErrorCode); + push('平台提示', raw.Message); + const data = Array.isArray(raw.Data) ? raw.Data[0] : raw.Data; + if (data && typeof data === 'object') { + push('平台说明', data.ErrorMsg || data.Message); + push('网点', data.BranchName); + } + if (!parts.length) return message; + return `${message}(${parts.join(',')})`.slice(0, 500); +} + /** * 组装「未绑定到快递接口」的简短提示 * @@ -571,7 +604,8 @@ async function callPlatform(ctx, { principalId, warehouseId, code, expressCode, ctx.logger.warn('[waybill.get] 取号失败 %s:%s', code, err.message); // 平台异常时保留原始返回,便于在取号记录里看到平台到底回了什么 if (err && err.rawResponse) detail.raw = err.rawResponse; - return fail(code, err.message || '取号失败'); + // 回给 WMS 的错误信息带上平台的可读原因(ErrorCode / ErrorMsg / 网点),方便对方直接定位 + return fail(code, describePlatformFailure(err)); } }