improve error messages

This commit is contained in:
2026-07-13 16:43:52 +02:00
parent a49f78cbb0
commit a894449d15
2 changed files with 15 additions and 20 deletions
@@ -10,7 +10,7 @@ extension PHBarDelegate {
do {
controller = try factory.make(for: screen)
} catch {
stderr("skipping screen \(screen.localizedName): \(error)")
stderr("skipping screen \(screen.localizedName): \(error.localizedDescription)")
return false
}
let window = BarWindow(controller: controller)
@@ -3,39 +3,34 @@ import TOML
extension PHConfig {
/// Load configuration from the resolved config directory
/// (see `PHPaths`). If the file doesn't exist or fails to parse, the
/// bundled default is used.
/// (see `PHPaths`). If the file doesn't exist or fails to parse,
/// the execution is interrupted.
static func load() throws -> PHConfig {
let url = PHPaths.configDirectory.appending(path: "config.toml")
if FileManager.default.fileExists(atPath: url.relativePath) {
return try load(from: url)
} else {
return try loadFromBundle()
}
}
throw PHBar.Error(
"""
Configuration file not found.
Make sure to have a file named `config.toml` inside the
directory: `\(url.deletingLastPathComponent().relativePath)`
/// Load the bundled configuration.
/// If the file doesn't exist or fails to parse, the execution is interrupted.
static private func loadFromBundle() throws -> PHConfig {
guard
let defaultConfig = Bundle.module.url(
forResource: "config",
withExtension: "toml"
)?.resolvingSymlinksInPath()
else {
throw PHBar.Error("Failed to load config file")
Tip: If you've never used phbar before, run the `phbar install` command
to automatically generate the required configuration files.
"""
)
}
return try load(from: defaultConfig)
}
/// Decode configuration from a file at URL.
/// If the file doesn't exist or fails to parse, the execution is interrupted.
static private func load(from url: URL) throws -> PHConfig {
guard let data = try? Data(contentsOf: url),
let contents = String(data: data, encoding: .utf8)
let contents = String(data: data, encoding: .utf8), !contents.isEmpty
else {
throw PHBar.Error("Failed to load config file")
throw PHBar.Error("Configuration file not readable or empty.")
}
return try load(from: contents)
@@ -53,7 +48,7 @@ extension PHConfig {
// resolve the same set.
return config.resolvingEnv()
} catch {
throw PHBar.Error("Failed to parse config file", underlyingError: error)
throw PHBar.Error("Configuration file not valid", underlyingError: error)
}
}
}