Skip to content

Commit

Permalink
Merge pull request #1 from emadhegab/develop
Browse files Browse the repository at this point in the history
Error Dependency Removal
  • Loading branch information
emadhegab authored Jul 24, 2018
2 parents 822e80e + 4692bb1 commit 39f08f5
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 74 deletions.
16 changes: 12 additions & 4 deletions MHNetwork.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
FD75389120FCBCF100F99D83 /* Response.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD75388B20FCBCF100F99D83 /* Response.swift */; };
FD75389220FCBCF100F99D83 /* Request.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD75388C20FCBCF100F99D83 /* Request.swift */; };
FD75389520FCBCF100F99D83 /* NetworkDispatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD75388F20FCBCF100F99D83 /* NetworkDispatcher.swift */; };
FD7AB6232102447E00B95DBB /* NetworkError.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD7AB6222102447E00B95DBB /* NetworkError.swift */; };
FD7AB6232102447E00B95DBB /* StatusCodes.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD7AB6222102447E00B95DBB /* StatusCodes.swift */; };
FDA192B220FE4944001C586F /* Operations.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDA192B120FE4944001C586F /* Operations.swift */; };
FDCC0B6F20FCC9A100092F44 /* ResponseTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDCC0B6D20FCC9A100092F44 /* ResponseTest.swift */; };
FDCC0B7020FCC9A100092F44 /* NetworkDispatcherTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDCC0B6E20FCC9A100092F44 /* NetworkDispatcherTest.swift */; };
Expand Down Expand Up @@ -48,7 +48,7 @@
FD75388B20FCBCF100F99D83 /* Response.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Response.swift; sourceTree = "<group>"; };
FD75388C20FCBCF100F99D83 /* Request.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Request.swift; sourceTree = "<group>"; };
FD75388F20FCBCF100F99D83 /* NetworkDispatcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkDispatcher.swift; sourceTree = "<group>"; };
FD7AB6222102447E00B95DBB /* NetworkError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkError.swift; sourceTree = "<group>"; };
FD7AB6222102447E00B95DBB /* StatusCodes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusCodes.swift; sourceTree = "<group>"; };
FDA192B120FE4944001C586F /* Operations.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Operations.swift; sourceTree = "<group>"; };
FDCC0B6D20FCC9A100092F44 /* ResponseTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResponseTest.swift; sourceTree = "<group>"; };
FDCC0B6E20FCC9A100092F44 /* NetworkDispatcherTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkDispatcherTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -96,6 +96,14 @@
name = Frameworks;
sourceTree = "<group>";
};
FD1F50ED2105D7C900313702 /* Helpers */ = {
isa = PBXGroup;
children = (
FD7AB6222102447E00B95DBB /* StatusCodes.swift */,
);
path = Helpers;
sourceTree = "<group>";
};
FD75386520FCBCC700F99D83 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -145,7 +153,7 @@
FD75388C20FCBCF100F99D83 /* Request.swift */,
FDA192B120FE4944001C586F /* Operations.swift */,
FD75388F20FCBCF100F99D83 /* NetworkDispatcher.swift */,
FD7AB6222102447E00B95DBB /* NetworkError.swift */,
FD1F50ED2105D7C900313702 /* Helpers */,
);
path = Network;
sourceTree = "<group>";
Expand Down Expand Up @@ -325,7 +333,7 @@
FD75389520FCBCF100F99D83 /* NetworkDispatcher.swift in Sources */,
FD75389020FCBCF100F99D83 /* Environment.swift in Sources */,
FDA192B220FE4944001C586F /* Operations.swift in Sources */,
FD7AB6232102447E00B95DBB /* NetworkError.swift in Sources */,
FD7AB6232102447E00B95DBB /* StatusCodes.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion MHNetwork/Network/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ public protocol Dispatcher {
/// - Parameter request: request to execute
/// - Returns: promise
func execute(request: Request, completion: @escaping (Response) -> Void,
onError: @escaping (Error) -> Void) throws
onError: @escaping (ErrorItem) -> Void) throws

}
109 changes: 109 additions & 0 deletions MHNetwork/Network/Helpers/StatusCodes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// CustomError.swift
// MHNetwork
//
// Created by Mohamed Emad Abdalla Hegab on 20.07.18.
// Copyright © 2018 Mohamed Hegab. All rights reserved.
//

import Foundation

public enum HTTPStatusCodes: Int {
case unspecified = 0 // for testing perpuse
// 100 Informational
case `continue` = 100
case switchingProtocols
case processing
// 200 Success
case ok = 200
case created
case accepted
case nonAuthoritativeInformation
case noContent
case resetContent
case partialContent
case multiStatus
case alreadyReported
case imUsed = 226
// 300 Redirection
case multipleChoices = 300
case movedPermanently
case found
case seeOther
case notModified
case useProxy
case switchProxy
case temporaryRedirect
case permanentRedirect
// 400 Client Error
case badRequest = 400
case unauthorized
case paymentRequired
case forbidden
case notFound
case methodNotAllowed
case notAcceptable
case proxyAuthenticationRequired
case requestTimeout
case conflict
case gone
case lengthRequired
case preconditionFailed
case payloadTooLarge
case uriTooLong
case unsupportedMediaType
case rangeNotSatisfiable
case expectationFailed
case imATeapot
case misdirectedRequest = 421
case unprocessableEntity
case locked
case failedDependency
case upgradeRequired = 426
case preconditionRequired = 428
case tooManyRequests
case requestHeaderFieldsTooLarge = 431
case unavailableForLegalReasons = 451
// 500 Server Error
case internalServerError = 500
case notImplemented
case badGateway
case serviceUnavailable
case gatewayTimeout
case httpVersionNotSupported
case variantAlsoNegotiates
case insufficientStorage
case loopDetected
case notExtended = 510
case networkAuthenticationRequired

public var description: String {
switch self {
case .badRequest:
return "Bad Request"
case .unauthorized:
return "Unauthorized"
case .forbidden:
return "Forbidden"
case .notFound:
return "Not Found"
case .internalServerError:
return "Internal Server Error"
case .notImplemented:
return "Not implemented"
case .badGateway:
return "Bad Gateway"
case .serviceUnavailable:
return "Service Unavailable"

default:
return "Error with code: \(self.rawValue)"
}
}
}

extension HTTPStatusCodes {
public var convertStatusCodeToErrorItem: ErrorItem {
return (self, nil, nil)
}
}
6 changes: 3 additions & 3 deletions MHNetwork/Network/NetworkDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class NetworkDispatcher: Dispatcher {
}

public func execute(request: Request, completion: @escaping (Response) -> Void,
onError: @escaping (Error) -> Void) throws {
onError: @escaping (ErrorItem) -> Void) throws {

try self.prepareURLRequest(for: request, onComplete: { [weak self] (rq) in
guard let `self` = self else { return }
Expand All @@ -35,7 +35,7 @@ public class NetworkDispatcher: Dispatcher {

private func prepareURLRequest(for request: Request,
onComplete: @escaping (URLRequest) -> Void,
onError: @escaping (Error) -> Void) throws {
onError: @escaping (ErrorItem) -> Void) throws {
// Compose the url
let fullUrl = "\(environment.host)/\(request.path)"
var urlRequest: URLRequest!
Expand All @@ -59,7 +59,7 @@ public class NetworkDispatcher: Dispatcher {
return URLQueryItem(name: element.key, value: element.value as? String)
})
guard var components = URLComponents(string: fullUrl), let url = URL(string: fullUrl) else {
onError(NetworkError.runTimeError("Bad Input"))
onError((HTTPStatusCodes.notFound, nil, nil))
return
}
components.queryItems = queryParams
Expand Down
13 changes: 0 additions & 13 deletions MHNetwork/Network/NetworkError.swift

This file was deleted.

7 changes: 3 additions & 4 deletions MHNetwork/Network/Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
//

import Foundation
public typealias ErrorItem = (code: Int?, error: Error?, data: Data?)
public typealias ErrorItem = (code: HTTPStatusCodes?, error: Error?, data: Data?)
public enum Response {

case data(_: Data)
case error(ErrorItem)
case error(error: ErrorItem)

public init(_ response: (r: HTTPURLResponse?, data: Data?, error: Error?), for request: Request) {

guard let serverResponse = response.r else {
self = Response.handleNetworkError(status: 500, error: nil, data: nil)
return
Expand All @@ -38,7 +37,7 @@ public enum Response {

private static func handleNetworkError(status: Int, error: Error?, data: Data?) -> Response {
// Handle the error and convert it to application friendly error
return .error((code: status, error: error, data: data))
return .error(error: (code: HTTPStatusCodes(rawValue: status), error: error, data: data))

}
}
21 changes: 11 additions & 10 deletions MHNetworkTests/NetworkDispatcherTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private class MockQuoteTask<T: Codable>: Operations {
return MockQuoteRequest.getRandomQuote
}

func exeute(in dispatcher: Dispatcher, completed: @escaping (T) -> Void, onError: @escaping (Error) -> Void) {
func exeute(in dispatcher: Dispatcher, completed: @escaping (T) -> Void, onError: @escaping (ErrorItem) -> Void) {

do {
try dispatcher.execute(request: self.request, completion: { (response) in
Expand All @@ -83,16 +83,16 @@ private class MockQuoteTask<T: Codable>: Operations {
let object = try decoder.decode(T.self, from: data)
completed(object)
} catch let error {
onError(error)
onError((nil, error, nil))
}
break
case .error(let status, let error, let data):
case .error(let error):
onError(error)
break
}
}, onError: onError)
} catch {
onError(error)
onError((nil, error, nil))
}
}
}
Expand All @@ -105,9 +105,11 @@ private class MockBadTask<T: Codable>: Operations {
return MockBadRequest(body: body)
}

func exeute(in dispatcher: Dispatcher, completed: @escaping (T) -> Void, onError: @escaping (Error) -> Void) {
func exeute(in dispatcher: Dispatcher, completed: @escaping (T) -> Void, onError: @escaping (ErrorItem) -> Void) {
do {



try dispatcher.execute(request: request, completion: { (response) in
switch response {

Expand All @@ -117,19 +119,18 @@ private class MockBadTask<T: Codable>: Operations {
let t = try decoder.decode(T.self, from: data)
completed(t)
} catch let error {
onError(error)
onError((nil, error, nil))
}
case .error(_, let networkError, _):
guard let error = networkError else { break }
case .error(let error):
onError(error)

break
}
}, onError: { (error) in
onError(error)
})

} catch {
onError(error)
onError((nil, error, nil))
}
}
}
Expand Down
Loading

0 comments on commit 39f08f5

Please sign in to comment.