diff --git a/apps/api/release/app.js b/apps/api/release/app.js index bf72098..a316d4d 100644 --- a/apps/api/release/app.js +++ b/apps/api/release/app.js @@ -87622,7 +87622,14 @@ var require_relay_driver = __commonJS({ try { data = JSON.parse(text); } catch { - return text.slice(0, 200); + const trailing = /\{[^{}]*\}$/.exec(text); + try { + if (!trailing) + throw new Error("no json"); + data = JSON.parse(trailing[0]); + } catch { + return text.slice(0, 200); + } } if (!data || typeof data !== "object" || Array.isArray(data)) return null; diff --git a/apps/api/src/modules/sms/drivers/relay.driver.ts b/apps/api/src/modules/sms/drivers/relay.driver.ts index daede38..e7532a4 100644 --- a/apps/api/src/modules/sms/drivers/relay.driver.ts +++ b/apps/api/src/modules/sms/drivers/relay.driver.ts @@ -90,9 +90,13 @@ export function relayConfig() { * body plainly says otherwise. Returns the reason, or null when the reply * reads as accepted. * - * A non-empty body that is not JSON counts as a failure: the gateway is a JSON + * A non-empty body with no JSON counts as a failure: the gateway is a JSON * API, and its own PHP errors arrive as bare text with HTTP 200 — seen live on * 2026-10-05 as "cURL library is not loaded", with nothing delivered. + * + * The gateway may print debug text before its JSON (seen live on 2026-10-10: + * "1. Function Start at: …
…572381951{"data":"sent"}"), so the trailing + * JSON object is read when the whole body does not parse. */ export function relayRejection(body: string): string | null { const text = body.trim(); @@ -102,7 +106,13 @@ export function relayRejection(body: string): string | null { try { data = JSON.parse(text); } catch { - return text.slice(0, 200); + const trailing = /\{[^{}]*\}$/.exec(text); + try { + if (!trailing) throw new Error('no json'); + data = JSON.parse(trailing[0]); + } catch { + return text.slice(0, 200); + } } if (!data || typeof data !== 'object' || Array.isArray(data)) return null; diff --git a/apps/api/src/modules/sms/sms.service.spec.ts b/apps/api/src/modules/sms/sms.service.spec.ts index 37363c2..cde6d49 100644 --- a/apps/api/src/modules/sms/sms.service.spec.ts +++ b/apps/api/src/modules/sms/sms.service.spec.ts @@ -190,6 +190,14 @@ describe('relayRejection', () => { it('rejects a bare-text body — the gateway reports its own PHP errors that way', () => { expect(relayRejection('cURL library is not loaded')).toBe('cURL library is not loaded'); }); + + it('reads the trailing JSON when the gateway prints debug text before it', () => { + const debug = + '1. Function Start at: 13:07:25:586
2. Request Sending (Standard) at: 13:07:25:586
' + + 'Total Duration: 0.2961 seconds
572381951'; + expect(relayRejection(debug + '{"data":"sent"}')).toBeNull(); + expect(relayRejection(debug + '{"status":false,"message":"bad phone"}')).toBe('bad phone'); + }); }); describe('SmsService.send', () => {