{"id":12108,"date":"2015-08-27T14:42:20","date_gmt":"2015-08-27T18:42:20","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=12108"},"modified":"2015-08-27T14:42:20","modified_gmt":"2015-08-27T18:42:20","slug":"capturing-swift-error-context","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/08\/27\/capturing-swift-error-context\/","title":{"rendered":"Capturing Swift Error Context"},"content":{"rendered":"<p><a href=\"http:\/\/ericasadun.com\/2015\/08\/27\/capturing-context-swiftlang\/\">Erica Sadun<\/a>:<\/p>\n<blockquote cite=\"http:\/\/ericasadun.com\/2015\/08\/27\/capturing-context-swiftlang\/\"><p>Say you&rsquo;re working with a type and you want to throw an error that reflects the context of where it originates. You can do that, mostly, using a few built-in compiler keywords. <code>__FUNCTION__<\/code>, <code>__LINE__<\/code>, and <code>__FILE__<\/code> provide literal interpolation about the details of a calling function[&#8230;]<\/p><p>[&#8230;]<\/p><p>Use a protocol to automatically pick up on type context. The default implementation in the following <code>Contextualizable<\/code> extension refers to <code>self.dynamicType<\/code> in a way that a method signature cannot.<\/p><\/blockquote>\n<p>I&rsquo;ve long been doing this sort of thing in Objective-C with macros. It&rsquo;s a bug help when debugging or tracking down what happened on a customer&rsquo;s Mac. Unlike the macros, this won&rsquo;t build up a stack trace of how the error was propagated, although you could approximate that using <code>-[NSThread callStackSymbols]<\/code>.<\/p>\n<p>Previously: <a href=\"http:\/\/mjtsai.com\/blog\/2014\/07\/13\/swift-and-cocoa-error-handling\/\">Swift and Cocoa Error Handling<\/a>, <a href=\"http:\/\/mjtsai.com\/blog\/2013\/03\/21\/jserrorstacktrace\/\">JSErrorStackTrace<\/a>, <a href=\"http:\/\/mjtsai.com\/blog\/2007\/01\/07\/nsxreturnthrowerror\/\">NSXReturnThrowError<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Erica Sadun: Say you&rsquo;re working with a type and you want to throw an error that reflects the context of where it originates. You can do that, mostly, using a few built-in compiler keywords. __FUNCTION__, __LINE__, and __FILE__ provide literal interpolation about the details of a calling function[&#8230;][&#8230;]Use a protocol to automatically pick up on [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[941,857,71,901],"class_list":["post-12108","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-macros","tag-nserror","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=12108"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12108\/revisions"}],"predecessor-version":[{"id":12109,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12108\/revisions\/12109"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=12108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=12108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=12108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}