diff --git a/lib/core/AxiosError.js b/lib/core/AxiosError.js index 60818f2b..14737173 100644 --- a/lib/core/AxiosError.js +++ b/lib/core/AxiosError.js @@ -11,29 +11,40 @@ class AxiosError extends Error { return axiosError; } - /** - * Create an Error with the specified message, config, error code, request and response. - * - * @param {string} message The error message. - * @param {string} [code] The error code (for example, 'ECONNABORTED'). - * @param {Object} [config] The config. - * @param {Object} [request] The request. - * @param {Object} [response] The response. - * - * @returns {Error} The created error. - */ - constructor(message, code, config, request, response) { - super(message); - this.name = 'AxiosError'; - this.isAxiosError = true; - code && (this.code = code); - config && (this.config = config); - request && (this.request = request); - if (response) { - this.response = response; - this.status = response.status; + /** + * Create an Error with the specified message, config, error code, request and response. + * + * @param {string} message The error message. + * @param {string} [code] The error code (for example, 'ECONNABORTED'). + * @param {Object} [config] The config. + * @param {Object} [request] The request. + * @param {Object} [response] The response. + * + * @returns {Error} The created error. + */ + constructor(message, code, config, request, response) { + super(message); + + // Make message enumerable to maintain backward compatibility + // The native Error constructor sets message as non-enumerable, + // but axios < v1.13.3 had it as enumerable + Object.defineProperty(this, 'message', { + value: message, + enumerable: true, + writable: true, + configurable: true + }); + + this.name = 'AxiosError'; + this.isAxiosError = true; + code && (this.code = code); + config && (this.config = config); + request && (this.request = request); + if (response) { + this.response = response; + this.status = response.status; + } } - } toJSON() { return { diff --git a/test/specs/core/AxiosError.spec.js b/test/specs/core/AxiosError.spec.js index 92576b69..17495ad4 100644 --- a/test/specs/core/AxiosError.spec.js +++ b/test/specs/core/AxiosError.spec.js @@ -67,4 +67,26 @@ describe('core::AxiosError', function () { const err = new AxiosError('test', 'foo', {}, {}, { status: 400 }); expect(err.status).toBe(400); }); + + it('should have message property as enumerable for backward compatibility', () => { + const err = new AxiosError('Test error message', 'ERR_TEST', {foo: 'bar'}); + + // Test Object.keys() includes message + const keys = Object.keys(err); + expect(keys).toContain('message'); + + // Test Object.entries() includes message + const entries = Object.entries(err); + const messageEntry = entries.find(([key]) => key === 'message'); + expect(messageEntry).toBeDefined(); + expect(messageEntry[1]).toBe('Test error message'); + + // Test spread operator includes message + const spread = {...err}; + expect(spread.message).toBe('Test error message'); + + // Verify message descriptor is enumerable + const descriptor = Object.getOwnPropertyDescriptor(err, 'message'); + expect(descriptor.enumerable).toBe(true); + }); });