Fix undefined checking on operation parameter

jh-changes
Shin'ya Ueoka 5 years ago
parent 52e7a3aa11
commit 04c077d614
  1. 18
      src/background/usecases/SettingUseCase.ts
  2. 26
      src/shared/operations.ts

@ -21,7 +21,12 @@ export default class SettingUseCase {
} }
async reload(): Promise<Settings> { async reload(): Promise<Settings> {
let data = await this.persistentSettingRepository.load(); let data;
try {
data = await this.persistentSettingRepository.load();
} catch (e) {
this.showUnableToLoad(e);
}
if (!data) { if (!data) {
data = DefaultSettingData; data = DefaultSettingData;
} }
@ -30,12 +35,17 @@ export default class SettingUseCase {
try { try {
value = data.toSettings(); value = data.toSettings();
} catch (e) { } catch (e) {
this.notifyPresenter.notifyInvalidSettings(() => { this.showUnableToLoad(e);
browser.runtime.openOptionsPage();
});
value = DefaultSettingData.toSettings(); value = DefaultSettingData.toSettings();
} }
this.settingRepository.update(value!!); this.settingRepository.update(value!!);
return value; return value;
} }
private showUnableToLoad(e: Error) {
console.error('unable to load settings', e);
this.notifyPresenter.notifyInvalidSettings(() => {
browser.runtime.openOptionsPage();
});
}
} }

@ -368,7 +368,9 @@ export type Operation =
const assertOptionalBoolean = (obj: any, name: string) => { const assertOptionalBoolean = (obj: any, name: string) => {
if (Object.prototype.hasOwnProperty.call(obj, name) && if (Object.prototype.hasOwnProperty.call(obj, name) &&
typeof obj[name] !== 'boolean') { typeof obj[name] !== 'boolean') {
throw new TypeError(`Not a boolean parameter: '${name}'`); throw new TypeError(
`Not a boolean parameter: '${name} (${typeof obj[name]})'`,
);
} }
}; };
@ -376,7 +378,9 @@ const assertOptionalString = (obj: any, name: string, values?: string[]) => {
if (Object.prototype.hasOwnProperty.call(obj, name)) { if (Object.prototype.hasOwnProperty.call(obj, name)) {
let value = obj[name]; let value = obj[name];
if (typeof value !== 'string') { if (typeof value !== 'string') {
throw new TypeError(`Not a string parameter: '${name}'`); throw new TypeError(
`Not a string parameter: '${name}' (${typeof value})`,
);
} }
if (values && values.length && values.indexOf(value) === -1) { if (values && values.length && values.indexOf(value) === -1) {
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
@ -421,32 +425,32 @@ export const valueOf = (o: any): Operation => {
assertOptionalBoolean(o, 'background'); assertOptionalBoolean(o, 'background');
return { return {
type: FOLLOW_START, type: FOLLOW_START,
newTab: Boolean(typeof o.newTab === undefined ? false : o.newTab), newTab: Boolean(typeof o.newTab === 'undefined' ? false : o.newTab),
background: Boolean(typeof o.background === undefined ? true : o.background), // eslint-disable-line max-len background: Boolean(typeof o.background === 'undefined' ? true : o.background), // eslint-disable-line max-len
}; };
case PAGE_HOME: case PAGE_HOME:
assertOptionalBoolean(o, 'newTab'); assertOptionalBoolean(o, 'newTab');
return { return {
type: PAGE_HOME, type: PAGE_HOME,
newTab: Boolean(typeof o.newTab === undefined ? false : o.newTab), newTab: Boolean(typeof o.newTab === 'undefined' ? false : o.newTab),
}; };
case TAB_CLOSE: case TAB_CLOSE:
assertOptionalString(o, 'select', ['left', 'right']); assertOptionalString(o, 'select', ['left', 'right']);
return { return {
type: TAB_CLOSE, type: TAB_CLOSE,
select: (typeof o.select === undefined ? 'right' : o.select), select: (typeof o.select === 'undefined' ? 'right' : o.select),
}; };
case TAB_RELOAD: case TAB_RELOAD:
assertOptionalBoolean(o, 'cache'); assertOptionalBoolean(o, 'cache');
return { return {
type: TAB_RELOAD, type: TAB_RELOAD,
cache: Boolean(typeof o.cache === undefined ? false : o.cache), cache: Boolean(typeof o.cache === 'undefined' ? false : o.cache),
}; };
case URLS_PASTE: case URLS_PASTE:
assertOptionalBoolean(o, 'newTab'); assertOptionalBoolean(o, 'newTab');
return { return {
type: URLS_PASTE, type: URLS_PASTE,
newTab: Boolean(typeof o.newTab === undefined ? false : o.newTab), newTab: Boolean(typeof o.newTab === 'undefined' ? false : o.newTab),
}; };
case INTERNAL_OPEN_URL: case INTERNAL_OPEN_URL:
assertOptionalBoolean(o, 'newTab'); assertOptionalBoolean(o, 'newTab');
@ -456,9 +460,9 @@ export const valueOf = (o: any): Operation => {
return { return {
type: INTERNAL_OPEN_URL, type: INTERNAL_OPEN_URL,
url: o.url, url: o.url,
newTab: Boolean(typeof o.newTab === undefined ? false : o.newTab), newTab: Boolean(typeof o.newTab === 'undefined' ? false : o.newTab),
newWindow: Boolean(typeof o.newWindow === undefined ? false : o.newWindow), // eslint-disable-line max-len newWindow: Boolean(typeof o.newWindow === 'undefined' ? false : o.newWindow), // eslint-disable-line max-len
background: Boolean(typeof o.background === undefined ? true : o.background), // eslint-disable-line max-len background: Boolean(typeof o.background === 'undefined' ? true : o.background), // eslint-disable-line max-len
}; };
case CANCEL: case CANCEL:
case ADDON_ENABLE: case ADDON_ENABLE: