Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ParseError should includes originalError #438

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions Sources/ParseSwift/Extensions/URLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,16 @@ internal extension URLSession {
if let responseError = responseError {
guard let parseError = responseError as? ParseError else {
return .failure(ParseError(code: .unknownError,
message: "Unable to connect with parse-server: \(responseError)"))
message: "Unable to connect with parse-server: \(responseError)",
originalError: responseError))
}
return .failure(parseError)
}
guard let response = urlResponse else {
guard let parseError = responseError as? ParseError else {
return .failure(ParseError(code: .unknownError,
message: "No response from server"))
return .failure(ParseError(code: .unknownError,
message: "No response from server",
originalError: responseError))
}
return .failure(parseError)
}
Expand Down Expand Up @@ -116,11 +118,13 @@ internal extension URLSession {
}
return .failure(ParseError(code: .unknownError,
// swiftlint:disable:next line_length
message: "Error decoding parse-server response: \(response) with error: \(String(describing: error)) Format: \(String(describing: String(data: responseData, encoding: .utf8)))"))
message: "Error decoding parse-server response: \(response) with error: \(String(describing: error)) Format: \(String(describing: String(data: responseData, encoding: .utf8)))",
originalError: nsError))
}
return .failure(ParseError(code: .unknownError,
// swiftlint:disable:next line_length
message: "Error decoding parse-server response: \(response) with error: \(String(describing: error)) Format: \(String(describing: String(data: json, encoding: .utf8)))"))
message: "Error decoding parse-server response: \(response) with error: \(String(describing: error)) Format: \(String(describing: String(data: json, encoding: .utf8)))",
originalError: error))
}
return .failure(parseError)
}
Expand All @@ -138,14 +142,16 @@ internal extension URLSession {
guard let response = urlResponse else {
guard let parseError = responseError as? ParseError else {
return .failure(ParseError(code: .unknownError,
message: "No response from server"))
message: "No response from server",
originalError: responseError))
}
return .failure(parseError)
}
if let responseError = responseError {
guard let parseError = responseError as? ParseError else {
return .failure(ParseError(code: .unknownError,
message: "Unable to connect with parse-server: \(responseError)"))
message: "Unable to connect with parse-server: \(responseError)",
originalError: responseError))
}
return .failure(parseError)
}
Expand All @@ -157,7 +163,8 @@ internal extension URLSession {
} catch {
let defaultError = ParseError(code: .unknownError,
// swiftlint:disable:next line_length
message: "Error decoding parse-server response: \(response) with error: \(String(describing: error))")
message: "Error decoding parse-server response: \(response) with error: \(String(describing: error))",
originalError: error)
let parseError = error as? ParseError ?? defaultError
return .failure(parseError)
}
Expand Down
30 changes: 30 additions & 0 deletions Sources/ParseSwift/Types/ParseError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,25 @@ import Foundation
An object with a Parse code and message.
*/
public struct ParseError: ParseTypeable, Swift.Error {

public static func == (lhs: ParseError, rhs: ParseError) -> Bool {
if let lhsE = lhs.originalError as? NSError,
let rhsE = rhs.originalError as? NSError
{
return lhsE.code == rhsE.code
}

return lhs.code == rhs.code
}

/// The value representing the error from the Parse Server.
public let code: Code
/// The text representing the error from the Parse Server.
public let message: String
/// An error value representing a custom error from the Parse Server.
public let otherCode: Int?
/// The original error
public let originalError: Swift.Error?
let error: String?

enum CodingKeys: String, CodingKey {
Expand Down Expand Up @@ -379,7 +392,22 @@ public extension ParseError {
self.message = message
self.otherCode = nil
self.error = nil
self.originalError = nil
}

/**
Create an error with a known code and custom message.
- parameter code: The known Parse code.
- parameter message: The custom message.
- parameter originalError: The original Error.
*/
init(code: Code, message: String, originalError: Error? = nil) {
self.code = code
self.message = message
self.otherCode = nil
self.error = nil
self.originalError = originalError
}

/**
Create an error with a custom code and custom message.
Expand All @@ -391,6 +419,7 @@ public extension ParseError {
self.message = message
self.otherCode = otherCode
self.error = nil
self.originalError = nil
}
}

Expand Down Expand Up @@ -419,6 +448,7 @@ extension ParseError {
message = try values.decode(String.self, forKey: .error)
}
self.error = nil
self.originalError = nil
}
}

Expand Down