Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions spec/src/modules/tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8143,6 +8143,33 @@ describe('ConstructorIO - Tracker', () => {
});
});

it('Should receive an error message when rate limited (429)', (done) => {
// Create a mock response for 429 error
fetchSpy = sinon.spy(() => Promise.resolve({
ok: false,
status: 429,
statusText: 'Too Many Requests',
headers: new Map([['content-type', 'text/plain']]),
text: () => Promise.resolve('Too many requests'),
}));
Comment on lines +8148 to +8154
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the mock here


const { tracker } = new ConstructorIO({
apiKey: testApiKey,
fetch: fetchSpy,
});

tracker.trackInputFocus(userParameters);

tracker.on('error', (response) => {
expect(response).to.have.property('url');
expect(response).to.have.property('method');
expect(response).to.have.property('message');
expect(response.message).to.not.be.undefined;
expect(response.message).to.equal('Too many requests');
done();
});
});

it('Should receive an error message when making a request to an invalid endpoint', (done) => {
const { tracker } = new ConstructorIO({
apiKey: testApiKey,
Expand Down
32 changes: 16 additions & 16 deletions src/modules/tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ function send(url, userParameters, networkParameters, method = 'GET', body = {})

if (request) {
const instance = this;
const emitError = helpers.getEmitError(instance, { url, method });

request.then((response) => {
// Request was successful, and returned a 2XX status code
Expand All @@ -152,26 +153,25 @@ function send(url, userParameters, networkParameters, method = 'GET', body = {})

// Request was successful, but returned a non-2XX status code
else {
response.json().then((json) => {
instance.eventemitter.emit('error', {
url,
method,
message: json && json.message,
const contentType = response.headers.get('Content-Type') || '';

if (contentType.includes('application/json')) {
response.json().then((json) => {
emitError(json && json.message);
}).catch((error) => {
emitError(error.type);
});
}).catch((error) => {
instance.eventemitter.emit('error', {
url,
method,
message: error.type,
} else {
// If not JSON, fallback to text
response.text().then((text) => {
emitError(text || 'Unknown error message');
}).catch((error) => {
emitError(`Error reading text: ${error.message}`);
});
});
}
}
}).catch((error) => {
instance.eventemitter.emit('error', {
url,
method,
message: error.toString(),
});
emitError(error.toString());
});
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ const utils = {

return url;
},

getEmitError(instance, { url, method }) {
return function emitError(message) {
instance.eventemitter.emit('error', { url, method, message });
};
},
};

module.exports = utils;
Loading