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 { do {
controller = try factory.make(for: screen) controller = try factory.make(for: screen)
} catch { } catch {
stderr("skipping screen \(screen.localizedName): \(error)") stderr("skipping screen \(screen.localizedName): \(error.localizedDescription)")
return false return false
} }
let window = BarWindow(controller: controller) let window = BarWindow(controller: controller)
@@ -3,39 +3,34 @@ import TOML
extension PHConfig { extension PHConfig {
/// Load configuration from the resolved config directory /// Load configuration from the resolved config directory
/// (see `PHPaths`). If the file doesn't exist or fails to parse, the /// (see `PHPaths`). If the file doesn't exist or fails to parse,
/// bundled default is used. /// the execution is interrupted.
static func load() throws -> PHConfig { static func load() throws -> PHConfig {
let url = PHPaths.configDirectory.appending(path: "config.toml") let url = PHPaths.configDirectory.appending(path: "config.toml")
if FileManager.default.fileExists(atPath: url.relativePath) { if FileManager.default.fileExists(atPath: url.relativePath) {
return try load(from: url) return try load(from: url)
} else { } 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. Tip: If you've never used phbar before, run the `phbar install` command
/// If the file doesn't exist or fails to parse, the execution is interrupted. to automatically generate the required configuration files.
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")
} }
return try load(from: defaultConfig)
} }
/// Decode configuration from a file at URL. /// Decode configuration from a file at URL.
/// If the file doesn't exist or fails to parse, the execution is interrupted. /// If the file doesn't exist or fails to parse, the execution is interrupted.
static private func load(from url: URL) throws -> PHConfig { static private func load(from url: URL) throws -> PHConfig {
guard let data = try? Data(contentsOf: url), guard let data = try? Data(contentsOf: url),
let contents = String(data: data, encoding: .utf8) let contents = String(data: data, encoding: .utf8), !contents.isEmpty
else { else {
throw PHBar.Error("Failed to load config file") throw PHBar.Error("Configuration file not readable or empty.")
} }
return try load(from: contents) return try load(from: contents)
@@ -53,7 +48,7 @@ extension PHConfig {
// resolve the same set. // resolve the same set.
return config.resolvingEnv() return config.resolvingEnv()
} catch { } catch {
throw PHBar.Error("Failed to parse config file", underlyingError: error) throw PHBar.Error("Configuration file not valid", underlyingError: error)
} }
} }
} }