From dad9acae4b518159049f5dd1b078eb455ce727e5 Mon Sep 17 00:00:00 2001 From: Mohit Tejani Date: Mon, 30 Mar 2026 12:24:36 +0530 Subject: [PATCH 1/3] handling error parsing scenario for dataSync endpoints error details structure. --- src/errors/pubnub-api-error.ts | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/errors/pubnub-api-error.ts b/src/errors/pubnub-api-error.ts index 8d18beddd..a1dff2ad2 100644 --- a/src/errors/pubnub-api-error.ts +++ b/src/errors/pubnub-api-error.ts @@ -143,6 +143,29 @@ export class PubNubAPIError extends Error { ) { errorData = errorResponse; status = errorResponse.status; + } else if ( + 'errors' in errorResponse && + Array.isArray(errorResponse.errors) && + errorResponse.errors.length > 0 + ) { + // Handle DataSync-style structured error responses: + // { errors: [{ errorCode: "SYN-0008", message: "...", path: "/id" }] } + errorData = errorResponse; + + const errors = errorResponse.errors as Array<{ + errorCode?: string; + message?: string; + path?: string; + }>; + + message = errors + .map((e) => { + const parts: string[] = []; + if (e.errorCode) parts.push(e.errorCode); + if (e.message) parts.push(e.message); + return parts.join(': '); + }) + .join('; '); } else errorData = errorResponse; if ('error' in errorResponse && errorResponse.error instanceof Error) errorData = errorResponse.error; @@ -229,6 +252,35 @@ export class PubNubAPIError extends Error { }; } + /** + * Format a user-facing error message for this API error. + * + * When the error contains structured details extracted from the service response + * (e.g., DataSync `errors` array), those details are included in the message. + * Otherwise, falls back to a generic description. + * + * @param operation - Request operation during which error happened. + * + * @returns Formatted error message string. + */ + public toFormattedMessage(operation: RequestOperation): string { + const fallback = 'REST API request processing error, check status for details'; + + // When errorData contains a structured `errors` array, `this.message` was already + // constructed from it in `createFromServiceResponse` — prefer it over the generic fallback. + if ( + this.errorData && + typeof this.errorData === 'object' && + !('name' in this.errorData && 'message' in this.errorData && 'stack' in this.errorData) && + 'errors' in this.errorData && + Array.isArray((this.errorData as Record).errors) + ) { + return `${operation}: ${this.message}`; + } + + return fallback; + } + /** * Convert API error object to PubNub client error object. * From 1679c35554c5f8fe092c2eac954bdce923ce420a Mon Sep 17 00:00:00 2001 From: Mohit Tejani Date: Mon, 30 Mar 2026 12:25:38 +0530 Subject: [PATCH 2/3] enahnce transport module for making http PUT requests, required for dataSync endpoints --- src/core/components/request.ts | 2 +- src/core/types/transport-request.ts | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/core/components/request.ts b/src/core/components/request.ts index 8d7ef34cd..0ea818f77 100644 --- a/src/core/components/request.ts +++ b/src/core/components/request.ts @@ -123,7 +123,7 @@ export abstract class AbstractRequest Date: Mon, 30 Mar 2026 12:30:54 +0530 Subject: [PATCH 3/3] fix lint error in request.ts --- src/core/components/request.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/core/components/request.ts b/src/core/components/request.ts index 0ea818f77..17c8d577c 100644 --- a/src/core/components/request.ts +++ b/src/core/components/request.ts @@ -123,7 +123,11 @@ export abstract class AbstractRequest