mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2026-09-09 18:03:21 +00:00
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".
This commit is contained in:
@@ -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: '',
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user