Skip to content

Commit

Permalink
Merge pull request #252 from contentstack/staging
Browse files Browse the repository at this point in the history
DX | 05-12-2024 | Release
  • Loading branch information
cs-raj authored Dec 5, 2024
2 parents c349ba4 + 964afe6 commit 46b0b86
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "contentstack",
"version": "3.22.2",
"version": "3.23.0",
"description": "Contentstack Javascript SDK",
"homepage": "https://www.contentstack.com/",
"author": {
Expand Down
16 changes: 12 additions & 4 deletions src/core/modules/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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
});
}

}
56 changes: 56 additions & 0 deletions test/entry/findone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
4 changes: 2 additions & 2 deletions test/typescript/asset-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions test/typescript/entry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions test/typescript/live-preview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 46b0b86

Please sign in to comment.