Sources/KeycaskCore/KeycaskError.swift
40 lines · 1414 bytes
1public enum KeycaskError: Error, Equatable, Sendable {
2 case notFound(String)
3 case ambiguous(name: String, candidates: [Entry])
4 case cannotDecrypt
5 case corrupt(String)
6 case vaultExists(String)
7 case noVault(String)
8 case duplicateID(EntryID)
9 case io(String)
10 case usage(String)
11 case failure(String)
12
13 public var exitCode: Int32 {
14 switch self {
15 case .failure, .io, .corrupt, .vaultExists, .duplicateID: 1
16 case .usage: 2
17 case .notFound, .noVault: 3
18 case .cannotDecrypt: 4
19 case .ambiguous: 5
20 }
21 }
22
23 public var message: String {
24 switch self {
25 case .notFound(let what): "\(what): not found"
26 case .ambiguous(let name, let candidates):
27 (["\(name): ambiguous, use an id:"]
28 + candidates.map { " \($0.id.rawValue) \($0.username ?? "") \($0.url ?? "")" })
29 .joined(separator: "\n")
30 case .cannotDecrypt: "cannot decrypt: wrong passphrase or damaged vault"
31 case .corrupt(let why): "vault is corrupt: \(why)"
32 case .vaultExists(let path): "vault \(path) already exists"
33 case .noVault(let path): "vault \(path) not found (run `keycask init`)"
34 case .duplicateID(let id): "duplicate id \(id.rawValue)"
35 case .io(let why): why
36 case .usage(let why): why
37 case .failure(let why): why
38 }
39 }
40}