Skip to content

Commit

Permalink
feat: hide logs behind debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
lahgolz committed Jul 15, 2023
1 parent 37fd6c6 commit d045ec1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
"sonarjs/no-duplicate-string": "off",
"sonarjs/no-identical-functions": "off",
"sonarjs/no-inverted-boolean-check": "error",
"sonarjs/cognitive-complexity": "off",

"sort-imports": "off",
"import/order": [
Expand Down
38 changes: 24 additions & 14 deletions src/handlers/message-handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { BaseResponseMessage, MessagePromise, RequestMessage, ResponseMessage } from '../types';
import { PINET_REGEX } from '../util';
import { DEBUG, PINET_REGEX } from '../util';

/**
* Handler for messages
Expand Down Expand Up @@ -53,15 +53,19 @@ export class MessageHandler {
const messageToSend = { id, ...message };
const hostPlatformURL = MessageHandler.getHostPlatformURL();

console.log(`Sending message to app platform (target origin: ${hostPlatformURL}):`, messageToSend);
if (DEBUG) {
console.log(`Sending message to app platform (target origin: ${hostPlatformURL}):`, messageToSend);
}

window.parent.postMessage(JSON.stringify(messageToSend), hostPlatformURL);

return new Promise((resolve: (message: ResponseMessage<M['type']>) => void, reject) => {
MessageHandler.emittedPromises[id] = { resolve, reject };

setTimeout(() => {
console.error(`Messaging promise with id ${id} timed out after 120000ms.`);
if (DEBUG) {
console.error(`Messaging promise with id ${id} timed out after 120000ms.`);
}

reject();
}, 120_000);
Expand All @@ -78,7 +82,9 @@ export class MessageHandler {

try {
if (typeof event.data !== 'string') {
console.log('Received message with non-string data:', event.data);
if (DEBUG) {
console.log('Received message with non-string data:', event.data);
}

return;
}
Expand All @@ -89,7 +95,9 @@ export class MessageHandler {
throw new Error('No id found in message response');
}

console.log(`Received response for message id ${parsedData.id}:`, parsedData);
if (DEBUG) {
console.log(`Received response for message id ${parsedData.id}:`, parsedData);
}

if (!(parsedData.id in MessageHandler.emittedPromises)) {
throw new Error(`No emitted promise found for native messaging response id ${parsedData.id}`);
Expand All @@ -99,15 +107,17 @@ export class MessageHandler {
MessageHandler.emittedPromises[parsedData.id].resolve(parsedData);
delete MessageHandler.emittedPromises[parsedData.id];
} catch (error) {
console.error(
`Native messaging: error when handling ${
parsedData.id === undefined
? `incoming message (possible response?)`
: `response for message id ${parsedData.id}`
}. Error is logged below.`,
);
console.error(error);
console.error(event.data);
if (DEBUG) {
console.error(
`Native messaging: error when handling ${
parsedData.id === undefined
? `incoming message (possible response?)`
: `response for message id ${parsedData.id}`
}. Error is logged below.`,
);
console.error(error);
console.error(event.data);
}

if (parsedData.id && parsedData.id in MessageHandler.emittedPromises) {
MessageHandler.emittedPromises[parsedData.id].reject(error);
Expand Down
22 changes: 16 additions & 6 deletions src/handlers/request-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Route, type RoutePayload, type RouteResult, postNetworkError } from '@pinetwork-js/api-typing';
import axios, { type AxiosError, type AxiosInstance, type RawAxiosRequestConfig } from 'axios';
import { MessageType } from '../message-types';
import { getDateTime } from '../util';
import { DEBUG, getDateTime } from '../util';
import type { CommunicationInformationResponsePayload, RequestMessage } from '../types';

/**
Expand Down Expand Up @@ -78,7 +78,9 @@ export class RequestHandler {
public handleError(error: AxiosError): void {
const errorCode = error.response?.status;

console.error(error);
if (DEBUG) {
console.error(error);
}

this.sendMessageToPiNetwork({
type: errorCode !== 401 && errorCode !== 403 ? MessageType.UNKNOWN_ERROR : MessageType.AUTH_ERROR,
Expand Down Expand Up @@ -155,7 +157,9 @@ export class RequestHandler {
*/
public waitForAction<M extends RequestMessage<M['type']>>(awaitedMessage: M): Promise<M> {
return new Promise((resolve, reject) => {
console.log('Waiting for action...');
if (DEBUG) {
console.log('Waiting for action...');
}

const timeout = window.setTimeout(() => {
reject(new Error('timeout'));
Expand Down Expand Up @@ -191,15 +195,21 @@ export class RequestHandler {
try {
parsedData = JSON.parse(event.data);
} catch {
console.warn('Error while parsing request', event, event.data);
if (DEBUG) {
console.warn('Error while parsing request', event, event.data);
}

return;
}

console.log('Message!', parsedData);
if (DEBUG) {
console.log('Message!', parsedData);
}

if (!parsedData) {
console.warn('Unable to parse action');
if (DEBUG) {
console.warn('Unable to parse action');
}

return;
}
Expand Down
1 change: 1 addition & 0 deletions src/util/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const PINET_REGEX = /https:\/\/([\dA-Za-z]+).pinet.com(\/?[^#?]*)\??([^#]*)#?(.*)/;
export const DEBUG: boolean = false;

0 comments on commit d045ec1

Please sign in to comment.