Skip to content

Commit

Permalink
Replace falsy by nullish operator for token.
Browse files Browse the repository at this point in the history
Put back the code with new Error() and update associated TS.

Signed-off-by: Florent MILLOT <millotflo@gmail.com>
  • Loading branch information
flomillot committed May 15, 2024
1 parent 23734e5 commit e8af985
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const prepareRequest = (init: any, token?: string) => {
}
const initCopy = Object.assign({}, init);
initCopy.headers = new Headers(initCopy.headers || {});
const tokenCopy = token ? token : getUserToken();
const tokenCopy = token ?? getUserToken();
initCopy.headers.append('Authorization', 'Bearer ' + tokenCopy);
return initCopy;
};
Expand All @@ -35,29 +35,31 @@ const handleError = (response: any) => {
return response.text().then((text: string) => {
const errorName = 'HttpResponseError : ';
const errorJson = parseError(text);
let customError: { message?: string; status?: string } = {};
let customError: Error & { status?: string };
if (
errorJson &&
errorJson.status &&
errorJson.error &&
errorJson.message
) {
customError.message =
customError = new Error(
errorName +
errorJson.status +
' ' +
errorJson.error +
', message : ' +
errorJson.message;
errorJson.status +
' ' +
errorJson.error +
', message : ' +
errorJson.message
);
customError.status = errorJson.status;
} else {
customError.message =
customError = new Error(
errorName +
response.status +
' ' +
response.statusText +
', message : ' +
text;
response.status +
' ' +
response.statusText +
', message : ' +
text
);
customError.status = response.status;
}
throw customError;
Expand Down

0 comments on commit e8af985

Please sign in to comment.