Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit 211d7fc

Browse files
committed
Display JSON errors in alerts while editing doc properties
1 parent 56d9550 commit 211d7fc

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

Source/DocEditor.m

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ @implementation DocEditor
2121
BOOL _readOnly;
2222
BOOL _untitled;
2323
JSONItem* _root;
24+
int _editErrorCount;
2425
BOOL _cancelingEdit;
2526

2627
IBOutlet DBWindowController* _dbWindowController;
2728
IBOutlet NSOutlineView* __weak _outline;
28-
IBOutlet NSButton *_addPropertyButton, *_removePropertyButton, *_addColumnButton;
29+
IBOutlet NSButton *_addPropertyButton, *_removePropertyButton;
2930
IBOutlet NSButton *_saveButton, *_revertButton;
3031
IBOutlet JSONKeyFormatter *_keyFormatter;
3132
}
@@ -119,6 +120,7 @@ - (JSONItem*) selectedOrClickedProperty {
119120

120121

121122
- (BOOL) saveDocument {
123+
NSAssert(!_readOnly, @"Shouldn't save changes in read-only mode!");
122124
NSDictionary* properties = _root.value;
123125
if (!_readOnly && _root && ![properties isEqual: _revision.properties]) {
124126
CBLDocument* doc;
@@ -258,8 +260,6 @@ - (void) enablePropertyButtons {
258260
BOOL canRemove = !_readOnly && selectedProperty && !selectedProperty.isSpecial;
259261
_addPropertyButton.enabled = canInsert;
260262
_removePropertyButton.enabled = canRemove;
261-
_addColumnButton.enabled = selectedProperty && selectedProperty.isSpecial;
262-
_addColumnButton.title = (selectedProperty && [_dbWindowController hasColumnForProperty: selectedProperty.path]) ? @"Remove Column" : @"Add Column";
263263
}
264264

265265

@@ -331,6 +331,7 @@ - (BOOL)outlineView:(NSOutlineView *)outlineView
331331
}
332332
if (isKeyColumn)
333333
_keyFormatter.item = item;
334+
_editErrorCount = 0;
334335
return YES;
335336
}
336337

@@ -367,6 +368,29 @@ - (void) outlineView: (NSOutlineView*)outlineView
367368
}
368369

369370

371+
- (BOOL) control: (NSControl*)control
372+
didFailToFormatString: (NSString*)string
373+
errorDescription: (NSString*)errorMessage
374+
{
375+
if (_cancelingEdit)
376+
return YES;
377+
NSBeep();
378+
if (++_editErrorCount >= 2) {
379+
NSAlert* alert = [NSAlert new];
380+
alert.messageText = @"Invalid JSON in property value";
381+
alert.informativeText = errorMessage;
382+
[alert addButtonWithTitle: @"Continue"];
383+
[alert addButtonWithTitle: @"Cancel"];
384+
[alert beginSheetModalForWindow: control.window
385+
completionHandler:^(NSModalResponse returnCode) {
386+
if (returnCode == NSAlertSecondButtonReturn)
387+
[self cancelOperation: self];
388+
}];
389+
}
390+
return NO;
391+
}
392+
393+
370394
- (void)outlineViewSelectionDidChange:(NSNotification *)notification {
371395
[self enablePropertyButtons];
372396
}

Source/JSONFormatter.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ - (BOOL)getObjectValue:(out id *)obj
4444
return YES;
4545
} else {
4646
if (errorMessage)
47-
*errorMessage = error.localizedDescription;
47+
*errorMessage = error.userInfo[@"NSDebugDescription"];
4848
return NO;
4949
}
5050
}

0 commit comments

Comments
 (0)