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;