From 3424c53da571ed8cbd95ee209b2f07617ebc6217 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 3 Jul 2026 16:47:38 +0000 Subject: [PATCH] fix(core): provide meaningful error message when API response body is null When the server returns no response body (e.g., during token expiration, network interruption, or CORS failures), err.error is null causing JSON.stringify(null) to produce the unhelpful message "null". Now we explicitly handle the null case and produce a message like "401 Unauthorized" instead of "null". --- .../api/src/lib/adf-http-client.service.spec.ts | 17 +++++++++++++++++ lib/core/api/src/lib/adf-http-client.service.ts | 9 ++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/core/api/src/lib/adf-http-client.service.spec.ts b/lib/core/api/src/lib/adf-http-client.service.spec.ts index 70e77d9172..d95ccbbfa1 100644 --- a/lib/core/api/src/lib/adf-http-client.service.spec.ts +++ b/lib/core/api/src/lib/adf-http-client.service.spec.ts @@ -213,6 +213,23 @@ describe('AdfHttpClient', () => { req.flush(errorResponse, { status: 403, statusText: 'Forbidden' }); }); + it('should return a meaningful error message when response body is null', (done) => { + const options: RequestOptions = { + path: '', + httpMethod: 'POST' + }; + + angularHttpClient.request('http://example.com', options, securityOptions, emitters).catch((err: AlfrescoApiResponseError) => { + expect(err instanceof Error).toBeTruthy(); + expect(err.message).toBe('401 Unauthorized'); + expect(err.status).toBe(401); + done(); + }); + + const req = controller.expectOne('http://example.com'); + req.flush(null, { status: 401, statusText: 'Unauthorized' }); + }); + it('should return a Error type on failed promise with response body', (done) => { const options: RequestOptions = { path: '', diff --git a/lib/core/api/src/lib/adf-http-client.service.ts b/lib/core/api/src/lib/adf-http-client.service.ts index 81fb4d8768..19e8c3d23f 100644 --- a/lib/core/api/src/lib/adf-http-client.service.ts +++ b/lib/core/api/src/lib/adf-http-client.service.ts @@ -223,7 +223,14 @@ export class AdfHttpClient implements JsApiHttpClient { // for backwards compatibility we need to convert it to error class as the HttpErrorResponse only implements Error interface, not extending it, // and we need to be able to correctly pass instanceof Error conditions used inside repository // we also need to pass error as Stringify string as we are detecting statusCodes using JSON.parse(error.message) in some places - const msg = typeof err.error === 'string' ? err.error : JSON.stringify(err.error); + let msg: string; + if (err.error == null) { + msg = `${err.status} ${err.statusText ?? 'Unknown error'}`; + } else if (typeof err.error === 'string') { + msg = err.error; + } else { + msg = JSON.stringify(err.error); + } // for backwards compatibility to handle cases in code where we try read response.error.response.body;