diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e88349c..59d6b00b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ ## Change log +### Version: 3.23.0 +#### Date: December-05-2024 +##### Enhancement: + - Added HTTP error codes in the findOne method + ### Version: 3.22.2 #### Date: November-18-2024 ##### Fix: diff --git a/package-lock.json b/package-lock.json index d121dbe1..96fa0353 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "contentstack", - "version": "3.22.2", + "version": "3.23.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "contentstack", - "version": "3.22.2", + "version": "3.23.0", "license": "MIT", "dependencies": { "@contentstack/utils": "^1.3.12", diff --git a/package.json b/package.json index b1e0c2b2..5abbc7dc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "contentstack", - "version": "3.22.2", + "version": "3.23.0", "description": "Contentstack Javascript SDK", "homepage": "https://www.contentstack.com/", "author": { diff --git a/src/core/modules/query.js b/src/core/modules/query.js index 7194d51a..f81738dd 100755 --- a/src/core/modules/query.js +++ b/src/core/modules/query.js @@ -828,7 +828,6 @@ export default class Query extends Entry { host = this.config.protocol + "://" + this.live_preview.host + '/' + this.config.version } const url = getRequestUrl(this.type, this.config, this.content_type_uid, host) - this.singleEntry = true; this._query.limit = 1; this.requestParams = { @@ -840,8 +839,17 @@ export default class Query extends Entry { query: this._query } }; - var options = Utils.mergeDeep({}, this.fetchOptions); - return Utils.sendRequest(Utils.mergeDeep({}, this), options); + const options = Utils.mergeDeep({}, this.fetchOptions); + return Utils.sendRequest(Utils.mergeDeep({}, this), options).catch(error => { + // Add HTTP status code to the error object if it exists + if (error.status) { + return Promise.reject({ + ...error, + http_code: error.status, // Adding the HTTP status code explicitly + http_message: error.statusText || 'An error occurred' + }); + } + return Promise.reject(error); // Fallback for other errors + }); } - } \ No newline at end of file diff --git a/test/entry/findone.js b/test/entry/findone.js index a1a3a052..e082486e 100755 --- a/test/entry/findone.js +++ b/test/entry/findone.js @@ -738,4 +738,60 @@ test('findOne: .except() - For the reference - Array', function(assert) { assert.fail("findOne: .except() - For the reference - Array"); assert.end(); }); +}); +/*! + * HTTP Error Handling + * !*/ + +test('findOne: should handle 404 Not Found error', function(assert) { + const Query = Stack.ContentType(contentTypes.invalid_type).Query(); + + Query + .toJSON() + .findOne() + .then(function success() { + assert.fail("Expected 404 error but got a successful response."); + assert.end(); + }, function error(err) { + assert.equal(err.http_code, 404, 'Should return HTTP status 404.'); + assert.ok(err.http_message, 'Error message should be present.'); + console.error("Error:", err.http_message); + assert.end(); + }); +}); + +test('findOne: should handle 401 Unauthorized error', function(assert) { + Stack.headers = { authorization: 'InvalidAPIKey' }; // Simulating an invalid API key + const Query = Stack.ContentType(contentTypes.source).Query(); + + Query + .toJSON() + .findOne() + .then(function success() { + assert.fail("Expected 401 error but got a successful response."); + assert.end(); + }, function error(err) { + assert.equal(err.http_code, 401, 'Should return HTTP status 401.'); + assert.ok(err.http_message, 'Error message should be present.'); + console.error("Error:", err.http_message); + assert.end(); + }); +}); + +test('findOne: should handle 500 Internal Server Error', function(assert) { + const mockStack = Contentstack.Stack({ ...init.stack, host: 'invalid.host' }); // Simulating a server error + const Query = mockStack.ContentType(contentTypes.source).Query(); + + Query + .toJSON() + .findOne() + .then(function success() { + assert.fail("Expected 500 error but got a successful response."); + assert.end(); + }, function error(err) { + assert.equal(err.http_code, 500, 'Should return HTTP status 500.'); + assert.ok(err.http_message, 'Error message should be present.'); + console.error("Error:", err.http_message); + assert.end(); + }); }); \ No newline at end of file diff --git a/test/typescript/asset-query.test.ts b/test/typescript/asset-query.test.ts index 723b3dd2..d2b79986 100644 --- a/test/typescript/asset-query.test.ts +++ b/test/typescript/asset-query.test.ts @@ -118,8 +118,8 @@ describe('Asset Query Test', () => { expect(assetQuery._query).toEqual({"include_reference_content_type_uid": true, query:{} }); done() }); - - test('Asset Query include owner test', done => { + // The includeOwner function is deprecated. + test.skip('Asset Query include owner test', done => { const assetQuery = makeAssetQuery().includeOwner() expect(assetQuery._query).toEqual({"include_owner": true, query:{} }); done() diff --git a/test/typescript/entry.test.ts b/test/typescript/entry.test.ts index 910e0815..033a7439 100644 --- a/test/typescript/entry.test.ts +++ b/test/typescript/entry.test.ts @@ -123,8 +123,8 @@ describe('Entry Test', () => { expect(entry._query).toEqual({"include_fallback": true}); done() }); - - test('Entry include owner test', done => { + // The includeOwner function is deprecated. + test.skip('Entry include owner test', done => { const entry = makeEntry().includeOwner() expect(entry._query).toEqual({"include_owner": true}); done() diff --git a/test/typescript/live-preview.test.ts b/test/typescript/live-preview.test.ts index 8ece36c6..fdffb35f 100644 --- a/test/typescript/live-preview.test.ts +++ b/test/typescript/live-preview.test.ts @@ -109,6 +109,8 @@ describe("Live preview realtime URL switch", () => { stack.livePreviewQuery({ content_type_uid: "some-other-ct", live_preview: "ser", + preview_timestamp: Date.now().toString(), + release_id: "release_id", }); try { @@ -145,6 +147,8 @@ describe("Live preview realtime URL switch", () => { stack.livePreviewQuery({ content_type_uid: "he", live_preview: "ser", + preview_timestamp: Date.now().toString(), + release_id: "release_id", }); try { @@ -189,6 +193,8 @@ describe("Live preview realtime URL switch", () => { stack.livePreviewQuery({ content_type_uid: "he", live_preview: "ser", + preview_timestamp: Date.now().toString(), + release_id: "release_id", }); try {