- saving progress;

This commit is contained in:
Vitalii Kiiko
2024-12-03 11:47:53 +01:00
parent 92ac388a5b
commit 2816d388a5
9 changed files with 159 additions and 34 deletions

View File

@@ -1,9 +1,36 @@
import { storeGet } from '../store';
import * as Sentry from "@sentry/browser";
const LOCAL_DEVELOPMENT = process.env.REACT_APP_LOCAL_DEVELOPMENT;
export class NetworkService {
static TOKEN_KEY
static REFRESH_TOKEN_KEY
static logApiError = (endpoint, status = 0, resp) => {
try {
if ([500].includes(status)) {
if (LOCAL_DEVELOPMENT !== '1') {
Sentry.init({
dsn: "https://e7b2134f7d816f663bb83e51b106a694@o4508381921738752.ingest.de.sentry.io/4508381935501392",
environment: process.env.NODE_ENV || "development"
});
const error = new Error(`Status ${status}`);
Sentry.captureException(`Error in endpoint: ${endpoint}`, {
level: 'error',
extra: {
originalError: error,
details: resp
}
});
}
}
} catch (err) {
console.log(err);
}
}
static postEmptyResponse = (url, body, callback, errorCallback) => {
fetch(url, {
method: 'POST',
@@ -77,10 +104,12 @@ export class NetworkService {
return { response: await response.json(), status: status }
})
.then(data => {
if (data.status >= 400 && data.status <= 599)
if (data.status >= 400 && data.status <= 599) {
errorCallback(data.response)
else
this.logApiError(url, data.status, data.response);
} else {
callback(data.response)
}
})
.catch(err => errorCallback(err));
};
@@ -118,10 +147,12 @@ export class NetworkService {
return { response: await response.json(), status: status }
})
.then(data => {
if (data.status >= 400 && data.status <= 599)
if (data.status >= 400 && data.status <= 599) {
errorCallback(data.response)
else
this.logApiError(url, data.status, data.response);
} else {
callback(data.response)
}
})
.catch(err => errorCallback(err));
};
@@ -159,10 +190,12 @@ export class NetworkService {
return { response: await response.blob(), status: status }
})
.then(data => {
if (data.status >= 400 && data.status <= 599)
if (data.status >= 400 && data.status <= 599) {
errorCallback(data.response)
else
this.logApiError(url, data.status, data.response);
} else {
callback(data.response)
}
})
.catch(err => errorCallback(err));
};
@@ -198,10 +231,12 @@ export class NetworkService {
return { response: await response.json(), status: status }
})
.then(data => {
if (data.status >= 400 && data.status <= 599)
if (data.status >= 400 && data.status <= 599) {
errorCallback(data.response)
else
this.logApiError(url, data.status, data.response);
} else {
callback(data.response)
}
})
.catch(err => errorCallback(err));
};
@@ -261,10 +296,12 @@ export class NetworkService {
return { response: await response.json(), status: status }
})
.then(data => {
if (data.status >= 400 && data.status <= 599)
if (data.status >= 400 && data.status <= 599) {
errorCallback(data.response)
else
this.logApiError(url, data.status, data.response);
} else {
callback(data.response)
}
})
.catch(err => errorCallback(err));
};
@@ -381,12 +418,15 @@ export class NetworkService {
})
.then(data => {
if (data.status >= 400 && data.status <= 599) {
errorCallback(data.response)
errorCallback(data.response);
this.logApiError(url, data.status, data.response);
} else {
callback(data.response)
}
})
.catch(err => errorCallback(err));
.catch(err => {
errorCallback(err)
});
};
@@ -422,10 +462,12 @@ export class NetworkService {
return { response: await response.blob(), status: status }
})
.then(data => {
if (data.status >= 400 && data.status <= 599)
if (data.status >= 400 && data.status <= 599) {
errorCallback(data.response)
else
this.logApiError(url, data.status, data.response);
} else {
callback(data.response)
}
})
.catch(err => errorCallback(err));
};
@@ -504,10 +546,12 @@ export class NetworkService {
return { response: await response.json(), status: status }
})
.then(data => {
if (data.status >= 400 && data.status <= 599)
if (data.status >= 400 && data.status <= 599) {
errorCallback(data.response)
else
this.logApiError(url, data.status, data.response);
} else {
callback(data.response)
}
})
.catch(err => errorCallback(err));