Skip to content

Commit

Permalink
Provide better error for nonexistent link target
Browse files Browse the repository at this point in the history
UX enhancement regarding #580.

Previously MacDown delegates error-handling to Finder when a link
to nonexistent local file is clicked in preview (via NSWorkspace).
This leads to a generic "Can't find the file XXX" message, which
is not really useful.

This patch detects a file URL in the above scenario, and shows a
better message inside MacDown that provides an absolute path
(instead of just the filename), and hints the user to use the
auto-create feature if desired.

Behaviours are unchanged for cases which...

* The URL is not a local file.
* The target file exists.
* The auto-create option is on.
  • Loading branch information
uranusjr committed Mar 17, 2016
1 parent 7cece8d commit 9c2b4ff
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions MacDown/Code/Document/MPDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ - (void)webView:(WebView *)webView
request:(NSURLRequest *)request frame:(WebFrame *)frame
decisionListener:(id<WebPolicyDecisionListener>)listener
{
// NSURL *url = information[WebActionOriginalURLKey];
switch ([information[WebActionNavigationTypeKey] integerValue])
{
case WebNavigationTypeLinkClicked:
Expand Down Expand Up @@ -1595,14 +1596,29 @@ - (BOOL)isCurrentBaseUrl:(NSURL *)another

- (void)openOrCreateFileForUrl:(NSURL *)url
{
// TODO: Make this togglable in preferences.
// If this is a file URL and the target does not exist, create and open it.
if (self.preferences.createFileForLinkTarget && url.isFileURL
&& ![[NSFileManager defaultManager] fileExistsAtPath:url.path])
// If the URL points to a nonexistent file, create automatically if
// requested, or provide better error message.
if (url.isFileURL && ![url checkResourceIsReachableAndReturnError:NULL])
{
NSDocumentController *controller =
[NSDocumentController sharedDocumentController];
[controller openUntitledDocumentForURL:url display:YES error:NULL];
if (self.preferences.createFileForLinkTarget)
{
NSDocumentController *controller =
[NSDocumentController sharedDocumentController];
[controller openUntitledDocumentForURL:url display:YES error:NULL];
return;
}

NSAlert *alert = [[NSAlert alloc] init];
NSString *template = NSLocalizedString(
@"File not found at path:\n%@",
@"preview navigation error message");
alert.messageText = [NSString stringWithFormat:template, url.path];
alert.informativeText = NSLocalizedString(
@"Please check the path of your link is correct. Turn on "
@"“Automatically create link targets” If you want MacDown to "
@"create nonexistent link targets for you",
@"preview navigation error information");
[alert runModal];
return;
}

Expand Down

0 comments on commit 9c2b4ff

Please sign in to comment.