Skip to content

Commit 1e5a6b1

Browse files
authored
Merge pull request swiftlang#227 from dabelknap/check-file-exists
More gracefully check that an input file exists
2 parents 83a9e1f + 961ac97 commit 1e5a6b1

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
public enum SwiftFormatError: Error {
14+
case fileNotFound
15+
}

Sources/SwiftFormat/SwiftFormatter.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public final class SwiftFormatter {
5050
public func format<Output: TextOutputStream>(
5151
contentsOf url: URL, to outputStream: inout Output
5252
) throws {
53+
guard FileManager.default.isReadableFile(atPath: url.path) else {
54+
throw SwiftFormatError.fileNotFound
55+
}
5356
let sourceFile = try SyntaxTreeParser.parse(url)
5457
try format(syntax: sourceFile, assumingFileURL: url, to: &outputStream)
5558
}

Sources/SwiftFormat/SwiftLinter.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public final class SwiftLinter {
4646
/// - Parameters url: The URL of the file containing the code to format.
4747
/// - Throws: If an unrecoverable error occurs when formatting the code.
4848
public func lint(contentsOf url: URL) throws {
49+
guard FileManager.default.isReadableFile(atPath: url.path) else {
50+
throw SwiftFormatError.fileNotFound
51+
}
4952
let sourceFile = try SyntaxTreeParser.parse(url)
5053
try lint(syntax: sourceFile, assumingFileURL: url)
5154
}

0 commit comments

Comments
 (0)