Mercurial > public > simoleon
comparison Simoleon/Jobs/FileController.swift @ 155:681f2cbe8c7f
Refactor error handling class
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Wed, 25 Aug 2021 11:00:21 +0100 |
parents | 8afba86ab8dd |
children |
comparison
equal
deleted
inserted
replaced
154:8afba86ab8dd | 155:681f2cbe8c7f |
---|---|
23 func read<T: Decodable>(json filename: String) throws -> T { | 23 func read<T: Decodable>(json filename: String) throws -> T { |
24 let data: Data | 24 let data: Data |
25 | 25 |
26 guard let file = Bundle.main.url(forResource: filename, withExtension: nil) | 26 guard let file = Bundle.main.url(forResource: filename, withExtension: nil) |
27 else { | 27 else { |
28 throw JsonErrors.fileMissing | 28 throw ErrorHandling.Json.fileMissing |
29 } | 29 } |
30 | 30 |
31 do { | 31 do { |
32 data = try Data(contentsOf: file) | 32 data = try Data(contentsOf: file) |
33 } catch { | 33 } catch { |
34 throw JsonErrors.loadFailed(cause: error.localizedDescription) | 34 throw ErrorHandling.Json.loadFailed(cause: error.localizedDescription) |
35 } | 35 } |
36 | 36 |
37 do { | 37 do { |
38 let decoder = JSONDecoder() | 38 let decoder = JSONDecoder() |
39 return try decoder.decode(T.self, from: data) | 39 return try decoder.decode(T.self, from: data) |
40 } catch { | 40 } catch { |
41 throw JsonErrors.parseFailed(cause: error.localizedDescription) | 41 throw ErrorHandling.Json.parseFailed(cause: error.localizedDescription) |
42 } | 42 } |
43 } | 43 } |
44 } | 44 } |