improve error handling
This commit is contained in:
parent
765f223fef
commit
84bab48627
2 changed files with 23 additions and 12 deletions
|
@ -146,13 +146,17 @@ v8::Local<v8::Value> SystemPreferences::GetUserDefault(
|
||||||
|
|
||||||
void SystemPreferences::RegisterDefaults(mate::Arguments* args) {
|
void SystemPreferences::RegisterDefaults(mate::Arguments* args) {
|
||||||
base::DictionaryValue value;
|
base::DictionaryValue value;
|
||||||
args->GetNext(&value);
|
|
||||||
|
|
||||||
@try {
|
if(!args->GetNext(&value)) {
|
||||||
NSDictionary* dict = DictionaryValueToNSDictionary(value);
|
|
||||||
[[NSUserDefaults standardUserDefaults] registerDefaults:dict];
|
|
||||||
} @catch (NSException* exception) {
|
|
||||||
args->ThrowError("Invalid userDefault data provided");
|
args->ThrowError("Invalid userDefault data provided");
|
||||||
|
} else {
|
||||||
|
args->GetNext(&value);
|
||||||
|
@try {
|
||||||
|
NSDictionary* dict = DictionaryValueToNSDictionary(value);
|
||||||
|
[[NSUserDefaults standardUserDefaults] registerDefaults:dict];
|
||||||
|
} @catch (NSException* exception) {
|
||||||
|
args->ThrowError("Invalid userDefault data provided");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,18 +35,20 @@ describe('systemPreferences module', () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe.only('systemPreferences.registerDefaults(defaults)', () => {
|
describe('systemPreferences.registerDefaults(defaults)', () => {
|
||||||
before(function () {
|
before(function () {
|
||||||
if (process.platform !== 'darwin') this.skip()
|
if (process.platform !== 'darwin') this.skip()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('registers defaults', () => {
|
it('registers defaults', () => {
|
||||||
|
// to pass into registerDefaults
|
||||||
const defaultsDict = {
|
const defaultsDict = {
|
||||||
'one': 'ONE',
|
'one': 'ONE',
|
||||||
'two': 2,
|
'two': 2,
|
||||||
'three': [1, 2, 3]
|
'three': [1, 2, 3]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// to test type
|
||||||
const defaultsMap = [
|
const defaultsMap = [
|
||||||
{ key: 'one', type: 'string', value: 'ONE' },
|
{ key: 'one', type: 'string', value: 'ONE' },
|
||||||
{ key: 'two', value: 2, type: 'integer' },
|
{ key: 'two', value: 2, type: 'integer' },
|
||||||
|
@ -63,16 +65,21 @@ describe('systemPreferences module', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throws when bad defaults are passed', () => {
|
it('throws when bad defaults are passed', () => {
|
||||||
const badDefaults1 = { 'one': null }
|
const badDefaults1 = { 'one': null } // catches null values
|
||||||
//const badDefaults1 = { 'one': null }
|
const badDefaults2 = 1 // argument must be a dictionary
|
||||||
|
const badDefaults3 = null // argument can't be null
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
systemPreferences.registerDefaults(badDefaults1)
|
systemPreferences.registerDefaults(badDefaults1)
|
||||||
}, 'Invalid userDefault data provided')
|
}, 'Invalid userDefault data provided')
|
||||||
//
|
|
||||||
// assert.throws(() => {
|
assert.throws(() => {
|
||||||
// systemPreferences.registerDefaults(badDefaults1)
|
systemPreferences.registerDefaults(badDefaults2)
|
||||||
// }, 'Invalid userDefault data provided')
|
}, 'Invalid userDefault data provided')
|
||||||
|
|
||||||
|
assert.throws(() => {
|
||||||
|
systemPreferences.registerDefaults(badDefaults3)
|
||||||
|
}, 'Invalid userDefault data provided')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue