improve error handling

This commit is contained in:
Shelley Vohr 2017-12-11 13:11:03 -05:00
parent 765f223fef
commit 84bab48627
No known key found for this signature in database
GPG key ID: F13993A75599653C
2 changed files with 23 additions and 12 deletions

View file

@ -146,13 +146,17 @@ v8::Local<v8::Value> SystemPreferences::GetUserDefault(
void SystemPreferences::RegisterDefaults(mate::Arguments* args) {
base::DictionaryValue value;
args->GetNext(&value);
@try {
NSDictionary* dict = DictionaryValueToNSDictionary(value);
[[NSUserDefaults standardUserDefaults] registerDefaults:dict];
} @catch (NSException* exception) {
if(!args->GetNext(&value)) {
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");
}
}
}

View file

@ -35,18 +35,20 @@ describe('systemPreferences module', () => {
})
})
describe.only('systemPreferences.registerDefaults(defaults)', () => {
describe('systemPreferences.registerDefaults(defaults)', () => {
before(function () {
if (process.platform !== 'darwin') this.skip()
})
it('registers defaults', () => {
// to pass into registerDefaults
const defaultsDict = {
'one': 'ONE',
'two': 2,
'three': [1, 2, 3]
}
// to test type
const defaultsMap = [
{ key: 'one', type: 'string', value: 'ONE' },
{ key: 'two', value: 2, type: 'integer' },
@ -63,16 +65,21 @@ describe('systemPreferences module', () => {
})
it('throws when bad defaults are passed', () => {
const badDefaults1 = { 'one': null }
//const badDefaults1 = { 'one': null }
const badDefaults1 = { 'one': null } // catches null values
const badDefaults2 = 1 // argument must be a dictionary
const badDefaults3 = null // argument can't be null
assert.throws(() => {
systemPreferences.registerDefaults(badDefaults1)
}, 'Invalid userDefault data provided')
//
// assert.throws(() => {
// systemPreferences.registerDefaults(badDefaults1)
// }, 'Invalid userDefault data provided')
assert.throws(() => {
systemPreferences.registerDefaults(badDefaults2)
}, 'Invalid userDefault data provided')
assert.throws(() => {
systemPreferences.registerDefaults(badDefaults3)
}, 'Invalid userDefault data provided')
})
})