Skip to content

Commit 8197675

Browse files
anu3990SameeraPriyathamTadikonda
authored andcommitted
Fine-tuning the error thrown for expired and invalid cloud API keys
1 parent 0ca0721 commit 8197675

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/requester.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ function getAccessToken(operation){
7070

7171
const req = https.request(options, (res) => {
7272
res.on('data', (d) => {
73+
if(res.statusCode === 400){
74+
throw new Error(d.toString());
75+
}
7376
const responseValue = JSON.parse(d.toString());
7477
operation.accessToken = responseValue.access_token;
7578
operation.expiration = new Date(responseValue[".expires"]);

test-basic/cloud_authentication-test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const marklogic = require('../');
1818
let assert = require('assert');
1919
const testconfig = require("../etc/test-config");
2020
const mlutil = require("../lib/mlutil");
21+
const expect = require('chai').expect;
2122

2223
describe('cloud-authentication tests', function() {
2324
it('should throw error without apiKey.', function(done){
@@ -46,4 +47,21 @@ describe('cloud-authentication tests', function() {
4647
done(error);
4748
}
4849
});
50+
51+
it('should throw error with invalid apiKey.', function (done) {
52+
let db = marklogic.createDatabaseClient({
53+
host: 'support.beta.marklogic.cloud',
54+
authType: 'cloud',
55+
apiKey: 'invalid'
56+
});
57+
let writeObject = {uri: '/test.json', content: '{"key":"value"}'};
58+
59+
try {
60+
// Also verified that it throws 'Error: User's API Key is expired.' when API key has expired a few seconds ago.
61+
expect(()=>db.documents.write(writeObject).throws(Error('API Key is not valid.')));
62+
done();
63+
} catch (error) {
64+
done(error);
65+
}
66+
});
4967
});

0 commit comments

Comments
 (0)