2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 02:49:03 +00:00

incr: simplify error logging, send to renderer

This commit is contained in:
18alantom 2022-05-27 15:09:24 +05:30 committed by Alan
parent 12f4162162
commit b7d41db996
11 changed files with 63 additions and 65 deletions

View File

@ -83,7 +83,6 @@ async function setOtherSettings(fyo: Fyo) {
await acc.setAndSync({
gstin: '27LIN180000A1Z5',
});
console.log(acc.gstin, await fyo.db.getSingleValues('gstin'));
}
/**

View File

@ -1,6 +1,7 @@
import { Fyo } from 'fyo';
import { AuthDemux } from 'fyo/demux/auth';
import { AuthDemuxBase, TelemetryCreds } from 'utils/auth/types';
import { AuthDemuxBase } from 'utils/auth/types';
import { Creds } from 'utils/types';
import { AuthDemuxConstructor } from './types';
interface AuthConfig {
@ -19,6 +20,7 @@ export class AuthHandler {
#session: Session;
fyo: Fyo;
#demux: AuthDemuxBase;
#creds?: Creds;
constructor(fyo: Fyo, Demux?: AuthDemuxConstructor) {
this.fyo = fyo;
@ -111,7 +113,11 @@ export class AuthHandler {
return this.#config.serverURL || '';
}
async getTelemetryCreds(): Promise<TelemetryCreds> {
return await this.#demux.getTelemetryCreds();
async getCreds(): Promise<Creds> {
if (!this.#creds) {
this.#creds = await this.#demux.getCreds();
}
return this.#creds;
}
}

View File

@ -1,6 +1,7 @@
import { ipcRenderer } from 'electron';
import { AuthDemuxBase, TelemetryCreds } from 'utils/auth/types';
import { AuthDemuxBase } from 'utils/auth/types';
import { IPC_ACTIONS } from 'utils/messages';
import { Creds } from 'utils/types';
export class AuthDemux extends AuthDemuxBase {
#isElectron: boolean = false;
@ -9,14 +10,11 @@ export class AuthDemux extends AuthDemuxBase {
this.#isElectron = isElectron;
}
async getTelemetryCreds(): Promise<TelemetryCreds> {
async getCreds(): Promise<Creds> {
if (this.#isElectron) {
const creds = await ipcRenderer.invoke(IPC_ACTIONS.GET_CREDS);
const url: string = creds?.telemetryUrl ?? '';
const token: string = creds?.tokenString ?? '';
return { url, token };
return (await ipcRenderer.invoke(IPC_ACTIONS.GET_CREDS)) as Creds;
} else {
return { url: '', token: '' };
return { errorLogUrl: '', tokenString: '', telemetryUrl: '' };
}
}
}

View File

@ -118,9 +118,9 @@ export class TelemetryManager {
return;
}
const { url, token } = await this.fyo.auth.getTelemetryCreds();
this.#url = url;
this.#token = token;
const { telemetryUrl, tokenString } = await this.fyo.auth.getCreds();
this.#url = telemetryUrl;
this.#token = tokenString;
}
#getTelemtryData(

View File

@ -1,7 +1,8 @@
import { AuthDemuxBase, TelemetryCreds } from 'utils/auth/types';
import { AuthDemuxBase } from 'utils/auth/types';
import { Creds } from 'utils/types';
export class DummyAuthDemux extends AuthDemuxBase {
async getTelemetryCreds(): Promise<TelemetryCreds> {
return { url: '', token: '' };
async getCreds(): Promise<Creds> {
return { errorLogUrl: '', tokenString: '', telemetryUrl: '' };
}
}

View File

@ -1,12 +1,13 @@
import { app } from 'electron';
import fs from 'fs';
import http from 'http';
import https from 'https';
import fetch from 'node-fetch';
import path from 'path';
import { Creds } from 'utils/types';
import { rendererLog } from './helpers';
export function getUrlAndTokenString() {
export function getUrlAndTokenString(): Creds {
const inProduction = app.isPackaged;
const empty = { url: '', telemetryUrl: '', tokenString: '' };
const empty: Creds = { errorLogUrl: '', telemetryUrl: '', tokenString: '' };
let errLogCredsPath = path.join(
process.resourcesPath,
'../creds/log_creds.txt'
@ -20,9 +21,9 @@ export function getUrlAndTokenString() {
return empty;
}
let apiKey, apiSecret, url, telemetryUrl;
let apiKey, apiSecret, errorLogUrl, telemetryUrl;
try {
[apiKey, apiSecret, url, telemetryUrl] = fs
[apiKey, apiSecret, errorLogUrl, telemetryUrl] = fs
.readFileSync(errLogCredsPath, 'utf-8')
.split('\n')
.filter((f) => f.length);
@ -35,53 +36,21 @@ export function getUrlAndTokenString() {
}
return {
url: encodeURI(url),
errorLogUrl: encodeURI(errorLogUrl),
telemetryUrl: encodeURI(telemetryUrl),
tokenString: `token ${apiKey}:${apiSecret}`,
};
}
function post(bodyJson: string) {
const inProduction = app.isPackaged;
const { url, tokenString } = getUrlAndTokenString();
const isHttps = url.split(':')[0].toLowerCase() === 'https';
export async function sendError(body: string) {
const { errorLogUrl, tokenString } = getUrlAndTokenString();
const headers = {
Authorization: tokenString,
Accept: 'application/json',
'Content-Type': 'application/json',
};
const req = (isHttps ? https : http).request(
url,
{
method: 'POST',
headers,
},
(res) => {
if (inProduction) {
return;
}
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
}
);
req.on('error', (e) => {
console.log(`ERROR: ${e.message}`);
await fetch(errorLogUrl, { method: 'POST', headers, body }).catch((err) => {
rendererLog(err);
});
req.write(bodyJson);
req.end();
}
export function sendError(bodyJson: string) {
post(bodyJson);
}
// Nothing nefarious going on here.
// Just regular old user mandated error logging.

View File

@ -42,6 +42,7 @@
"@types/luxon": "^2.3.1",
"@types/mocha": "^9.1.0",
"@types/node": "^17.0.23",
"@types/node-fetch": "^2.6.1",
"@typescript-eslint/eslint-plugin": "^4.15.1",
"@typescript-eslint/parser": "^4.15.1",
"@vue/cli-plugin-babel": "^4.5.0",

View File

@ -55,7 +55,13 @@ export default function registerIpcRendererListeners() {
await handleError(true, error as Error);
});
ipcRenderer.on(IPC_CHANNELS.CONSOLE_LOG, console.log);
ipcRenderer.on(IPC_CHANNELS.CONSOLE_LOG, (_, ...stuff: unknown[]) => {
if (!fyo.store.isDevelopment) {
return;
}
console.log(...stuff);
});
document.addEventListener('visibilitychange', function () {
const { visibilityState } = document;

View File

@ -1,5 +1,5 @@
export type TelemetryCreds = { url: string; token: string };
import { Creds } from 'utils/types';
export abstract class AuthDemuxBase {
abstract getTelemetryCreds(): Promise<TelemetryCreds>
abstract getCreds(): Promise<Creds>;
}

View File

@ -16,10 +16,11 @@ export interface CountryInfo {
locale: string;
}
export interface VersionParts {
major: number;
minor: number;
patch: number;
beta?: number;
};
}
export type Creds = { errorLogUrl: string; telemetryUrl: string; tokenString: string };

View File

@ -1447,6 +1447,14 @@
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==
"@types/node-fetch@^2.6.1":
version "2.6.1"
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.1.tgz#8f127c50481db65886800ef496f20bbf15518975"
integrity sha512-oMqjURCaxoSIsHSr1E47QHzbmzNR5rK8McHuNb11BOM9cHcIK3Avy0s/b2JlXHoQGTYS3NsvWzV1M0iK7l0wbA==
dependencies:
"@types/node" "*"
form-data "^3.0.0"
"@types/node@*":
version "17.0.8"
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.8.tgz#50d680c8a8a78fe30abe6906453b21ad8ab0ad7b"
@ -6058,6 +6066,15 @@ fork-ts-checker-webpack-plugin@^3.1.1:
tapable "^1.0.0"
worker-rpc "^0.1.0"
form-data@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.1.tgz#ebd53791b78356a99af9a300d4282c4d5eb9755f"
integrity sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"
form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"