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

error update #13

Merged
merged 1 commit into from
Nov 11, 2020
Merged
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
138 changes: 122 additions & 16 deletions Sources/Networking/NetworkingError.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,49 @@
//
// NetworkingError.swift
//
//
//
// Created by Sacha DSO on 30/01/2020.
//

import Foundation

public struct NetworkingError: Error {

public struct NetworkingError: Error, LocalizedError {
public enum Status: Int {
case unknown = -1
case networkUnreachable = 0

case unableToParseResponse = 1

case unableToParseRequest = 2

// 1xx Informational
case continueError = 100
case switchingProtocols = 101
case processing = 102

// 2xx Success
case ok = 200
case created = 201
case accepted = 202
case nonAuthoritativeInformation = 203
case noContent = 204
case resetContent = 205
case partialContent = 206
case multiStatus = 207
case alreadyReported = 208
case IMUsed = 226

// 3xx Redirection
case multipleChoices = 300
case movedPermanently = 301
case found = 302
case seeOther = 303
case notModified = 304
case useProxy = 305
case switchProxy = 306
case temporaryRedirect = 307
case permenantRedirect = 308

// 4xx Client Error
case badRequest = 400
case unauthorized = 401
Expand Down Expand Up @@ -44,14 +73,14 @@ public struct NetworkingError: Error {
case tooManyRequests = 429
case requestHeaderFieldsTooLarge = 431
case unavailableForLegalReasons = 451

// 4xx nginx
case noResponse = 444
case sslCertificateError = 495
case sslCertificateRequired = 496
case httpRequestSentToHTTPSPort = 497
case clientClosedRequest = 499

// 5xx Server Error
case internalServerError = 500
case notImplemented = 501
Expand All @@ -64,33 +93,110 @@ public struct NetworkingError: Error {
case loopDetected = 508
case notExtended = 510
case networkAuthenticationRequired = 511

// domain
case cancelled = -999
case badURL = -1000
case timedOut = -1001
case unsupportedURL = -1002
case cannotFindHost = -1003
case cannotConnectToHost = -1004
case networkConnectionLost = -1005
case dnsLookupFailed = -1006
case httpTooManyRedirects = -1007
case resourceUnavailable = -1008
case notConnectedToInternet = -1009
case redirectToNonExistentLocation = -1010
case badServerResponse = -1011
case userCancelledAuthentication = -1012
case userAuthenticationRequired = -1013
case zeroByteResource = -1014
case cannotDecodeRawData = -1015
case cannotDecodeContentData = -1016
case cannotParseResponse = -1017
case appTransportSecurityRequiresSecureConnection = -1022
case fileDoesNotExist = -1100
case fileIsDirectory = -1101
case noPermissionsToReadFile = -1102
case dataLengthExceedsMaximum = -1103

// SSL errors
case secureConnectionFailed = -1200
case serverCertificateHasBadDate = -1201
case serverCertificateUntrusted = -1202
case serverCertificateHasUnknownRoot = -1203
case serverCertificateNotYetValid = -1204
case clientCertificateRejected = -1205
case CclientCertificateRequired = -1206

case cannotLoadFromNetwork = -2000

// Download and file I/O errors
case cannotCreateFile = -3000
case cannotOpenFile = -3001
case cannotCloseFile = -3002
case cannotWriteToFile = -3003
case CcannotRemoveFile = -3004
case cannotMoveFile = -3005
case downloadDecodingFailedMidStream = -3006
case downloadDecodingFailedToComplete = -3007
}

public var status: Status
public var code: Int { return status.rawValue }

public var jsonPayload: Any?

public init(httpStatusCode: Int) {
self.status = Status(rawValue: httpStatusCode) ?? .unknown
public init(errorCode: Int) {
self.status = Status(rawValue: errorCode) ?? .unknown
}

public init(status: Status) {
self.status = status
}

public init(error: Error) {
if let networkingError = error as? NetworkingError {
self.status = networkingError.status
} else {
if let theError = error as? URLError {
self.status = Status(rawValue: theError.errorCode) ?? .unknown
} else {
self.status = .unknown
}
}
}

// for LocalizedError protocol
public var errorDescription: String? {
return "\(self.status)"
}

}

extension NetworkingError: CustomStringConvertible {

public var description: String {
return String(describing: self.status)
.replacingOccurrences(of: "(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])",
with: " ",
options: [.regularExpression])
.capitalized
}

}

extension NetworkingError {

public static var unableToParseResponse: NetworkingError {
return NetworkingError(httpStatusCode: Status.unableToParseResponse.rawValue)
return NetworkingError(status: .unableToParseResponse)
}


public static var unableToParseRequest: NetworkingError {
return NetworkingError(status: .unableToParseRequest)
}

public static var unknownError: NetworkingError {
return NetworkingError(status: .unknown)
}

}
Loading