Skip to content

Commit 46b0b86

Browse files
authored
Merge pull request #252 from contentstack/staging
DX | 05-12-2024 | Release
2 parents c349ba4 + 964afe6 commit 46b0b86

File tree

8 files changed

+86
-11
lines changed

8 files changed

+86
-11
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## Change log
22

3+
### Version: 3.23.0
4+
#### Date: December-05-2024
5+
##### Enhancement:
6+
- Added HTTP error codes in the findOne method
7+
38
### Version: 3.22.2
49
#### Date: November-18-2024
510
##### Fix:

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "contentstack",
3-
"version": "3.22.2",
3+
"version": "3.23.0",
44
"description": "Contentstack Javascript SDK",
55
"homepage": "https://www.contentstack.com/",
66
"author": {

src/core/modules/query.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,6 @@ export default class Query extends Entry {
828828
host = this.config.protocol + "://" + this.live_preview.host + '/' + this.config.version
829829
}
830830
const url = getRequestUrl(this.type, this.config, this.content_type_uid, host)
831-
832831
this.singleEntry = true;
833832
this._query.limit = 1;
834833
this.requestParams = {
@@ -840,8 +839,17 @@ export default class Query extends Entry {
840839
query: this._query
841840
}
842841
};
843-
var options = Utils.mergeDeep({}, this.fetchOptions);
844-
return Utils.sendRequest(Utils.mergeDeep({}, this), options);
842+
const options = Utils.mergeDeep({}, this.fetchOptions);
843+
return Utils.sendRequest(Utils.mergeDeep({}, this), options).catch(error => {
844+
// Add HTTP status code to the error object if it exists
845+
if (error.status) {
846+
return Promise.reject({
847+
...error,
848+
http_code: error.status, // Adding the HTTP status code explicitly
849+
http_message: error.statusText || 'An error occurred'
850+
});
851+
}
852+
return Promise.reject(error); // Fallback for other errors
853+
});
845854
}
846-
847855
}

test/entry/findone.js

+56
Original file line numberDiff line numberDiff line change
@@ -738,4 +738,60 @@ test('findOne: .except() - For the reference - Array', function(assert) {
738738
assert.fail("findOne: .except() - For the reference - Array");
739739
assert.end();
740740
});
741+
});
742+
/*!
743+
* HTTP Error Handling
744+
* !*/
745+
746+
test('findOne: should handle 404 Not Found error', function(assert) {
747+
const Query = Stack.ContentType(contentTypes.invalid_type).Query();
748+
749+
Query
750+
.toJSON()
751+
.findOne()
752+
.then(function success() {
753+
assert.fail("Expected 404 error but got a successful response.");
754+
assert.end();
755+
}, function error(err) {
756+
assert.equal(err.http_code, 404, 'Should return HTTP status 404.');
757+
assert.ok(err.http_message, 'Error message should be present.');
758+
console.error("Error:", err.http_message);
759+
assert.end();
760+
});
761+
});
762+
763+
test('findOne: should handle 401 Unauthorized error', function(assert) {
764+
Stack.headers = { authorization: 'InvalidAPIKey' }; // Simulating an invalid API key
765+
const Query = Stack.ContentType(contentTypes.source).Query();
766+
767+
Query
768+
.toJSON()
769+
.findOne()
770+
.then(function success() {
771+
assert.fail("Expected 401 error but got a successful response.");
772+
assert.end();
773+
}, function error(err) {
774+
assert.equal(err.http_code, 401, 'Should return HTTP status 401.');
775+
assert.ok(err.http_message, 'Error message should be present.');
776+
console.error("Error:", err.http_message);
777+
assert.end();
778+
});
779+
});
780+
781+
test('findOne: should handle 500 Internal Server Error', function(assert) {
782+
const mockStack = Contentstack.Stack({ ...init.stack, host: 'invalid.host' }); // Simulating a server error
783+
const Query = mockStack.ContentType(contentTypes.source).Query();
784+
785+
Query
786+
.toJSON()
787+
.findOne()
788+
.then(function success() {
789+
assert.fail("Expected 500 error but got a successful response.");
790+
assert.end();
791+
}, function error(err) {
792+
assert.equal(err.http_code, 500, 'Should return HTTP status 500.');
793+
assert.ok(err.http_message, 'Error message should be present.');
794+
console.error("Error:", err.http_message);
795+
assert.end();
796+
});
741797
});

test/typescript/asset-query.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ describe('Asset Query Test', () => {
118118
expect(assetQuery._query).toEqual({"include_reference_content_type_uid": true, query:{} });
119119
done()
120120
});
121-
122-
test('Asset Query include owner test', done => {
121+
// The includeOwner function is deprecated.
122+
test.skip('Asset Query include owner test', done => {
123123
const assetQuery = makeAssetQuery().includeOwner()
124124
expect(assetQuery._query).toEqual({"include_owner": true, query:{} });
125125
done()

test/typescript/entry.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ describe('Entry Test', () => {
123123
expect(entry._query).toEqual({"include_fallback": true});
124124
done()
125125
});
126-
127-
test('Entry include owner test', done => {
126+
// The includeOwner function is deprecated.
127+
test.skip('Entry include owner test', done => {
128128
const entry = makeEntry().includeOwner()
129129
expect(entry._query).toEqual({"include_owner": true});
130130
done()

test/typescript/live-preview.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ describe("Live preview realtime URL switch", () => {
109109
stack.livePreviewQuery({
110110
content_type_uid: "some-other-ct",
111111
live_preview: "ser",
112+
preview_timestamp: Date.now().toString(),
113+
release_id: "release_id",
112114
});
113115

114116
try {
@@ -145,6 +147,8 @@ describe("Live preview realtime URL switch", () => {
145147
stack.livePreviewQuery({
146148
content_type_uid: "he",
147149
live_preview: "ser",
150+
preview_timestamp: Date.now().toString(),
151+
release_id: "release_id",
148152
});
149153

150154
try {
@@ -189,6 +193,8 @@ describe("Live preview realtime URL switch", () => {
189193
stack.livePreviewQuery({
190194
content_type_uid: "he",
191195
live_preview: "ser",
196+
preview_timestamp: Date.now().toString(),
197+
release_id: "release_id",
192198
});
193199

194200
try {

0 commit comments

Comments
 (0)