{"id":2421,"date":"2017-02-09T11:47:45","date_gmt":"2017-02-09T06:17:45","guid":{"rendered":"https:\/\/www.innovationm.com\/blog\/?p=2421"},"modified":"2017-02-09T11:47:45","modified_gmt":"2017-02-09T06:17:45","slug":"error-handling-in-swift-3-0","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/error-handling-in-swift-3-0\/","title":{"rendered":"Error Handling in Swift 3.0"},"content":{"rendered":"<p><strong>Error Handling:<\/strong><\/p>\n<p class=\"para\"><em>Error is an issue or unexpected condition that may cause a program to not function properly.<\/em>\u00a0Error can be compile time or runtime.<br \/>\nError Handling is a process of\u00a0identifying, catching\u00a0and recovering \u00a0the error conditions.<\/p>\n<p class=\"para\">Here we are trying to handle server side error which occurred while sending request or receiving response. These errors maybe of the following types:<\/p>\n<ol>\n<li class=\"para\">No internet connection<\/li>\n<li class=\"para\">Connection Failed i.e. Request time out, bad url, unsupported url, cannot find host, network connection lost, Resource unavailable.<\/li>\n<li class=\"para\">Security\/ Authentication: It may be App\u00a0authentication or User authentication.<\/li>\n<li class=\"para\">Infrastructure error i.e. Version mismatch, Database connectivity problem<\/li>\n<li class=\"para\">Server App Issue: Server application have any issue to respond the request.<\/li>\n<li class=\"para\">Validation Error: The request is correct but due to some validation response is not expected. These are server&#8217;s custom error.<\/li>\n<\/ol>\n<p>All errors from 1 to 5 returns object of Error protocol. Validation errors are custom errors which are generated by server app. So here we don&#8217;t receive any Error Object. Here we need to make our own class object.<br \/>\nTo do that make your custom error handling class in which you can handle all of these errors from one class<\/p>\n<p class=\"para\"><strong>Required Classes and methods:<\/strong><\/p>\n<p>To do that we need two classes:<\/p>\n<ol>\n<li><strong>ErrorManager:\u00a0\u00a0<\/strong>In this class we define\u00a0a method\n<p class=\"p1\"><em><span class=\"s1\">processError(error: <\/span><span class=\"s2\">Error<\/span><span class=\"s1\">? = <\/span><span class=\"s3\">nil<\/span><span class=\"s1\">, errorCode: <\/span><span class=\"s2\">Int<\/span><span class=\"s1\">? = <\/span><span class=\"s3\">nil<\/span><span class=\"s1\">, errorMsg: <\/span><span class=\"s2\">String<\/span><span class=\"s1\">? = <\/span><span class=\"s3\">nil<\/span><span class=\"s1\">) -&gt; <\/span><span class=\"s4\">ErrorModel.<br \/>\n<\/span><\/em><span class=\"s4\">This method is called where we receive any error in response from http\u00a0or any validation from our server which is not an object of a error class.<\/span><\/p>\n<\/li>\n<li><strong>ErrorModel:\u00a0<\/strong>In ErrorModel class we have two methods, one is for error object other is to make custom error.<\/li>\n<\/ol>\n<p><strong>Steps to Handle error:<\/strong><\/p>\n<ol>\n<li>Make class ErrorManager and define a static method\u00a0<strong><strong><em><span class=\"s1\">processError(error: <\/span><span class=\"s2\">Error<\/span><span class=\"s1\">? = <\/span><span class=\"s3\">nil<\/span><span class=\"s1\">, errorCode: <\/span><span class=\"s2\">Int<\/span><span class=\"s1\">? = <\/span><span class=\"s3\">nil<\/span><span class=\"s1\">, errorMsg: <\/span><span class=\"s2\">String<\/span><span class=\"s1\">? = <\/span><span class=\"s3\">nil<\/span><span class=\"s1\">) -&gt; <\/span><span class=\"s4\">ErrorModel.<br \/>\n<\/span><\/em><\/strong><\/strong><\/p>\n<pre class=\"lang:swift decode:true\">    \r\n    static func processError(error: Error? = nil, errorCode: Int? = nil, errorMsg: String? = nil) -&gt; ErrorModel {\r\n       \r\n        var errorModel: ErrorModel?\r\n        if error == nil {\r\n            errorModel = ErrorModel.error(errorCode: errorCode!, errorMessage: errorMsg)     \r\n        } else {\r\n            errorModel = ErrorModel.error(error: error!)\r\n        }\r\n        return errorModel!\r\n    }\r\n<\/pre>\n<p>In this method we have three\u00a0parameters:<\/p>\n<ul>\n<li><em>error:\u00a0<\/em> Object of Error protocol received from server.<\/li>\n<li><em>errorCode:<\/em>\u00a0Custom or your server&#8217;s error code.<\/li>\n<li><em>errorMessage:\u00a0<\/em>Server&#8217;s error description or your custom message.<\/li>\n<\/ul>\n<p><strong><em><span class=\"s4\"><br \/>\n<\/span><\/em><\/strong><span class=\"s4\">You can use this method by passing either <\/span><span class=\"s4\">Error object or error code and <\/span><span class=\"s4\">error message i.e<\/span><span class=\"s4\">.<br \/>\n<\/span><\/p>\n<pre class=\"lang:swift decode:true\">ErrorManager.processError(errorCode: errorCode, errorMsg: errorMsg)<\/pre>\n<p>OR<\/p>\n<pre class=\"lang:swift decode:true\">ErrorManager.processError(error: response.result.error)\r\n<\/pre>\n<\/li>\n<li>There are two methods defined in ErrorModel class, one is for Error Object and other is for custom error. Define variables code, message and title which may used later in your view controller.\n<pre class=\"lang:swift decode:true\">    \r\n    \/\/\/ For Error Model\r\n    static func error(error: Error) -&gt; ErrorModel {\r\n        \r\n        let code = error._code\r\n        let message = getErrorMessage(error: error)\r\n        let title = getErrorTitle(error: error)\r\n        \r\n        return ErrorModel(errorTitle: title, errorCode: code, errorMessage: message)\r\n    }\r\n\r\n    \/\/\/ For custom error\r\n    static func error(errorCode: Int, errorMessage: String?) -&gt; ErrorModel {\r\n        \r\n        let errorModel: ErrorModel?\r\n        \r\n        \/\/ For all errors from server\r\n        errorModel = ErrorModel(errorTitle: nil, errorCode: errorCode, errorMessage: errorMessage! )\r\n        \r\n        return errorModel!\r\n        \r\n    }\r\n<\/pre>\n<p>Here you can change message by checking error code.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Error Handling: Error is an issue or unexpected condition that may cause a program to not function properly.\u00a0Error can be compile time or runtime. Error Handling is a process of\u00a0identifying, catching\u00a0and recovering \u00a0the error conditions. Here we are trying to handle server side error which occurred while sending request or receiving response. These errors maybe [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2506,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,71,8],"tags":[160,122,89,183,178],"class_list":["post-2421","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-mobile","category-mobile-architecture-and-design","tag-ios","tag-ipad","tag-iphone","tag-swift","tag-swift-3-0"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Error Handling in Swift 3.0 | InnovationM Blog<\/title>\n<meta name=\"description\" content=\"Error Handling in Swift\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.innovationm.com\/blog\/error-handling-in-swift-3-0\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Error Handling in Swift 3.0 | InnovationM Blog\" \/>\n<meta property=\"og:description\" content=\"Error Handling in Swift\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/error-handling-in-swift-3-0\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2017-02-09T06:17:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/Error-Handling.png\" \/>\n\t<meta property=\"og:image:width\" content=\"901\" \/>\n\t<meta property=\"og:image:height\" content=\"501\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/error-handling-in-swift-3-0\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/error-handling-in-swift-3-0\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"Error Handling in Swift 3.0\",\"datePublished\":\"2017-02-09T06:17:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/error-handling-in-swift-3-0\\\/\"},\"wordCount\":396,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/error-handling-in-swift-3-0\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/02\\\/Error-Handling.png\",\"keywords\":[\"iOS\",\"iPad\",\"iPhone\",\"Swift\",\"Swift 3.0\"],\"articleSection\":[\"iOS\",\"Mobile\",\"Mobile Architecture and Design\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/error-handling-in-swift-3-0\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/error-handling-in-swift-3-0\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/error-handling-in-swift-3-0\\\/\",\"name\":\"Error Handling in Swift 3.0 | InnovationM Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/error-handling-in-swift-3-0\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/error-handling-in-swift-3-0\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/02\\\/Error-Handling.png\",\"datePublished\":\"2017-02-09T06:17:45+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"description\":\"Error Handling in Swift\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/error-handling-in-swift-3-0\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/error-handling-in-swift-3-0\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/error-handling-in-swift-3-0\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/02\\\/Error-Handling.png\",\"contentUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/02\\\/Error-Handling.png\",\"width\":901,\"height\":501},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/error-handling-in-swift-3-0\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Error Handling in Swift 3.0\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"InnovationM - Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"http:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Error Handling in Swift 3.0 | InnovationM Blog","description":"Error Handling in Swift","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.innovationm.com\/blog\/error-handling-in-swift-3-0\/","og_locale":"en_US","og_type":"article","og_title":"Error Handling in Swift 3.0 | InnovationM Blog","og_description":"Error Handling in Swift","og_url":"https:\/\/www.innovationm.com\/blog\/error-handling-in-swift-3-0\/","og_site_name":"InnovationM - Blog","article_published_time":"2017-02-09T06:17:45+00:00","og_image":[{"width":901,"height":501,"url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/Error-Handling.png","type":"image\/png"}],"author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/error-handling-in-swift-3-0\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/error-handling-in-swift-3-0\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"Error Handling in Swift 3.0","datePublished":"2017-02-09T06:17:45+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/error-handling-in-swift-3-0\/"},"wordCount":396,"commentCount":0,"image":{"@id":"https:\/\/www.innovationm.com\/blog\/error-handling-in-swift-3-0\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/Error-Handling.png","keywords":["iOS","iPad","iPhone","Swift","Swift 3.0"],"articleSection":["iOS","Mobile","Mobile Architecture and Design"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/error-handling-in-swift-3-0\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/error-handling-in-swift-3-0\/","url":"https:\/\/www.innovationm.com\/blog\/error-handling-in-swift-3-0\/","name":"Error Handling in Swift 3.0 | InnovationM Blog","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/error-handling-in-swift-3-0\/#primaryimage"},"image":{"@id":"https:\/\/www.innovationm.com\/blog\/error-handling-in-swift-3-0\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/Error-Handling.png","datePublished":"2017-02-09T06:17:45+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"description":"Error Handling in Swift","breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/error-handling-in-swift-3-0\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/error-handling-in-swift-3-0\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.innovationm.com\/blog\/error-handling-in-swift-3-0\/#primaryimage","url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/Error-Handling.png","contentUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2017\/02\/Error-Handling.png","width":901,"height":501},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/error-handling-in-swift-3-0\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Error Handling in Swift 3.0"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"InnovationM - Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["http:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/2421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=2421"}],"version-history":[{"count":0,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/2421\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media\/2506"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=2421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=2421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=2421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}