@@ -143,6 +143,29 @@ export class PubNubAPIError extends Error {
143143 ) {
144144 errorData = errorResponse ;
145145 status = errorResponse . status ;
146+ } else if (
147+ 'errors' in errorResponse &&
148+ Array . isArray ( errorResponse . errors ) &&
149+ errorResponse . errors . length > 0
150+ ) {
151+ // Handle DataSync-style structured error responses:
152+ // { errors: [{ errorCode: "SYN-0008", message: "...", path: "/id" }] }
153+ errorData = errorResponse ;
154+
155+ const errors = errorResponse . errors as Array < {
156+ errorCode ?: string ;
157+ message ?: string ;
158+ path ?: string ;
159+ } > ;
160+
161+ message = errors
162+ . map ( ( e ) => {
163+ const parts : string [ ] = [ ] ;
164+ if ( e . errorCode ) parts . push ( e . errorCode ) ;
165+ if ( e . message ) parts . push ( e . message ) ;
166+ return parts . join ( ': ' ) ;
167+ } )
168+ . join ( '; ' ) ;
146169 } else errorData = errorResponse ;
147170
148171 if ( 'error' in errorResponse && errorResponse . error instanceof Error ) errorData = errorResponse . error ;
@@ -229,6 +252,35 @@ export class PubNubAPIError extends Error {
229252 } ;
230253 }
231254
255+ /**
256+ * Format a user-facing error message for this API error.
257+ *
258+ * When the error contains structured details extracted from the service response
259+ * (e.g., DataSync `errors` array), those details are included in the message.
260+ * Otherwise, falls back to a generic description.
261+ *
262+ * @param operation - Request operation during which error happened.
263+ *
264+ * @returns Formatted error message string.
265+ */
266+ public toFormattedMessage ( operation : RequestOperation ) : string {
267+ const fallback = 'REST API request processing error, check status for details' ;
268+
269+ // When errorData contains a structured `errors` array, `this.message` was already
270+ // constructed from it in `createFromServiceResponse` — prefer it over the generic fallback.
271+ if (
272+ this . errorData &&
273+ typeof this . errorData === 'object' &&
274+ ! ( 'name' in this . errorData && 'message' in this . errorData && 'stack' in this . errorData ) &&
275+ 'errors' in this . errorData &&
276+ Array . isArray ( ( this . errorData as Record < string , unknown > ) . errors )
277+ ) {
278+ return `${ operation } : ${ this . message } ` ;
279+ }
280+
281+ return fallback ;
282+ }
283+
232284 /**
233285 * Convert API error object to PubNub client error object.
234286 *
0 commit comments