Skip to content

Commit 8170c03

Browse files
Address PR Comments
1 parent c6d00d2 commit 8170c03

File tree

3 files changed

+537
-79
lines changed

3 files changed

+537
-79
lines changed

src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,7 @@ export const MILLIS_IN_ONE_SEC = 1000;
201201
export const HTTP_OK = 200 as const;
202202
export const HTTP_ACCEPTED = 202 as const;
203203
export const HTTP_BAD_REQUEST = 400 as const;
204+
export const HTTP_UNAUTHORIZED = 401 as const;
204205
export const HTTP_FORBIDDEN = 403 as const;
206+
export const HTTP_NOT_FOUND = 404 as const;
207+
export const HTTP_SERVER_ERROR = 500 as const;

src/identityApiClient.ts

Lines changed: 92 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Constants, { HTTP_ACCEPTED, HTTP_OK } from './constants';
1+
import Constants, { HTTP_ACCEPTED, HTTP_BAD_REQUEST, HTTP_OK } from './constants';
22
import {
33
AsyncUploader,
44
FetchUploader,
@@ -114,54 +114,65 @@ export default function IdentityAPIClient(
114114
try {
115115
const response: Response = await uploader.upload(uploadPayload);
116116

117-
let message: string;
118117
let aliasResponseBody: IAliasResponseBody;
119-
120-
// FetchUploader returns the response as a JSON object that we have to await
121-
if (response.json) {
122-
// HTTP responses of 202, 200, and 403 do not have a response.
123-
// response.json will always exist on a fetch, but can only be
124-
// await-ed when the response is not empty, otherwise it will
125-
// throw an error.
126-
try {
127-
aliasResponseBody = await response.json();
128-
} catch (e) {
129-
verbose('The request has no response body');
130-
}
131-
} else {
132-
// https://go.mparticle.com/work/SQDSDKS-6568
133-
// XHRUploader returns the response as a string that we need to parse
134-
const xhrResponse = (response as unknown) as XMLHttpRequest;
135-
136-
aliasResponseBody = xhrResponse.responseText
137-
? JSON.parse(xhrResponse.responseText)
138-
: '';
139-
}
140-
118+
let message: string;
141119
let errorMessage: string;
142120

143121
switch (response.status) {
144-
// Alias response is a 202 with no body
145-
case HTTP_OK:
146122
case HTTP_ACCEPTED:
147-
// https://go.mparticle.com/work/SQDSDKS-6670
148-
message =
149-
'Successfully sent forwarding stats to mParticle Servers';
150-
break;
151-
default: {
152-
// 400 has an error message, but 403 doesn't
153-
const errorResponse: IAliasErrorResponse = aliasResponseBody as unknown as IAliasErrorResponse;
154-
if (errorResponse?.message) {
155-
errorMessage = errorResponse.message;
123+
case HTTP_OK:
124+
125+
// 400 error will has a body and will go through the happy path to report the error
126+
case HTTP_BAD_REQUEST:
127+
128+
// FetchUploader returns the response as a JSON object that we have to await
129+
if (response.json) {
130+
// HTTP responses of 202, 200, and 403 do not have a response.
131+
// response.json will always exist on a fetch, but can only be
132+
// await-ed when the response is not empty, otherwise it will
133+
// throw an error.
134+
try {
135+
aliasResponseBody = await response.json();
136+
} catch (e) {
137+
verbose('The request has no response body');
138+
}
139+
} else {
140+
// https://go.mparticle.com/work/SQDSDKS-6568
141+
// XHRUploader returns the response as a string that we need to parse
142+
const xhrResponse = (response as unknown) as XMLHttpRequest;
143+
144+
aliasResponseBody = xhrResponse.responseText
145+
? JSON.parse(xhrResponse.responseText)
146+
: '';
147+
148+
// https://go.mparticle.com/work/SQDSDKS-6670
149+
message =
150+
'Successfully sent forwarding stats to mParticle Servers';
156151
}
157-
message =
158-
'Issue with sending Alias Request to mParticle Servers, received HTTP Code of ' +
159-
response.status;
160-
161-
if (errorResponse?.code) {
162-
message += ' - ' + errorResponse.code;
152+
153+
154+
if (response.status === HTTP_BAD_REQUEST) {
155+
// 400 has an error message, but 403 doesn't
156+
const errorResponse: IAliasErrorResponse = aliasResponseBody as unknown as IAliasErrorResponse;
157+
if (errorResponse?.message) {
158+
errorMessage = errorResponse.message;
159+
}
160+
message =
161+
'Issue with sending Alias Request to mParticle Servers, received HTTP Code of ' +
162+
response.status;
163+
164+
if (errorResponse?.code) {
165+
message += ' - ' + errorResponse.code;
166+
}
163167
}
168+
169+
break;
170+
171+
// 401 and 403 have no bodies and should be rejected outright
172+
default: {
173+
throw new Error('Received HTTP Code of ' + response.status);
164174
}
175+
165176
}
166177

167178
verbose(message);
@@ -230,42 +241,53 @@ export default function IdentityAPIClient(
230241
let identityResponse: IIdentityResponse;
231242
let message: string;
232243

233-
// FetchUploader returns the response as a JSON object that we have to await
234-
if (response.json) {
235-
// https://go.mparticle.com/work/SQDSDKS-6568
236-
// FetchUploader returns the response as a JSON object that we have to await
237-
const responseBody: IdentityResultBody = await response.json();
238-
239-
identityResponse = this.getIdentityResponseFromFetch(
240-
response,
241-
responseBody
242-
);
243-
} else {
244-
identityResponse = this.getIdentityResponseFromXHR(
245-
(response as unknown) as XMLHttpRequest
246-
);
247-
}
248-
249-
250-
switch (identityResponse.status) {
251-
case HTTP_OK:
244+
switch (response.status) {
252245
case HTTP_ACCEPTED:
253-
message = 'Received Identity Response from server: ';
254-
message += JSON.stringify(identityResponse.responseText);
255-
break;
246+
case HTTP_OK:
256247

257-
default: {
258-
// 400 has an error message, but 403 doesn't
259-
const errorResponse: IdentityApiErrorResponse = identityResponse.responseText as unknown as IdentityApiErrorResponse;
260-
if (errorResponse?.Errors) {
248+
// 400 error will has a body and will go through the happy path to report the error
249+
case HTTP_BAD_REQUEST:
250+
251+
// FetchUploader returns the response as a JSON object that we have to await
252+
if (response.json) {
253+
// https://go.mparticle.com/work/SQDSDKS-6568
254+
// FetchUploader returns the response as a JSON object that we have to await
255+
const responseBody: IdentityResultBody = await response.json();
256+
257+
identityResponse = this.getIdentityResponseFromFetch(
258+
response,
259+
responseBody
260+
);
261+
} else {
262+
identityResponse = this.getIdentityResponseFromXHR(
263+
(response as unknown) as XMLHttpRequest
264+
);
265+
}
266+
267+
if (identityResponse.status === HTTP_BAD_REQUEST) {
268+
const errorResponse: IdentityApiErrorResponse = identityResponse.responseText as unknown as IdentityApiErrorResponse;
261269
message = 'Issue with sending Identity Request to mParticle Servers, received HTTP Code of ' + identityResponse.status;
262270

263-
const errorMessage = errorResponse.Errors.map((error) => error.message).join(', ');
264-
message += ' - ' + errorMessage;
271+
if (errorResponse?.Errors) {
272+
const errorMessage = errorResponse.Errors.map((error) => error.message).join(', ');
273+
message += ' - ' + errorMessage;
274+
}
275+
276+
} else {
277+
message = 'Received Identity Response from server: ';
278+
message += JSON.stringify(identityResponse.responseText);
265279
}
280+
281+
break;
282+
283+
// 401 and 403 have no bodies and should be rejected outright
284+
default: {
285+
throw new Error('Received HTTP Code of ' + response.status);
266286
}
267287
}
268288

289+
mpInstance._Store.identityCallInFlight = false;
290+
269291
verbose(message);
270292
parseIdentityResponse(
271293
identityResponse,
@@ -280,6 +302,7 @@ export default function IdentityAPIClient(
280302
mpInstance._Store.identityCallInFlight = false;
281303

282304
const errorMessage = (err as Error).message || err.toString();
305+
283306
error('Error sending identity request to servers' + ' - ' + errorMessage);
284307
invokeCallback(
285308
callback,

0 commit comments

Comments
 (0)