Skip to content

Commit

Permalink
refactor: add response message to error
Browse files Browse the repository at this point in the history
  • Loading branch information
lovetodream committed Dec 9, 2022
1 parent a256893 commit c9814c2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions Sources/LoggingLoki/LokiError.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
enum LokiError: Error {
case invalidResponse
import struct Foundation.Data
import protocol Foundation.LocalizedError

enum LokiError: LocalizedError {
case invalidResponse(Data?)

var errorDescription: String? {
switch self {
case .invalidResponse(let data):
guard let data else { return nil }
return String(data: data, encoding: .utf8)
}
}
}
2 changes: 1 addition & 1 deletion Sources/LoggingLoki/LokiSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ extension URLSession: LokiSession {
} else if let httpResponse = response as? HTTPURLResponse, (200..<300).contains(httpResponse.statusCode) {
completion(.success(httpResponse.statusCode))
} else {
completion(.failure(LokiError.invalidResponse))
completion(.failure(LokiError.invalidResponse(data)))
}
}
task.resume()
Expand Down

0 comments on commit c9814c2

Please sign in to comment.