Refactor the type of os from String to an closest enum kind of thing

Summary: This diff updates the type of os in Store from string to a custom enum kind of type.

Reviewed By: passy

Differential Revision: D13622598

fbshipit-source-id: c57a1f2eedbe9e88d43c681c2fa6ca72b93e8808
This commit is contained in:
Pritesh Nandgaonkar
2019-01-10 09:52:52 -08:00
committed by Facebook Github Bot
parent d868cd6405
commit 3b9253680f
4 changed files with 55 additions and 47 deletions

View File

@@ -26,7 +26,7 @@ import os from 'os';
import util from 'util';
import path from 'path';
import type {Notification} from '../../plugin';
import type {Store, DeviceLogEntry} from 'flipper';
import type {Store, DeviceLogEntry, OS} from 'flipper';
export type Crash = {|
notificationID: string,
@@ -82,7 +82,7 @@ export function getNewPersisitedStateFromCrashLog(
persistedState: ?PersistedState,
persistingPlugin: Class<FlipperDevicePlugin<> | FlipperPlugin<>>,
content: string,
os: ?string,
os: ?OS,
): ?PersistedState {
const persistedStateReducer = persistingPlugin.persistedStateReducer;
if (!os || !persistedStateReducer) {
@@ -145,9 +145,9 @@ export function parseCrashLogAndUpdateState(
export function shouldShowCrashNotification(
baseDevice: ?BaseDevice,
content: string,
os: ?string,
os: ?OS,
): boolean {
if (os && os.includes('Android')) {
if (os && os === 'Android') {
return true;
}
const appPath = parsePath(content);
@@ -159,49 +159,54 @@ export function shouldShowCrashNotification(
return true;
}
export function parseCrashLog(content: string, os: string): CrashLog {
export function parseCrashLog(content: string, os: OS): CrashLog {
const stubString = 'Cannot figure out the cause';
if (os.includes('iOS')) {
const regex = /Exception Type: *[\w]*/;
const arr = regex.exec(content);
const exceptionString = arr ? arr[0] : '';
const exceptionRegex = /[\w]*$/;
const tmp = exceptionRegex.exec(exceptionString);
const exception =
tmp && tmp[0].length ? tmp[0] : 'Cannot figure out the cause';
const crash = {
callstack: content,
name: exception,
reason: exception,
};
return crash;
} else if (os.includes('Android')) {
const regForName = /.*\n/;
const nameRegArr = regForName.exec(content);
let name = nameRegArr ? nameRegArr[0] : stubString;
const regForCallStack = /\tat[\w\s\n.$&+,:;=?@#|'<>.^*()%!-]*$/;
const callStackArray = regForCallStack.exec(content);
const callStack = callStackArray ? callStackArray[0] : '';
let remainingString =
callStack.length > 0 ? content.replace(callStack, '') : '';
if (remainingString[remainingString.length - 1] === '\n') {
remainingString = remainingString.slice(0, -1);
switch (os) {
case 'iOS': {
const regex = /Exception Type: *[\w]*/;
const arr = regex.exec(content);
const exceptionString = arr ? arr[0] : '';
const exceptionRegex = /[\w]*$/;
const tmp = exceptionRegex.exec(exceptionString);
const exception =
tmp && tmp[0].length ? tmp[0] : 'Cannot figure out the cause';
const crash = {
callstack: content,
name: exception,
reason: exception,
};
return crash;
}
const reason =
remainingString.length > 0
? remainingString.split('\n').pop()
: stubString;
if (name[name.length - 1] === '\n') {
name = name.slice(0, -1);
case 'Android': {
const regForName = /.*\n/;
const nameRegArr = regForName.exec(content);
let name = nameRegArr ? nameRegArr[0] : stubString;
const regForCallStack = /\tat[\w\s\n.$&+,:;=?@#|'<>.^*()%!-]*$/;
const callStackArray = regForCallStack.exec(content);
const callStack = callStackArray ? callStackArray[0] : '';
let remainingString =
callStack.length > 0 ? content.replace(callStack, '') : '';
if (remainingString[remainingString.length - 1] === '\n') {
remainingString = remainingString.slice(0, -1);
}
const reason =
remainingString.length > 0
? remainingString.split('\n').pop()
: stubString;
if (name[name.length - 1] === '\n') {
name = name.slice(0, -1);
}
const crash = {
callstack: content,
name: name,
reason: reason,
};
return crash;
}
default: {
throw new Error('Unsupported OS');
}
const crash = {
callstack: content,
name: name,
reason: reason,
};
return crash;
}
throw new Error('Unsupported OS');
}
export function parsePath(content: string): ?string {