mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 15:09:33 +01:00
feat: better errors when server fails
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import { extractHostname } from "./hostname";
|
||||||
|
|
||||||
const hosts = {
|
const hosts = {
|
||||||
API_HOST:
|
API_HOST:
|
||||||
process.env.NODE_ENV === "production"
|
process.env.NODE_ENV === "production"
|
||||||
@@ -22,3 +24,16 @@ const hosts = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default hosts;
|
export default hosts;
|
||||||
|
|
||||||
|
export const getServerNameFromHost = (host) => {
|
||||||
|
const names = {
|
||||||
|
[extractHostname(hosts.API_HOST)]: "Notesnook Sync Server",
|
||||||
|
[extractHostname(hosts.AUTH_HOST)]: "Authentication Server",
|
||||||
|
[extractHostname(hosts.SSE_HOST)]: "Eventing Server",
|
||||||
|
[extractHostname(hosts.SUBSCRIPTIONS_HOST)]:
|
||||||
|
"Subscriptions Management Server",
|
||||||
|
[extractHostname(hosts.ISSUES_HOST)]: "Bug Reporting Server",
|
||||||
|
};
|
||||||
|
console.log(names, host);
|
||||||
|
return names[host];
|
||||||
|
};
|
||||||
|
|||||||
17
packages/core/utils/hostname.js
Normal file
17
packages/core/utils/hostname.js
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
export function extractHostname(url) {
|
||||||
|
var hostname;
|
||||||
|
//find & remove protocol (http, ftp, etc.) and get hostname
|
||||||
|
|
||||||
|
if (url.indexOf("//") > -1) {
|
||||||
|
hostname = url.split("/")[2];
|
||||||
|
} else {
|
||||||
|
hostname = url.split("/")[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
//find & remove port number
|
||||||
|
// hostname = hostname.split(":")[0];
|
||||||
|
//find & remove "?"
|
||||||
|
hostname = hostname.split("?")[0];
|
||||||
|
|
||||||
|
return hostname;
|
||||||
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
import { EV, EVENTS } from "../common";
|
import { EV, EVENTS } from "../common";
|
||||||
|
import { getServerNameFromHost } from "./constants";
|
||||||
|
import { extractHostname } from "./hostname";
|
||||||
|
|
||||||
function get(url, token) {
|
function get(url, token) {
|
||||||
return request(url, token, "GET");
|
return request(url, token, "GET");
|
||||||
@@ -73,7 +75,7 @@ async function handleResponse(response) {
|
|||||||
|
|
||||||
async function request(url, token, method) {
|
async function request(url, token, method) {
|
||||||
return handleResponse(
|
return handleResponse(
|
||||||
await fetch(url, {
|
await fetchWrapped(url, {
|
||||||
method,
|
method,
|
||||||
headers: getAuthorizationHeader(token),
|
headers: getAuthorizationHeader(token),
|
||||||
})
|
})
|
||||||
@@ -88,7 +90,7 @@ async function bodyRequest(
|
|||||||
contentType = "application/x-www-form-urlencoded"
|
contentType = "application/x-www-form-urlencoded"
|
||||||
) {
|
) {
|
||||||
return handleResponse(
|
return handleResponse(
|
||||||
await fetch(url, {
|
await fetchWrapped(url, {
|
||||||
method,
|
method,
|
||||||
body: transformer(data, contentType),
|
body: transformer(data, contentType),
|
||||||
headers: {
|
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
|
// * @param {RequestInfo} resource
|
||||||
|
|||||||
Reference in New Issue
Block a user