mirror of
https://github.com/frappe/books.git
synced 2025-01-03 07:12:21 +00:00
incr: simplify error logging, send to renderer
This commit is contained in:
parent
12f4162162
commit
b7d41db996
@ -83,7 +83,6 @@ async function setOtherSettings(fyo: Fyo) {
|
|||||||
await acc.setAndSync({
|
await acc.setAndSync({
|
||||||
gstin: '27LIN180000A1Z5',
|
gstin: '27LIN180000A1Z5',
|
||||||
});
|
});
|
||||||
console.log(acc.gstin, await fyo.db.getSingleValues('gstin'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { Fyo } from 'fyo';
|
import { Fyo } from 'fyo';
|
||||||
import { AuthDemux } from 'fyo/demux/auth';
|
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';
|
import { AuthDemuxConstructor } from './types';
|
||||||
|
|
||||||
interface AuthConfig {
|
interface AuthConfig {
|
||||||
@ -19,6 +20,7 @@ export class AuthHandler {
|
|||||||
#session: Session;
|
#session: Session;
|
||||||
fyo: Fyo;
|
fyo: Fyo;
|
||||||
#demux: AuthDemuxBase;
|
#demux: AuthDemuxBase;
|
||||||
|
#creds?: Creds;
|
||||||
|
|
||||||
constructor(fyo: Fyo, Demux?: AuthDemuxConstructor) {
|
constructor(fyo: Fyo, Demux?: AuthDemuxConstructor) {
|
||||||
this.fyo = fyo;
|
this.fyo = fyo;
|
||||||
@ -111,7 +113,11 @@ export class AuthHandler {
|
|||||||
return this.#config.serverURL || '';
|
return this.#config.serverURL || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
async getTelemetryCreds(): Promise<TelemetryCreds> {
|
async getCreds(): Promise<Creds> {
|
||||||
return await this.#demux.getTelemetryCreds();
|
if (!this.#creds) {
|
||||||
|
this.#creds = await this.#demux.getCreds();
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.#creds;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { ipcRenderer } from 'electron';
|
import { ipcRenderer } from 'electron';
|
||||||
import { AuthDemuxBase, TelemetryCreds } from 'utils/auth/types';
|
import { AuthDemuxBase } from 'utils/auth/types';
|
||||||
import { IPC_ACTIONS } from 'utils/messages';
|
import { IPC_ACTIONS } from 'utils/messages';
|
||||||
|
import { Creds } from 'utils/types';
|
||||||
|
|
||||||
export class AuthDemux extends AuthDemuxBase {
|
export class AuthDemux extends AuthDemuxBase {
|
||||||
#isElectron: boolean = false;
|
#isElectron: boolean = false;
|
||||||
@ -9,14 +10,11 @@ export class AuthDemux extends AuthDemuxBase {
|
|||||||
this.#isElectron = isElectron;
|
this.#isElectron = isElectron;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getTelemetryCreds(): Promise<TelemetryCreds> {
|
async getCreds(): Promise<Creds> {
|
||||||
if (this.#isElectron) {
|
if (this.#isElectron) {
|
||||||
const creds = await ipcRenderer.invoke(IPC_ACTIONS.GET_CREDS);
|
return (await ipcRenderer.invoke(IPC_ACTIONS.GET_CREDS)) as Creds;
|
||||||
const url: string = creds?.telemetryUrl ?? '';
|
|
||||||
const token: string = creds?.tokenString ?? '';
|
|
||||||
return { url, token };
|
|
||||||
} else {
|
} else {
|
||||||
return { url: '', token: '' };
|
return { errorLogUrl: '', tokenString: '', telemetryUrl: '' };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,9 +118,9 @@ export class TelemetryManager {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { url, token } = await this.fyo.auth.getTelemetryCreds();
|
const { telemetryUrl, tokenString } = await this.fyo.auth.getCreds();
|
||||||
this.#url = url;
|
this.#url = telemetryUrl;
|
||||||
this.#token = token;
|
this.#token = tokenString;
|
||||||
}
|
}
|
||||||
|
|
||||||
#getTelemtryData(
|
#getTelemtryData(
|
||||||
|
@ -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 {
|
export class DummyAuthDemux extends AuthDemuxBase {
|
||||||
async getTelemetryCreds(): Promise<TelemetryCreds> {
|
async getCreds(): Promise<Creds> {
|
||||||
return { url: '', token: '' };
|
return { errorLogUrl: '', tokenString: '', telemetryUrl: '' };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
import { app } from 'electron';
|
import { app } from 'electron';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import http from 'http';
|
import fetch from 'node-fetch';
|
||||||
import https from 'https';
|
|
||||||
import path from 'path';
|
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 inProduction = app.isPackaged;
|
||||||
const empty = { url: '', telemetryUrl: '', tokenString: '' };
|
const empty: Creds = { errorLogUrl: '', telemetryUrl: '', tokenString: '' };
|
||||||
let errLogCredsPath = path.join(
|
let errLogCredsPath = path.join(
|
||||||
process.resourcesPath,
|
process.resourcesPath,
|
||||||
'../creds/log_creds.txt'
|
'../creds/log_creds.txt'
|
||||||
@ -20,9 +21,9 @@ export function getUrlAndTokenString() {
|
|||||||
return empty;
|
return empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
let apiKey, apiSecret, url, telemetryUrl;
|
let apiKey, apiSecret, errorLogUrl, telemetryUrl;
|
||||||
try {
|
try {
|
||||||
[apiKey, apiSecret, url, telemetryUrl] = fs
|
[apiKey, apiSecret, errorLogUrl, telemetryUrl] = fs
|
||||||
.readFileSync(errLogCredsPath, 'utf-8')
|
.readFileSync(errLogCredsPath, 'utf-8')
|
||||||
.split('\n')
|
.split('\n')
|
||||||
.filter((f) => f.length);
|
.filter((f) => f.length);
|
||||||
@ -35,53 +36,21 @@ export function getUrlAndTokenString() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
url: encodeURI(url),
|
errorLogUrl: encodeURI(errorLogUrl),
|
||||||
telemetryUrl: encodeURI(telemetryUrl),
|
telemetryUrl: encodeURI(telemetryUrl),
|
||||||
tokenString: `token ${apiKey}:${apiSecret}`,
|
tokenString: `token ${apiKey}:${apiSecret}`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function post(bodyJson: string) {
|
export async function sendError(body: string) {
|
||||||
const inProduction = app.isPackaged;
|
const { errorLogUrl, tokenString } = getUrlAndTokenString();
|
||||||
const { url, tokenString } = getUrlAndTokenString();
|
|
||||||
const isHttps = url.split(':')[0].toLowerCase() === 'https';
|
|
||||||
const headers = {
|
const headers = {
|
||||||
Authorization: tokenString,
|
Authorization: tokenString,
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
};
|
};
|
||||||
|
|
||||||
const req = (isHttps ? https : http).request(
|
await fetch(errorLogUrl, { method: 'POST', headers, body }).catch((err) => {
|
||||||
url,
|
rendererLog(err);
|
||||||
{
|
|
||||||
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}`);
|
|
||||||
});
|
|
||||||
req.write(bodyJson);
|
|
||||||
req.end();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function sendError(bodyJson: string) {
|
|
||||||
post(bodyJson);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Nothing nefarious going on here.
|
|
||||||
// Just regular old user mandated error logging.
|
|
||||||
|
@ -42,6 +42,7 @@
|
|||||||
"@types/luxon": "^2.3.1",
|
"@types/luxon": "^2.3.1",
|
||||||
"@types/mocha": "^9.1.0",
|
"@types/mocha": "^9.1.0",
|
||||||
"@types/node": "^17.0.23",
|
"@types/node": "^17.0.23",
|
||||||
|
"@types/node-fetch": "^2.6.1",
|
||||||
"@typescript-eslint/eslint-plugin": "^4.15.1",
|
"@typescript-eslint/eslint-plugin": "^4.15.1",
|
||||||
"@typescript-eslint/parser": "^4.15.1",
|
"@typescript-eslint/parser": "^4.15.1",
|
||||||
"@vue/cli-plugin-babel": "^4.5.0",
|
"@vue/cli-plugin-babel": "^4.5.0",
|
||||||
|
@ -55,7 +55,13 @@ export default function registerIpcRendererListeners() {
|
|||||||
await handleError(true, error as Error);
|
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 () {
|
document.addEventListener('visibilitychange', function () {
|
||||||
const { visibilityState } = document;
|
const { visibilityState } = document;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
export type TelemetryCreds = { url: string; token: string };
|
import { Creds } from 'utils/types';
|
||||||
|
|
||||||
export abstract class AuthDemuxBase {
|
export abstract class AuthDemuxBase {
|
||||||
abstract getTelemetryCreds(): Promise<TelemetryCreds>
|
abstract getCreds(): Promise<Creds>;
|
||||||
}
|
}
|
||||||
|
@ -16,10 +16,11 @@ export interface CountryInfo {
|
|||||||
locale: string;
|
locale: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export interface VersionParts {
|
export interface VersionParts {
|
||||||
major: number;
|
major: number;
|
||||||
minor: number;
|
minor: number;
|
||||||
patch: number;
|
patch: number;
|
||||||
beta?: number;
|
beta?: number;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
export type Creds = { errorLogUrl: string; telemetryUrl: string; tokenString: string };
|
||||||
|
17
yarn.lock
17
yarn.lock
@ -1447,6 +1447,14 @@
|
|||||||
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
|
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
|
||||||
integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==
|
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@*":
|
"@types/node@*":
|
||||||
version "17.0.8"
|
version "17.0.8"
|
||||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.8.tgz#50d680c8a8a78fe30abe6906453b21ad8ab0ad7b"
|
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"
|
tapable "^1.0.0"
|
||||||
worker-rpc "^0.1.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:
|
form-data@^4.0.0:
|
||||||
version "4.0.0"
|
version "4.0.0"
|
||||||
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
|
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
|
||||||
|
Loading…
Reference in New Issue
Block a user