Skip to content

Commit

Permalink
refactor: add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nickybondarenko committed Apr 9, 2024
1 parent 68b5d43 commit 510aa7a
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions Sources/Confidence/EventStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import Foundation
import os

internal protocol EventStorage {
func startNewBatch() throws
func writeEvent(event: Event) throws
func batchReadyFiles() throws -> [String]
func eventsFrom(id: String) throws -> [Event]
func startNewBatch()
func writeEvent(event: Event)
func batchReadyIds() -> [String]
func eventsFrom(id: String) -> [Event]
func remove(id: String)
}

Expand All @@ -23,23 +23,39 @@ internal class EventStorageImpl: EventStorage {
fileURL = folderURL.appendingPathComponent("events-\(Date().currentTime)")
}

func startNewBatch() throws {
func startNewBatch() {
let urlString = "\(fileURL)"+"\(EventStorageImpl.READYTOSENDEXTENSION)"

Check warning on line 27 in Sources/Confidence/EventStorage.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Operator Usage Whitespace Violation: Operators should be surrounded by a single whitespace when they are being used (operator_usage_whitespace)
let newPath = URL(fileURLWithPath: urlString)
try FileManager.default.moveItem(at: fileURL, to: newPath)
do {
try FileManager.default.moveItem(at: fileURL, to: newPath)
} catch {
Logger(subsystem: "com.confidence.eventsender", category: "storage").error(
"Error when trying to start a new batch: \(error)")
}
fileURL = folderURL.appendingPathComponent("events-\(Date().currentTime)")
currentBatch = []
}

Check warning on line 38 in Sources/Confidence/EventStorage.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
func writeEvent(event: Event) throws {
func writeEvent(event: Event) {
currentBatch.append(event)
let data = try encoder.encode(currentBatch)
try data.write(to: fileURL, options: .atomic)
do {
let data = try encoder.encode(currentBatch)
try data.write(to: fileURL, options: .atomic)
} catch {
Logger(subsystem: "com.confidence.eventsender", category: "storage").error(
"Error when trying to write to disk: \(error)")
}
}

Check warning on line 49 in Sources/Confidence/EventStorage.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
func batchReadyFiles() throws -> [String] {
func batchReadyIds() -> [String] {
var readyFilesList: [String] = []
let directoryContents = try FileManager.default.contentsOfDirectory(atPath: folderURL.absoluteString)
var directoryContents: [String] = []
do {
directoryContents = try FileManager.default.contentsOfDirectory(atPath: folderURL.absoluteString)
} catch {
Logger(subsystem: "com.confidence.eventsender", category: "storage").error(
"Error when trying to read contents of directory on disk: \(error)")
}
for file in directoryContents {
if file.hasSuffix(EventStorageImpl.READYTOSENDEXTENSION) {

Check warning on line 60 in Sources/Confidence/EventStorage.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Prefer For-Where Violation: `where` clauses are preferred over a single `if` inside a `for` (for_where)
readyFilesList.append(file)
Expand All @@ -48,18 +64,23 @@ internal class EventStorageImpl: EventStorage {
return readyFilesList
}

Check warning on line 66 in Sources/Confidence/EventStorage.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
func eventsFrom(id: String) throws -> [Event] {
let currentData = try Data(contentsOf: fileURL)

let events = try decoder.decode([Event].self, from: currentData)
func eventsFrom(id: String) -> [Event] {
var events: [Event] = []
do {
let currentData = try Data(contentsOf: URL(string: id)!)

Check warning on line 70 in Sources/Confidence/EventStorage.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Force Unwrapping Violation: Force unwrapping should be avoided (force_unwrapping)
events = try decoder.decode([Event].self, from: currentData)
} catch {
Logger(subsystem: "com.confidence.eventsender", category: "storage").error(
"Error when trying to get events at path: \(error)")
}
return events
}

func remove(id: String) {
do {
try FileManager.default.removeItem(atPath: id)
} catch {
Logger(subsystem: "com.confidence.eventstorage", category: "storage").error(
Logger(subsystem: "com.confidence.eventsender", category: "storage").error(
"Error when trying to delete an event batch: \(error)")
}
}
Expand Down

0 comments on commit 510aa7a

Please sign in to comment.