Skip to content

Commit 5564943

Browse files
Address PR Comments
1 parent 7c855c5 commit 5564943

File tree

1 file changed

+20
-35
lines changed

1 file changed

+20
-35
lines changed

src/identityApiClient.ts

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -119,66 +119,51 @@ export default function IdentityAPIClient(
119119
let errorMessage: string;
120120

121121
switch (response.status) {
122+
// A successfull Alias request will return without a body
122123
case HTTP_ACCEPTED:
123124
case HTTP_OK:
125+
// https://go.mparticle.com/work/SQDSDKS-6670
126+
message = 'Received Alias Response from server: ' + JSON.stringify(response.status);
127+
break;
124128

125129
// Our Identity API will return a 400 error if there is an issue with the requeest body
126130
// such as if the body is empty or one of the attirbutes is missing or malformed
127131
// A 400 will return an error in the response body and will go through the happy path to report the error
128-
// Any unhandled errors, such as 500 or 429, will be caught here as well
129132
case HTTP_BAD_REQUEST:
130-
131-
// FetchUploader returns the response as a JSON object that we have to await
133+
// response.json will always exist on a fetch, but can only be await-ed when the
134+
// response is not empty, otherwise it will throw an error.
132135
if (response.json) {
133-
// HTTP responses of 202, 200, and 403 do not have a response body.
134-
// response.json will always exist on a fetch, but can only be
135-
// await-ed when the response is not empty, otherwise it will
136-
// throw an error.
137136
try {
138137
aliasResponseBody = await response.json();
139138
} catch (e) {
140-
verbose('No response body for Alias request');
139+
verbose('The request has no response body');
141140
}
142141
} else {
143142
// https://go.mparticle.com/work/SQDSDKS-6568
144143
// XHRUploader returns the response as a string that we need to parse
145144
const xhrResponse = (response as unknown) as XMLHttpRequest;
146-
145+
147146
aliasResponseBody = xhrResponse.responseText
148147
? JSON.parse(xhrResponse.responseText)
149148
: '';
150149
}
151150

152-
if (response.status === HTTP_BAD_REQUEST) {
153-
// 400 has an error message, but 403 doesn't
154-
const errorResponse: IAliasErrorResponse = aliasResponseBody as unknown as IAliasErrorResponse;
155-
if (errorResponse?.message) {
156-
errorMessage = errorResponse.message;
157-
}
158-
message =
159-
'Issue with sending Alias Request to mParticle Servers, received HTTP Code of ' +
160-
response.status;
161-
162-
if (errorResponse?.code) {
163-
message += ' - ' + errorResponse.code;
164-
}
165-
} else {
166-
// https://go.mparticle.com/work/SQDSDKS-6670
167-
message = 'Received Alias Response from server: ';
168-
message += JSON.stringify(response.status);
151+
const errorResponse: IAliasErrorResponse = aliasResponseBody as unknown as IAliasErrorResponse;
169152

170-
// In case we receive a valid HTTP code with a response body
171-
if (aliasResponseBody) {
172-
message += ' - ' + JSON.stringify(aliasResponseBody);
173-
}
153+
if (errorResponse?.message) {
154+
errorMessage = errorResponse.message;
155+
}
156+
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;
174163
}
175164

176165
break;
177166

178-
// Our Identity API will return:
179-
// - 401 if the `x-mp-key` is incorrect or missing
180-
// - 403 if the there is a permission or account issue related to the `x-mp-key`
181-
// 401 and 403 have no response bodies and should be rejected outright
182167
// Any unhandled errors, such as 500 or 429, will be caught here as well
183168
default: {
184169
throw new Error('Received HTTP Code of ' + response.status);
@@ -257,7 +242,7 @@ export default function IdentityAPIClient(
257242
case HTTP_OK:
258243

259244
// Our Identity API will return a 400 error if there is an issue with the requeest body
260-
// such as if the body is empty or one of the attirbutes is missing or malformed
245+
// such as if the body is empty or one of the attributes is missing or malformed
261246
// A 400 will return an error in the response body and will go through the happy path to report the error
262247
case HTTP_BAD_REQUEST:
263248

0 commit comments

Comments
 (0)