Skip to content

Commit

Permalink
Add SwiftPolyglotError and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pereBohigas committed Mar 24, 2024
1 parent 3f1f43c commit 483ccdf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Sources/SwiftPolyglot/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ let swiftPolyglot: SwiftPolyglot = .init(
do {
try swiftPolyglot.run()
} catch {
print(error)
print(error.localizedDescription)
exit(EXIT_FAILURE)
}
6 changes: 2 additions & 4 deletions Sources/SwiftPolyglotCore/SwiftPolyglot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public struct SwiftPolyglot {

public func run() throws {
guard !arguments.isEmpty else {
print("Usage: script.swift <language codes> [--errorOnMissing]")
exit(1)
throw SwiftPolyglotError.noLanguageCodes
}

let isRunningFromGitHubActions = ProcessInfo.processInfo.environment["GITHUB_ACTIONS"] == "true"
Expand Down Expand Up @@ -79,8 +78,7 @@ public struct SwiftPolyglot {
searchDirectory()

if missingTranslations, errorOnMissing {
print("Error: One or more translations are missing.")
exit(1)
throw SwiftPolyglotError.missingTranslations
} else if missingTranslations {
print("Completed with missing translations.")
} else {
Expand Down
17 changes: 17 additions & 0 deletions Sources/SwiftPolyglotCore/SwiftPolyglotError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Foundation

enum SwiftPolyglotError: Error {
case missingTranslations
case noLanguageCodes
}

extension SwiftPolyglotError: LocalizedError {
public var errorDescription: String? {
switch self {
case .missingTranslations:
return "Error: One or more translations are missing."
case .noLanguageCodes:
return "Usage: script.swift <language codes> [--errorOnMissing]"
}
}
}

0 comments on commit 483ccdf

Please sign in to comment.