feat: better errors when server fails

This commit is contained in:
thecodrr
2022-01-18 12:00:57 +05:00
parent 17eeaeb72c
commit 2e50130c16
3 changed files with 57 additions and 2 deletions

View File

@@ -1,4 +1,6 @@
import { EV, EVENTS } from "../common";
import { getServerNameFromHost } from "./constants";
import { extractHostname } from "./hostname";
function get(url, token) {
return request(url, token, "GET");
@@ -73,7 +75,7 @@ async function handleResponse(response) {
async function request(url, token, method) {
return handleResponse(
await fetch(url, {
await fetchWrapped(url, {
method,
headers: getAuthorizationHeader(token),
})
@@ -88,7 +90,7 @@ async function bodyRequest(
contentType = "application/x-www-form-urlencoded"
) {
return handleResponse(
await fetch(url, {
await fetchWrapped(url, {
method,
body: transformer(data, contentType),
headers: {
@@ -126,6 +128,27 @@ function errorTransformer(errorJson) {
}
}
/**
*
* @param {RequestInfo} input
* @param {RequestInit} init
*/
async function fetchWrapped(input, init) {
try {
const response = await fetch(input, init);
return response;
} catch (e) {
const host = extractHostname(input);
const serverName = getServerNameFromHost(host);
if (serverName)
throw new Error(
`${serverName} is not responding. Please check your internet connection. If the problem persists, feel free email us at support@streetwriters.co. (Reference error: ${e.message})`
);
throw e;
}
}
// /**
// *
// * @param {RequestInfo} resource