I'm trying to check whether the statusCode is equal to 2xx, however i'm not quite sure how to create this regex code for that type of error handling. Here is my code at the moment with a variable that retrieve the statusCode from header?
func SignUpWithPassword(email: String, password: String, delegate: CreateUserDelegate) {
testProvider.request(.SignIn(email, password), completion: { result in
switch result {
case let .Success(response):
let statusCode = response.statusCode
case let .Failure(error):
print(error)
let description = "Error! Please try again!"
delegate.CreateUser(didError: description)
}
})
delegate.CreateUser(didSucceed: SignUpUser(email: email, password: password))
}
I believe that NSHTTPURLResponse status code is an Int.
you should be able to use a ranged expression on this in your branching logic:
switch response.statusCode
{
case 200 ... 299 :
print("Success")
default:
print("Failure")
}
I'm using this enum:
enum HTTPStatusCodeGroup: Int {
case Info = 100
case Success = 200
case Redirect = 300
case Client = 400
case Server = 500
case Unknown = 999
init(code: Int) {
switch code {
case 100...199: self = .Info
case 200...299: self = .Success
case 300...399: self = .Redirect
case 400...499: self = .Client
case 500...599: self = .Server
default: self = .Unknown
}
}
}
In Swift, I have created the own Regax class.
// Custom Regex class
class Regex {
let internalExpression: NSRegularExpression
let pattern: String
init(_ pattern: String) {
self.pattern = pattern
do {
self.internalExpression = try NSRegularExpression(pattern: self.pattern, options: .CaseInsensitive)
} catch _ {
self.internalExpression = NSRegularExpression.init();
}
}
func test(input: String) -> Bool {
let matches = self.internalExpression.matchesInString(input, options: .WithTransparentBounds, range: NSMakeRange(0, input.characters.count)) as Array<NSTextCheckingResult>
return matches.count > 0
}
}
// Override operator
infix operator =~ {}
func =~ (input: NSInteger, pattern: String) -> Int {
return (Regex(pattern).test(String(input))) ? input : -1;
}
// Example add it viewDidLoad method
let statusCode = 200
switch statusCode
{
case statusCode =~ "2[0-9][0-9]" :
print("2[0-9][0-9] Success")
break
case statusCode =~ "3[0-9][0-9]" :
print("3[0-9][0-9] Success")
break
case statusCode =~ "4[0-9][0-9]" :
print("4[0-9][0-9] Success")
break
case statusCode =~ "5[0-9][0-9]" :
print("5[0-9][0-9] Success")
break
default:
print("Failure")
}
Related
I'm actually trying to some code logic with swift for training purpose at the moment, I was wondering what is the proper way to throw my error from my init ?
So the flow is Controller ask for account creation when initializing Model is asking my sql manager to create the account and this method return the result from a closure.
But something feels wrong, should I just use a return from the sql manager who contained both my Int? and Error? ?
init(_ username: String, _ password: String) throws {
self.id = 0
self.username = username
self.password = password
var toThrow: Error? = nil
// Register in database
userManager.create(self) { (id: Int?, err: Error?) in
Thread.sleep(forTimeInterval: 10)
if let error = err {
// Register in database goes wrong
debugPrint("Handle error from user creation...")
toThrow = error
} else {
// There is no id and no error ?
guard let _ = id else { return }
self.id = id!
}
}
if let error = toThrow {
throw error
}
}
If you are on Swift 5 you could look into using Result and define your closure like
(id: Int) -> Result<Int, Error>
and change your code to
userManager.create(self) { (id: Int?) -> Result<Int, Error> in
Thread.sleep(forTimeInterval: 10)
if let error = err {
// Register in database goes wrong
debugPrint("Handle error from user creation...")
return .failure(error)
} else {
// There is no id and no error ?
guard let _ = id else { return }
return .success(id)
}
}
If you have your own error enum for the Db class like
enum DbError {
case create
case update
//...
}
Then you can use that type in the closure declaration
(id: Int?) -> Result<Int, DbError>
and return a specific error for this action
return .failure(.create)
Note that I haven't compiled this so consider it an example
Here is the solution if people want to see:
#IBAction func didPressRegister() {
guard let username = usernameField.text else { return }
guard let password = passwordField.text else { return }
let user = UserModel(username, password)
userManager.create(user) { result in
switch(result) {
case .failure(let error):
// TODO: UIAlert
debugPrint(error)
case .success(let int):
// TODO: Generate user token and redirect main
debugPrint(int)
}
}
}
// TODO
public func create(_ user: UserModel, _ complete: #escaping (Result<Int, Error>) -> ()) {
debugPrint("Requested to create the user... \(user)")
complete(.failure(toThrow.ACCOUNT_ERROR))
}
I have a few functions that are basically the same apart from a few variable names that they reference. I want to abstract the function so that I don't have to keep duplicating the code. This is an example function:
func listenToParticipantNumber() {
guard let reference = participantNumberReference else {
return
}
guard participantNumberListener == nil else {
return
}
participantNumberListener = backendClient.listenToRtdbProperty(reference) { [weak self] (result: Result<Int, RequestError>) in
guard let strongSelf = self else {
return
}
switch result {
case .success(let participantNumber):
strongSelf.participantNumber = participantNumber
case .failure:
break
}
}
}
In another function, I'd switch out participantNumberReference, participantNumber, and participantNumberListener for different variables (which are all private to my class), and the block return type of Int. But the core layout of the function would be the same.
How can I make this process cleaner to reuse this code rather than having to duplicate it? Is it possible to somehow use KeyPaths to reference different variables of my class?
It takes a lot of pre-amble to achieve this abstraction level, so you have to ask larger questions about What You're Really Trying To Do, and Is It Worth It, but here's an outline. The essence is to use a dictionary with properties instead of explicitly declared variables.
Now you can add future cases to PersonType and initialize them (which may be automate-able) and the function will apply to all of them regardless. PersonType could conceivably be declared outside the class to increase separation.
This is probably a better suited for Code Review, where you can post a lot more context.
enum PersonType { case Participant, case Leader, case Observer }
struct TrackedObject { var number: Int; var numberReference: ReferenceProtocol; var numberListener: ListenerProtocol; }
// Instead of 9 private variables, make a Dictionary of 3 objects each of
// which have 3 properties
private var persons: [ PersonType: TrackedObject ] = ...
func listenToPersonOfType(personType: PersonType) {
guard let reference = persons[personType].numberReference else {
return
}
guard persons[personType].numberListener == nil else {
return
}
persons[personType].numberListener = backendClient.listenToRtdbProperty(reference) { [weak self] (result: Result<Int, RequestError>) in
guard let strongSelf = self else {
return
}
switch result {
case .success(let number):
strongSelf.persons[personType].number = number
case .failure:
break
}
}
}
I think you're on the right road with a KeyPath. I would probably refactor this a little bit so that the listen method returns Listener? (whatever type that is), and hoist the "if listener is already set, don't do this," outside of this function.
That would leave you something along these lines:
func listen<Response>(reference: Int?, // Or whatever type this is
updating keyPath: WritableKeyPath<C, Response>,
completion: (Result<Response, Error>) -> Void) -> Listener? {
guard let reference = reference else { return nil }
return backendClient.listenToRtdbProperty(reference) { [weak self] (result: Result<Response, Error>) in
guard var strongSelf = self else {
return
}
switch result {
case .success(let result):
strongSelf[keyPath: keyPath] = result
case .failure:
break
}
}
Or you can keep your existing structure with something like this:
func listen<Response>(reference: Int?,
updating keyPath: WritableKeyPath<C, Response>,
forListener listener: WritableKeyPath<C, Listener?>,
completion: (Result<Response, Error>) -> Void) {
guard let reference = reference else { return }
guard self[keyPath: listener] == nil else {
return
}
var mutableSelf = self
mutableSelf[keyPath: listener] = backendClient.listenToRtdbProperty(reference) { [weak self] (result: Result<Response, Error>) in
guard var strongSelf = self else {
return
}
switch result {
case .success(let result):
strongSelf[keyPath: keyPath] = result
case .failure:
break
}
}
}
(C in all of this code is "this class." We can't yet write Self to mean "the current class.")
So I solved it like this:
private func listen<T>(
to property: ReferenceWritableKeyPath<LivestreamModel, T?>,
withReference propertyReference: ReferenceWritableKeyPath<LivestreamModel, String?>,
listener: ReferenceWritableKeyPath<LivestreamModel, DatabaseHandle?>) {
guard let reference = self[keyPath: propertyReference] else {
return
}
guard self[keyPath: listener] == nil else {
return
}
self[keyPath: listener] = backendClient.listenToRtdbProperty(reference) { [weak self] (result: Result<T, RequestError>) in
switch result {
case .success(let value):
self?[keyPath: property] = value
case .failure:
self?[keyPath: property] = nil
}
}
}
I actually did this before Rob replied, but I had to use ReferenceWritableKeyPath because it gives a warning that self has no subscript members when using WritableKeyPath.
Now I want query Result with different object(using generic)
I create DataType enum to handle different status
enum DataType{
case Case
case Product
case Category
case Composition
}
and query fuction called query<T:Object>
func query<T:Object>(type: DataType,id: Int,complete: #escaping (_ success:Bool,_ results:Results<T>?,_ error:String?) -> ()) {
switch type {
case .Case:
guard let caseResult = realm.objects(Case.self).filter("id = \(id)") as? Results<T> else {
complete(false ,nil ,"Query case failed.")
return
}
complete(true ,caseResult,nil)
return
case .Category:
guard let categoryResult = realm.objects(Category.self).filter("id = \(id)") as? Results<T> else {
complete(false ,nil ,"Query category failed.")
return
}
complete(true ,categoryResult,nil)
return
case .Product:
guard let productResult = realm.objects(Product.self).filter("id = \(id)") as? Results<T> else {
complete(false ,nil ,"Query category failed.")
return
}
complete(true ,productResult,nil)
return
case .Composition:
guard let compositionResult = realm.objects(Composition.self).filter("id = \(id)") as? Results<T> else {
complete(false ,nil ,"Query category failed.")
return
}
complete(true ,compositionResult,nil)
return
}
}
But when I created this fuction, and Xcode said
Will never be execut
It means the closure can never execute from this fuction?
Can anyone help with this problem?Thanks!
What I am trying to achieve is perform a URLSession request in swift 3. I am performing this action in a separate function (so as not to write the code separately for GET and POST) and returning the URLSessionDataTask and handling the success and failure in closures. Sort of like this-
let task = URLSession.shared.dataTask(with: request) { (data, uRLResponse, responseError) in
DispatchQueue.main.async {
var httpResponse = uRLResponse as! HTTPURLResponse
if responseError != nil && httpResponse.statusCode == 200{
successHandler(data!)
}else{
if(responseError == nil){
//Trying to achieve something like below 2 lines
//Following line throws an error soo its not possible
//var errorTemp = Error(domain:"", code:httpResponse.statusCode, userInfo:nil)
//failureHandler(errorTemp)
}else{
failureHandler(responseError!)
}
}
}
}
I do not wish to handle the error condition in this function and wish to generate an error using the response code and return this Error to handle it wherever this function is called from.
Can anybody tell me how to go about this? Or is this not the "Swift" way to go about handling such situations?
In your case, the error is that you're trying to generate an Error instance. Error in Swift 3 is a protocol that can be used to define a custom error. This feature is especially for pure Swift applications to run on different OS.
In iOS development the NSError class is still available and it conforms to Error protocol.
So, if your purpose is only to propagate this error code, you can easily replace
var errorTemp = Error(domain:"", code:httpResponse.statusCode, userInfo:nil)
with
var errorTemp = NSError(domain:"", code:httpResponse.statusCode, userInfo:nil)
Otherwise check the Sandeep Bhandari's answer regarding how to create a custom error type
You can create a protocol, conforming to the Swift LocalizedError protocol, with these values:
protocol OurErrorProtocol: LocalizedError {
var title: String? { get }
var code: Int { get }
}
This then enables us to create concrete errors like so:
struct CustomError: OurErrorProtocol {
var title: String?
var code: Int
var errorDescription: String? { return _description }
var failureReason: String? { return _description }
private var _description: String
init(title: String?, description: String, code: Int) {
self.title = title ?? "Error"
self._description = description
self.code = code
}
}
You should use NSError object.
let error = NSError(domain: "", code: 401, userInfo: [ NSLocalizedDescriptionKey: "Invalid access token"])
Then cast NSError to Error object.
You can create enums to deal with errors :)
enum RikhError: Error {
case unknownError
case connectionError
case invalidCredentials
case invalidRequest
case notFound
case invalidResponse
case serverError
case serverUnavailable
case timeOut
case unsuppotedURL
}
and then create a method inside enum to receive the http response code and return the corresponding error in return :)
static func checkErrorCode(_ errorCode: Int) -> RikhError {
switch errorCode {
case 400:
return .invalidRequest
case 401:
return .invalidCredentials
case 404:
return .notFound
//bla bla bla
default:
return .unknownError
}
}
Finally update your failure block to accept single parameter of type RikhError :)
I have a detailed tutorial on how to restructure traditional Objective - C based Object Oriented network model to modern Protocol Oriented model using Swift3 here https://learnwithmehere.blogspot.in Have a look :)
Hope it helps :)
Details
Xcode Version 10.2.1 (10E1001)
Swift 5
Solution of organizing errors in an app
import Foundation
enum AppError {
case network(type: Enums.NetworkError)
case file(type: Enums.FileError)
case custom(errorDescription: String?)
class Enums { }
}
extension AppError: LocalizedError {
var errorDescription: String? {
switch self {
case .network(let type): return type.localizedDescription
case .file(let type): return type.localizedDescription
case .custom(let errorDescription): return errorDescription
}
}
}
// MARK: - Network Errors
extension AppError.Enums {
enum NetworkError {
case parsing
case notFound
case custom(errorCode: Int?, errorDescription: String?)
}
}
extension AppError.Enums.NetworkError: LocalizedError {
var errorDescription: String? {
switch self {
case .parsing: return "Parsing error"
case .notFound: return "URL Not Found"
case .custom(_, let errorDescription): return errorDescription
}
}
var errorCode: Int? {
switch self {
case .parsing: return nil
case .notFound: return 404
case .custom(let errorCode, _): return errorCode
}
}
}
// MARK: - FIle Errors
extension AppError.Enums {
enum FileError {
case read(path: String)
case write(path: String, value: Any)
case custom(errorDescription: String?)
}
}
extension AppError.Enums.FileError: LocalizedError {
var errorDescription: String? {
switch self {
case .read(let path): return "Could not read file from \"\(path)\""
case .write(let path, let value): return "Could not write value \"\(value)\" file from \"\(path)\""
case .custom(let errorDescription): return errorDescription
}
}
}
Usage
//let err: Error = NSError(domain:"", code: 401, userInfo: [NSLocalizedDescriptionKey: "Invaild UserName or Password"])
let err: Error = AppError.network(type: .custom(errorCode: 400, errorDescription: "Bad request"))
switch err {
case is AppError:
switch err as! AppError {
case .network(let type): print("Network ERROR: code \(type.errorCode), description: \(type.localizedDescription)")
case .file(let type):
switch type {
case .read: print("FILE Reading ERROR")
case .write: print("FILE Writing ERROR")
case .custom: print("FILE ERROR")
}
case .custom: print("Custom ERROR")
}
default: print(err)
}
Implement LocalizedError:
struct StringError : LocalizedError
{
var errorDescription: String? { return mMsg }
var failureReason: String? { return mMsg }
var recoverySuggestion: String? { return "" }
var helpAnchor: String? { return "" }
private var mMsg : String
init(_ description: String)
{
mMsg = description
}
}
Note that simply implementing Error, for instance, as described in one of the answers, will fail (at least in Swift 3), and calling localizedDescription will result in the string "The operation could not be completed. (.StringError error 1.)"
I still think that Harry's answer is the simplest and completed but if you need something even simpler, then use:
struct AppError {
let message: String
init(message: String) {
self.message = message
}
}
extension AppError: LocalizedError {
var errorDescription: String? { return message }
// var failureReason: String? { get }
// var recoverySuggestion: String? { get }
// var helpAnchor: String? { get }
}
And use or test it like this:
printError(error: AppError(message: "My App Error!!!"))
func print(error: Error) {
print("We have an ERROR: ", error.localizedDescription)
}
let error = NSError(domain:"", code:401, userInfo:[ NSLocalizedDescriptionKey: "Invaild UserName or Password"]) as Error
self.showLoginError(error)
create an NSError object and typecast it to Error ,show it anywhere
private func showLoginError(_ error: Error?) {
if let errorObj = error {
UIAlertController.alert("Login Error", message: errorObj.localizedDescription).action("OK").presentOn(self)
}
}
protocol CustomError : Error {
var localizedTitle: String
var localizedDescription: String
}
enum RequestError : Int, CustomError {
case badRequest = 400
case loginFailed = 401
case userDisabled = 403
case notFound = 404
case methodNotAllowed = 405
case serverError = 500
case noConnection = -1009
case timeOutError = -1001
}
func anything(errorCode: Int) -> CustomError? {
return RequestError(rawValue: errorCode)
}
I know you have already satisfied with an answer but if you are interested to know the right approach, then this might be helpful for you.
I would prefer not to mix http-response error code with the error code in the error object (confused? please continue reading a bit...).
The http response codes are standard error codes about a http response defining generic situations when response is received and varies from 1xx to 5xx ( e.g 200 OK, 408 Request timed out,504 Gateway timeout etc - http://www.restapitutorial.com/httpstatuscodes.html )
The error code in a NSError object provides very specific identification to the kind of error the object describes for a particular domain of application/product/software. For example your application may use 1000 for "Sorry, You can't update this record more than once in a day" or say 1001 for "You need manager role to access this resource"... which are specific to your domain/application logic.
For a very small application, sometimes these two concepts are merged. But they are completely different as you can see and very important & helpful to design and work with large software.
So, there can be two techniques to handle the code in better way:
1. The completion callback will perform all the checks
completionHandler(data, httpResponse, responseError)
2. Your method decides success and error situation and then invokes corresponding callback
if nil == responseError {
successCallback(data)
} else {
failureCallback(data, responseError) // failure can have data also for standard REST request/response APIs
}
Happy coding :)
I am having trouble figuring out the syntax for accessing the raw value of an enum. The code below should clarify what I am trying to do.
enum Result<T, U> {
case Success(T)
case Failure(U)
}
struct MyError: ErrorType {
var status: Int = 0
var message: String = "An undefined error has occured."
init(status: Int, message: String) {
self.status = status
self.message = message
}
}
let goodResult: Result<String, MyError> = .Success("Some example data")
let badResult: Result<String, MyError> = .Failure(MyError(status: 401, message: "Unauthorized"))
var a: String = goodResult //<--- How do I get the string out of here?
var b: MyError = badResult //<--- How do I get the error out of here?
You can make it without switch like this:
if case .Success(let str) = goodResult {
a = str
}
It's not the prettiest way, but playing around in a playground I was able to extract that value using a switch and case:
switch goodResult {
case let .Success(val):
print(val)
case let .Failure(err):
print(err.message)
}
EDIT:
A prettier way would be to write a method for the enum that returns a tuple of optional T and U types:
enum Result<T, U> {
case Success(T)
case Failure(U)
func value() -> (T?, U?) {
switch self {
case let .Success(value):
return (value, nil)
case let .Failure(value):
return (nil, value)
}
}
}