add logError: boolean param to fetcher

Case for it is getUser api method. When user is not authenticated, api returns 403 error which pops up in console and have no meaning
This commit is contained in:
Vyrtsev Mikhail
2019-04-20 21:24:00 +03:00
parent d1420286dd
commit 22006bee81
2 changed files with 8 additions and 2 deletions
+1
View File
@@ -133,6 +133,7 @@ export const getUser = (): Promise<User | null> =>
.get<User | null>({
url: '/user',
withCredentials: true,
logError: false,
})
.catch(() => null);
+7 -2
View File
@@ -10,6 +10,8 @@ interface FetcherInitBase {
url: string;
overriddenApiBase?: string;
withCredentials?: boolean;
/** whether log error message to console */
logError?: boolean;
}
interface FetcherInitJSON extends FetcherInitBase {
@@ -34,6 +36,7 @@ const fetcher = methods.reduce<Partial<FetcherObject>>((acc, method) => {
withCredentials = false,
overriddenApiBase = API_BASE,
contentType = 'application/json',
logError = true,
} = typeof data === 'string' ? { url: data } : data;
const basename = `${BASE_URL}${overriddenApiBase}`;
@@ -81,8 +84,10 @@ const fetcher = methods.reduce<Partial<FetcherObject>>((acc, method) => {
try {
err = JSON.parse(text);
} catch (e) {
// eslint-disable-next-line no-console
console.error(err);
if (logError) {
// eslint-disable-next-line no-console
console.error(err);
}
throw 'Something went wrong.';
}
throw err;