Skip to content

Commit f424edd

Browse files
authored
Handle text API responses (#223)
* Handle text API responses * update content header in test mockup * Refactor emitError
1 parent 895720b commit f424edd

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

spec/src/modules/tracker.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8143,6 +8143,33 @@ describe('ConstructorIO - Tracker', () => {
81438143
});
81448144
});
81458145

8146+
it('Should receive an error message when rate limited (429)', (done) => {
8147+
// Create a mock response for 429 error
8148+
fetchSpy = sinon.spy(() => Promise.resolve({
8149+
ok: false,
8150+
status: 429,
8151+
statusText: 'Too Many Requests',
8152+
headers: new Map([['content-type', 'text/plain']]),
8153+
text: () => Promise.resolve('Too many requests'),
8154+
}));
8155+
8156+
const { tracker } = new ConstructorIO({
8157+
apiKey: testApiKey,
8158+
fetch: fetchSpy,
8159+
});
8160+
8161+
tracker.trackInputFocus(userParameters);
8162+
8163+
tracker.on('error', (response) => {
8164+
expect(response).to.have.property('url');
8165+
expect(response).to.have.property('method');
8166+
expect(response).to.have.property('message');
8167+
expect(response.message).to.not.be.undefined;
8168+
expect(response.message).to.equal('Too many requests');
8169+
done();
8170+
});
8171+
});
8172+
81468173
it('Should receive an error message when making a request to an invalid endpoint', (done) => {
81478174
const { tracker } = new ConstructorIO({
81488175
apiKey: testApiKey,

src/modules/tracker.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ function send(url, userParameters, networkParameters, method = 'GET', body = {})
139139

140140
if (request) {
141141
const instance = this;
142+
const emitError = helpers.getEmitError(instance, { url, method });
142143

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

153154
// Request was successful, but returned a non-2XX status code
154155
else {
155-
response.json().then((json) => {
156-
instance.eventemitter.emit('error', {
157-
url,
158-
method,
159-
message: json && json.message,
156+
const contentType = response.headers.get('Content-Type') || '';
157+
158+
if (contentType.includes('application/json')) {
159+
response.json().then((json) => {
160+
emitError(json && json.message);
161+
}).catch((error) => {
162+
emitError(error.type);
160163
});
161-
}).catch((error) => {
162-
instance.eventemitter.emit('error', {
163-
url,
164-
method,
165-
message: error.type,
164+
} else {
165+
// If not JSON, fallback to text
166+
response.text().then((text) => {
167+
emitError(text || 'Unknown error message');
168+
}).catch((error) => {
169+
emitError(`Error reading text: ${error.message}`);
166170
});
167-
});
171+
}
168172
}
169173
}).catch((error) => {
170-
instance.eventemitter.emit('error', {
171-
url,
172-
method,
173-
message: error.toString(),
174-
});
174+
emitError(error.toString());
175175
});
176176
}
177177
}

src/utils/helpers.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ const utils = {
134134

135135
return url;
136136
},
137+
138+
getEmitError(instance, { url, method }) {
139+
return function emitError(message) {
140+
instance.eventemitter.emit('error', { url, method, message });
141+
};
142+
},
137143
};
138144

139145
module.exports = utils;

0 commit comments

Comments
 (0)